⚙️ Setup¶
import numpy as np
rng = np.random.default_rng(351)
print("ready")
ready
from math import comb
pmf = {k: comb(3,k)*0.5**3 for k in range(4)}
for k,p in pmf.items():
print(f"P(X={k}) = C(3,{k})/8 = {p:.3f}")
print(f"sum = {sum(pmf.values()):.3f}")
P(X=0) = C(3,0)/8 = 0.125 P(X=1) = C(3,1)/8 = 0.375 P(X=2) = C(3,2)/8 = 0.375 P(X=3) = C(3,3)/8 = 0.125 sum = 1.000
Answer: X follows a binomial pattern with PMF P(X=0)=1/8, P(X=1)=3/8, P(X=2)=3/8, P(X=3)=1/8. The counts 1,3,3,1 are row 3 of Pascal's triangle, and they sum to 8/8 = 1.
EX = sum(k*p for k,p in pmf.items())
print(f"E[X] = sum k*P(k) = {EX:.3f}")
sim = rng.integers(0,2,size=(100_000,3)).sum(axis=1)
print(f"simulated mean = {sim.mean():.3f} (theory 1.5)")
E[X] = sum k*P(k) = 1.500 simulated mean = 1.503 (theory 1.5)
Answer: E[X] = 0(1/8) + 1(3/8) + 2(3/8) + 3(1/8) = 12/8 = 1.5. With a fair coin the expected number of heads in n flips is just n/2, and the simulation confirms it.
EX = 1.5
EX2 = sum(k**2*p for k,p in pmf.items())
var = EX2 - EX**2
print(f"E[X^2] = {EX2:.3f}")
print(f"Var(X) = {EX2:.3f} - {EX**2:.3f} = {var:.3f}")
print(f"SD(X) = {var**0.5:.3f}")
E[X^2] = 3.000 Var(X) = 3.000 - 2.250 = 0.750 SD(X) = 0.866
Answer: E[X²] = 0 + 1(3/8) + 4(3/8) + 9(1/8) = 24/8 = 3. Var(X) = 3 − 1.5² = 0.75, and SD ≈ 0.866. (This matches the binomial formula npq = 3 × 0.5 × 0.5 = 0.75.)
EX = 3.5 # mean of one die
VarX = sum((k-EX)**2/6 for k in range(1,7))
E_pay = 2*EX + 1
Var_pay = 2**2 * VarX # b shifts the mean but not the variance
print(f"Var(X) for one die = {VarX:.4f}")
print(f"E[2X+1] = 2(3.5)+1 = {E_pay:.1f}")
print(f"Var(2X+1) = 4*Var(X) = {Var_pay:.4f}")
Var(X) for one die = 2.9167 E[2X+1] = 2(3.5)+1 = 8.0 Var(2X+1) = 4*Var(X) = 11.6667
Answer: E[2X+1] = 2·E[X] + 1 = 2(3.5) + 1 = 8 dollars. Variance ignores the shift and squares the scale: Var(2X+1) = 2²·Var(X) = 4 × 2.9167 ≈ 11.67. Adding a constant moves the center; multiplying stretches the spread.
cost = 2
E_winnings = 100*0.01 + 0*0.99
E_net = E_winnings - cost
print(f"E[winnings] = 100(0.01) = {E_winnings:.2f}")
print(f"E[net gain] = {E_winnings:.2f} - {cost} = {E_net:.2f}")
print("Negative expected value: not worth it as a money decision." if E_net<0 else "Positive EV.")
E[winnings] = 100(0.01) = 1.00 E[net gain] = 1.00 - 2 = -1.00 Negative expected value: not worth it as a money decision.
Answer: E[winnings] = 100(0.01) = $1.00, so E[net] = 1.00 − 2.00 = **−$1.00** per ticket. The expected value is negative, so over many tickets you lose about a dollar each on average. Expected value is the standard yardstick for comparing risky choices, the same idea a model uses when it minimizes expected loss.