Week 10 · Expectation, variance, and covariance

Use paired outcomes to explain why the variance of a sum depends on how its parts move together.

Watch on YouTube

Subject contents · Week 9 · Week 11

Use paired outcomes to explain why the variance of a sum depends on how its parts move together.

Predict how two uncertain quantities combine

Suppose a service has two stages and both slow down when the same machine is busy. A total that includes both stages can vary much more than a calculation that treats the stages as unrelated. The average total and the variability of that total are separate questions.

Here you will compute the average, variance, and covariance of paired outcomes and use them to predict the variability of a sum or difference. This is the same calculation used to interpret a covariance matrix in Week 5 and to reason about averages of correlated model predictions.

The idea

Expectation is a probability-weighted average. For a discrete variable X, multiply each possible value by its probability and add the terms. Expectation is linear even when variables are dependent. The expected sum of two variables is the sum of their expectations.

Variance is the expected squared deviation from the mean. Covariance averages the product of two centered variables. When they often move in the same direction, the product tends to be positive. When they often move in opposite directions, it tends to be negative.

Var(X + Y) = Var(X) + Var(Y) + 2 Cov(X, Y)

Var(X − Y) = Var(X) + Var(Y) − 2 Cov(X, Y)

These follow by expanding the square of the centered sum or difference. The pairings matter. If you shuffle one column independently, its variance stays the same but the covariance may change.

Expand the square and keep the cross term

For possible values xᵢ with probabilities pᵢ, the mean of X is μₓ = Σᵢ pᵢ xᵢ. The symbol μ represents a mean, and Σ means add all terms. Variance measures the mean squared distance from that mean, Var(X) = E[(X − μₓ)²]. E means take a probability-weighted average.

Now let a = X − μₓ and b = Y − μᵧ. Since the mean of X + Y is μₓ + μᵧ, its centered value is a + b. Squaring gives a² + 2ab + b². Taking an expectation gives Var(X), twice E[ab], and Var(Y). The middle term E[ab] is the covariance.

Cov(X, Y) = E[(X − μₓ)(Y − μᵧ)].

The difference uses (a − b)² = a² − 2ab + b², so its covariance term has the opposite sign. If X and Y are measured in seconds, their variances and covariance have units of seconds squared. Taking the square root of a variance yields a standard deviation in seconds.

Work it out

Use four equally likely pairs. X = (−1, −1, 1, 1) and Y = (−1, 1, −1, 1). Both have mean zero and variance one. The products XY are (1, −1, −1, 1), so their covariance is zero.

Now set Z = X + Y. Its variance is two. Cov(X, Z) = Cov(X, X + Y) = Var(X) + Cov(X, Y) = 1. If a total contains both X and Z, it contains X twice. The variance is 1 + 2 + 2(1) = 5. If you subtract Z from X, the X terms cancel, leaving −Y with variance one.

Zero covariance alone does not prove independence. If X takes −1, 0, and 1 with equal probability, then X² is completely determined by X even though their covariance is zero.

Variance bars show Var(X)=1, Var(Z)=2, Var(X+Z)=5, and Var(X−Z)=1. The positive covariance adds to the sum and subtracts from the difference.

X and Z share variation. Their covariance adds to the variance of a sum and subtracts from the variance of a difference.

Run the Python lab

import numpy as np

X = np.array([-1., -1., 1., 1.])
Y = np.array([-1., 1., -1., 1.])
Z = X + Y

def covariance(a, b):
    return np.mean((a - a.mean()) * (b - b.mean()))

cov = covariance(X, Z)
print(f"cov(X, Z): {cov:.1f}")
print(f"var(X + Z): {np.var(X + Z):.1f}")
print(f"var(X - Z): {np.var(X - Z):.1f}")

assert np.isclose(np.var(X + Z), np.var(X) + np.var(Z) + 2 * cov)
assert np.isclose(np.var(X - Z), np.var(X) + np.var(Z) - 2 * cov)

rng = np.random.default_rng(5)
rows = rng.integers(0, len(X), size=20_000)
assert abs(np.var(X[rows] + Z[rows]) - 5) < 0.15

Expected output

cov(X, Z): 1.0
var(X + Z): 5.0
var(X - Z): 1.0

Keep the pairs together in the simulation

The lab uses np.var with its default denominator n because these arrays represent all equally likely outcomes in the toy distribution. Its covariance function centers both arrays, multiplies corresponding entries, and averages. Sampling row indices keeps the corresponding X and Z outcomes together. Sampling each column independently would simulate a different joint distribution and change the answer.

Check the second result by direct cancellation: X − Z = X − (X + Y) = −Y. Negating a variable changes its mean’s sign but preserves its variance. You should get the same value from the formula and from the simulated pairs. This is a useful check before interpreting a surprising result in data.

Try it yourself

  1. Replace Z by −X. What happens to the variance of the sum and the difference?
  2. Compute Cov(X, X²) for equally likely values −1, 0, and 1. Why does the result say nothing about whether X determines X²?

Check your work. With Z = −X, the sum is always zero and the difference is 2X with variance four. For the second exercise, E[X] = E[X³] = 0, so covariance is zero even though the variables are dependent.

Further reading

Deep Learning · Applied Math and Machine Learning Basics

Subject contents · Week 9 · Week 11