Expectation#
The expectation (expected value, mean) of a random variable is the probability-weighted average of its possible values:
Discrete:
Continuous:
Expectation is the centre of mass of the distribution, representing the balancing point if you placed probability mass along a number line.
Worked examples:
- Bernoulli: .
- Binomial: (derivable from the sum-of-Bernoullis view).
- Uniform: (the midpoint).
- Exponential: (rate inverse of mean wait time).
LOTUS (Law of the Unconscious Statistician): To find , you do not need the distribution of . You integrate or sum against the distribution of :
Worked example: for a fair die. With for :
Contrast this with ; the expectation of the square is larger than the square of the expectation (Jensen's inequality).
Linearity of expectation#
Linearity is the most powerful shortcut in probability. For any random variables and constants :
No independence required. The expectation of a sum is always the sum of expectations, regardless of whether and are independent, correlated, or even deterministically linked.
Proof sketch (discrete):
Worked example: dependent variables. Let be the roll of a die, and (so is completely determined by ). Then , , and
Indeed always, so . Linearity works despite perfect dependence.
Binomial expectation via linearity: where . Even if the were correlated, (for i.i.d. they happen to be independent, but linearity does not need that).
Variance#
Expectation tells you where a distribution sits; variance tells you how far it spreads. Two variables can share a mean and behave completely differently, which is why a single summary statistic is never enough.
The variance measures spread: how far tends to be from its mean:
The standard deviation is , which has the same units as .
Computational formula (often easier to calculate):
Proof: .
Scaling and shifting:
The constant shift adds no variance. Scaling by scales variance by (since variance is in squared units).
Worked examples:
- Bernoulli: . Maximum at .
- Binomial: .
- Uniform: .
- Exponential: .
Worked computation: fair die. . From earlier, . So
Standard deviation .
Covariance and correlation#
Covariance measures how two variables move together:
Computational form: .
- Positive covariance: above its mean tends to happen with above its mean.
- Negative covariance: opposite directions.
- Zero covariance: no linear relationship detected.
Properties:
- .
- (symmetric).
- .
- .
The last formula shows why the variance of a sum is not always the sum of variances: the covariance term can add or subtract.
Correlation standardises covariance to the range :
- : perfect positive linear relationship.
- : perfect negative linear relationship.
- : uncorrelated.
Critical warning: uncorrelated independent. Independence implies zero correlation, but zero correlation does not imply independence. For example, let and . Then . They are uncorrelated but perfectly dependent: knowing determines exactly.
Correlation only captures linear dependence. Non-linear relationships (quadratic, periodic) can have zero correlation but strong dependence.
Worked example: joint table. Suppose and have the following joint PMF:
| 0.2 | 0.3 | |
| 0.1 | 0.4 |
Then , , .
Small positive covariance: slightly increases the chance of .
Conditional expectation intro#
The conditional expectation is the average of over the subset of outcomes where . It is a function of : for each possible value of , we get a (potentially different) expected value of .
Worked example: from the table above:
When , averages lower; when , averages higher, consistent with the positive covariance we found.
Law of total expectation: : the overall mean is the weighted average of conditional means. Week 4 deepens this.
Let X take values 1, 2, 3 with probabilities 0.2, 0.5, 0.3. Compute E[X] as a decimal.
Knowledge check#
Linearity of expectation holds only when the random variables are independent. True or false?
Browser lab#
Sample pairs from a known joint distribution, compute empirical mean/variance/covariance, and compare to formulas.
import numpy as np
import matplotlib.pyplot as plt
rng = np.random.default_rng(42)
n = 5000
joint = np.array([[0.1, 0.2, 0.1],
[0.05, 0.3, 0.05],
[0.0, 0.1, 0.1]])
x_vals = np.array([0, 1, 2])
y_vals = np.array([0, 1, 2])
flat = joint.ravel() / joint.sum()
indices = rng.choice(len(flat), size=n, p=flat)
xi = indices // 3
yi = indices % 3
xs = x_vals[xi]
ys = y_vals[yi]
E_x = np.sum(x_vals * joint.sum(axis=1))
E_y = np.sum(y_vals * joint.sum(axis=0))
E_xy = np.sum(np.outer(x_vals, y_vals) * joint)
Cov_xy = E_xy - E_x * E_y
E_x_emp = xs.mean()
E_y_emp = ys.mean()
Cov_emp = np.mean((xs - E_x_emp) * (ys - E_y_emp))
print(f"E[X] true={E_x:.4f} emp={E_x_emp:.4f}")
print(f"E[Y] true={E_y:.4f} emp={E_y_emp:.4f}")
print(f"Cov true={Cov_xy:.4f} emp={Cov_emp:.4f}")
px = joint.sum(axis=1)
py = joint.sum(axis=0)
fig, axes = plt.subplots(1, 3, figsize=(13, 4))
axes[0].bar(x_vals, px, color="tab:blue", alpha=0.7)
axes[0].set_title("Marginal P(X)")
axes[0].set_xlabel("x")
axes[1].bar(y_vals, py, color="tab:orange", alpha=0.7)
axes[1].set_title("Marginal P(Y)")
axes[1].set_xlabel("y")
axes[2].scatter(xs[:500], ys[:500], alpha=0.3, s=8)
axes[2].set_title("Joint samples (first 500)")
axes[2].set_xlabel("x"); axes[2].set_ylabel("y")
plt.tight_layout()
plt.show()
print("Notice: sample E[X], E[Y] and Cov match the exact table (0.7922/0.8000, 1.0956/1.1000, 0.1205/0.1200) — the joint sample carries the dependence.")
Key takeaways#
- Expectation is the probability-weighted average, representing the centre of mass. LOTUS computes directly against the distribution of , with no need for the distribution of .
- Linearity of expectation holds for any random variables, dependent or not. That is why it is the most powerful shortcut in probability.
- Variance measures spread, with the shift-and-scale rule and the computational form .
- Covariance measures linear co-movement; correlation standardises it to .
- Uncorrelated does not imply independent. With symmetric and , the variables are perfectly dependent yet have zero covariance.
- : the covariance term is why variances do not simply add.
Further reading#
- Joseph K. Blitzstein & Jessica Hwang, Introduction to Probability (2nd ed., CRC Press, 2019): expectation, variance, and covariance with strong intuition for linearity.
- George Casella & Roger L. Berger, Statistical Inference (2nd ed., Duxbury, 2002): Chapter 4 develops moments systematically.
- Larry Wasserman, All of Statistics (Springer, 2004): the expectation and variance sections are concise and applied.
Reading is free. PDF and ePub unlock with the newsletter. Join once and every lesson opens in this browser.