from fcmath.probability import expected_value, standard_deviation, variance
values = [0, 2]
probabilities = [0.5, 0.5]
expected_value(values, probabilities), variance(values, probabilities), standard_deviation(values, probabilities)(1.0, 1.0, 1.0)
Probability distributions can be large, but two summaries are especially useful: expected value and variance. Expected value describes the long-run center. Variance describes spread around that center. Together, they help compare games, risks, measurements, and uncertain decisions.
After this lesson you should be able to:
fcmath, Python, and SymPy.For a discrete random variable, expected value is:
E[X] = sum of value * probability
If X takes values 0 and 10 with probabilities 0.7 and 0.3, then
E[X] = 0(0.7) + 10(0.3) = 3
This means that over many repetitions, the average value approaches 3.
A fair die has values 1,2,3,4,5,6, each with probability 1/6.
E[X] = (1+2+3+4+5+6)/6 = 21/6 = 3.5
But a die can never roll 3.5. Expected value is a long-run average, not a promise about one trial.
Variance measures how far values tend to be from the mean. For a discrete random variable:
Var(X) = sum of probability * (value - mean)^2
Standard deviation is the square root of variance:
SD(X) = sqrt(Var(X))
Variance uses squared distances, so it is sensitive to values far from the mean.
Suppose X is 0 with probability 0.5 and 2 with probability 0.5.
First compute the mean:
E[X] = 0(0.5) + 2(0.5) = 1
Now build a table:
| value | probability | value - mean | squared distance | weighted squared distance |
|---|---|---|---|---|
| 0 | 0.5 | -1 | 1 | 0.5 |
| 2 | 0.5 | 1 | 1 | 0.5 |
Add the last column:
Var(X) = 0.5 + 0.5 = 1
SD(X) = sqrt(1) = 1
Expected value has a very useful property:
E[X + Y] = E[X] + E[Y]
This is true even when X and Y are not independent. For example, if one fair die has expected value 3.5, then two fair dice have total expected value:
3.5 + 3.5 = 7
Two games can have the same expected value but different risk.
$5 every time.$0 half the time and $10 half the time.Both have expected value $5, but Game B has more spread. Variance helps describe that uncertainty.
A game costs $2. It pays $5 with probability 0.4 and $0 with probability 0.6. Is the game favorable in expected value?
Expected payout:
5(0.4) + 0(0.6) = 2
Expected net gain:
expected payout - cost = 2 - 2 = 0
The game is fair in expected value. It is not guaranteed to break even on one play.
Distribution A:
X = 4 with probability 1
Distribution B:
Y = 0 with probability 1/2
Y = 8 with probability 1/2
Both have expected value 4. But X never varies, while Y jumps between 0 and 8. So Y has larger variance and more risk.
0, 10 with probabilities 0.7, 0.3.0, 2 with probabilities 0.5, 0.5.$5 or $0/$10 with equal probabilities?0(0.7)+10(0.3)=3.3.5.0.5(0-1)^2 + 0.5(2-1)^2 = 1.$0/$10 game is riskier because it has more spread.Expectation and variance calculations are safest when you use a table and fill one column at a time.
Guess first. Which contributes more to expected value: a large value with small probability or a smaller value with large probability?
Guided exercise.
For values 1, 4, 10 with probabilities 0.2, 0.5, 0.3:
E[X] = 1(0.2) + 4(0.5) + 10(0.3)
= 0.2 + 2.0 + 3.0
= 5.2
Each row contributes value * probability.
Checkpoint. Compute expected value for values -2, 4 with probabilities 0.25, 0.75.
Guess first. If the expected number of heads in one fair coin flip is 0.5, can one flip produce 0.5 heads?
Guided exercise.
One flip produces either 0 heads or 1 head. The expected value is
0(0.5) + 1(0.5) = 0.5
This is a long-run average over many flips.
Checkpoint. Explain why the expected value 3.5 for a fair die is useful even though no die face is 3.5.
Guess first. Why do we subtract the mean before squaring?
Guided exercise.
For values 0, 4 with probabilities 0.5, 0.5, the mean is 2. Deviations are:
0 - 2 = -2
4 - 2 = 2
Square and weight:
Var(X) = 0.5(-2)^2 + 0.5(2)^2
= 2 + 2
= 4
Checkpoint. Compute the variance for values 1, 3 with probabilities 0.5, 0.5.
Guess first. Which has more variance: always getting 5, or getting 0 or 10 with equal probability?
Guided exercise.
Always getting 5 has no spread, so variance is 0. Getting 0 or 10 has the same mean but values far from the mean, so it has positive variance.
Checkpoint. Two investments both have expected return 6%. One always returns 6%; the other returns -4% or 16% with equal probability. Which has larger variance?
Guess first. If one fair die has expected value 3.5, do three fair dice have expected total 10.5?
Guided exercise.
By linearity of expectation:
E[die1 + die2 + die3] = 3.5 + 3.5 + 3.5 = 10.5
You do not need to list all 216 outcomes.
Checkpoint. If one raffle ticket has expected value $1.20, what is the expected value of 5 identical tickets?
(-2)(0.25)+4(0.75)=-0.5+3=2.5.0.5(1-2)^2 + 0.5(3-2)^2 = 1.5 * 1.20 = $6.00.Use this checkpoint to practice center, spread, and fair-game reasoning.
Use fcmath to compute expected value and variance.
(1.0, 1.0, 1.0)
Compare two games.
(0.0, 25.0)
Use SymPy for exact variance arithmetic.