Write function to compute expected value