Unit 3, lesson 17

Expectation, Variance, and Covariance

How expected value, variance, covariance and correlation summarize a probability model, how a covariance matrix collects them, and how a conditional expectation gives the best prediction under squared error.

  • Video 17
  • Expectation and Sampling Uncertainty

A shop planning tomorrow’s stock wants a single number for demand, a sense of how far demand can stray from that number, and a way to use the temperature forecast. For a probability model, expected value, variance and covariance answer these questions, and a conditional expectation updates the prediction once the temperature is known. This lesson defines these quantities with the assumptions they need, collects variances and covariances in a covariance matrix, and keeps quantities of a model apart from averages computed from data. The joint model of temperature and demand used throughout is constructed for illustration.

Key terms

Expected value

Definition Let YY be a discrete random variable with PMF pYp_Y. If ∑y∣y∣ pY(y)\sum_y \lvert y \rvert\, p_Y(y) is finite, the expected value of YY is

E[Y]=∑yy pY(y),E[Y] = \sum_{y} y\, p_Y(y),

with the sum over the support. For a function gg, the expected value E[g(Y)]E[g(Y)] is ∑yg(y) pY(y)\sum_y g(y)\, p_Y(y), provided ∑y∣g(y)∣ pY(y)\sum_y \lvert g(y) \rvert\, p_Y(y) is finite. For an absolutely continuous YY with density ff, the sum becomes an integral,

E[Y]=∫−∞∞y f(y) dy,E[Y] = \int_{-\infty}^{\infty} y\, f(y)\,dy,

provided the same integral with ∣y∣\lvert y \rvert in place of yy is finite. The expected value is a property of the model. The average of nn observed values is the expected value of the empirical distribution, which gives each observation the weight 1/n1/n. It is a different quantity, which can serve as an estimate of E[Y]E[Y] when the observations come from the population the model describes.

In plain words The expected value is a probability-weighted average of the possible values, the balance point of the distribution. It need not be a value the variable can take. When the sum or integral of absolute values is infinite, no finite expected value exists, and a finite average computed from a sample does not show that one exists.

Example A constructed joint model gives the temperature TT in degrees Celsius and the demand YY in units.

ProbabilityY=2Y = 2Y=6Y = 6Total
T=10T = 100.30.30.20.20.50.5
T=20T = 200.10.10.40.40.50.5
Total0.40.40.60.611

The margins give E[T]=10×0.5+20×0.5=15E[T] = 10 \times 0.5 + 20 \times 0.5 = 15 degrees and E[Y]=2×0.4+6×0.6=4.4E[Y] = 2 \times 0.4 + 6 \times 0.6 = 4.4 units, a demand that no single day has. The expected squared demand is E[Y2]=4×0.4+36×0.6=23.2E[Y^2] = 4 \times 0.4 + 36 \times 0.6 = 23.2 square units. For a continuous example, let UU be uniform on [0,2][0, 2] with density 12\tfrac12. Its expected value is the area under y f(y)=y/2y\,f(y) = y/2 from 00 to 22, a triangle with base 22 and height 11, so E[U]=1E[U] = 1.

Linearity of expectation

Definition Let XX and YY be random variables on the same sample space with finite expected values, and let aa, bb and cc be constants. Then

E[aX+bY+c]=a E[X]+b E[Y]+c.\begin{aligned} &E[aX + bY + c] \\[0.4em] &\quad = a\,E[X] + b\,E[Y] + c. \end{aligned}

The rule extends to every finite sum, E[X1+⋯+Xn]=E[X1]+⋯+E[Xn]E[X_1 + \dots + X_n] = E[X_1] + \dots + E[X_n], and it holds whether or not the variables are independent. For a random vector Z\mathbf{Z} whose components have finite expected values, E[Z]E[\mathbf{Z}] is the vector of those expected values, and for a constant vector w\mathbf{w} linearity gives E[w⊤Z]=w⊤E[Z]E[\mathbf{w}^{\top}\mathbf{Z}] = \mathbf{w}^{\top}E[\mathbf{Z}]. The rule covers linear combinations only. For a nonlinear function gg, the value E[g(X)]E[g(X)] need not equal g(E[X])g(E[X]).

In plain words The mean of a sum is the sum of the means, and constant factors and shifts pass straight through. This holds even when the parts are strongly related, which makes the rule easy to apply. The spread of a sum is a different matter, and it does depend on how the parts move together.

Example If demand on each of two days has expected value 4.44.4 units, total demand over the two days has expected value 8.88.8 units, whether the two days are independent or closely linked. The temperature in Fahrenheit, 1.8T+321.8T + 32, has expected value 1.8×15+32=591.8 \times 15 + 32 = 59 degrees Fahrenheit. Squaring does not pass through, since E[Y2]=23.2E[Y^2] = 23.2 while (E[Y])2=4.42=19.36(E[Y])^2 = 4.4^2 = 19.36.

Variance

Definition Let YY have a finite second moment E[Y2]E[Y^2], and write μ=E[Y]\mu = E[Y]. The variance of YY is

Var⁡(Y)=E[(Y−μ)2],\operatorname{Var}(Y) = E\bigl[(Y - \mu)^2\bigr],

and the standard deviation σY\sigma_Y is its nonnegative square root. Expanding the square gives Var⁡(Y)=E[Y2]−μ2\operatorname{Var}(Y) = E[Y^2] - \mu^2. For constants aa and bb, Var⁡(aY+b)=a2Var⁡(Y)\operatorname{Var}(aY + b) = a^2 \operatorname{Var}(Y), and the standard deviation is multiplied by ∣a∣\lvert a \rvert. For observed values y1,…,yny_1, \dots, y_n with average yˉ\bar{y}, the variance of the empirical distribution divides ∑i=1n(yi−yˉ)2\sum_{i=1}^{n} (y_i - \bar{y})^2 by nn. For n≥2n \ge 2 the usual sample variance divides by n−1n - 1 instead,

s2=1n−1∑i=1n(yi−yˉ)2.s^2 = \frac{1}{n - 1} \sum_{i=1}^{n} (y_i - \bar{y})^2.

When the observations are independent and identically distributed with finite variance, the expected value of s2s^2 is exactly Var⁡(Y)\operatorname{Var}(Y). The divisor n−1n - 1 compensates for measuring the deviations from yˉ\bar{y}, which is computed from the same observations, rather than from μ\mu.

In plain words Variance is the expected squared distance from the mean. Squaring keeps deviations above and below the mean from canceling, and it gives the variance squared units, while the standard deviation returns to the original unit. A large variance can come from a change of units rather than from more uncertainty. The divisors nn and n−1n - 1 answer different questions, so a report should say which one it uses.

Example Demand in the joint model deviates from 4.44.4 by −2.4-2.4 with probability 0.40.4 and by 1.61.6 with probability 0.60.6, so

Var⁡(Y)=0.4×5.76+0.6×2.56=3.84\begin{aligned} \operatorname{Var}(Y) &= 0.4 \times 5.76 \\[0.4em] &\quad + 0.6 \times 2.56 \\[0.4em] &= 3.84 \end{aligned}

square units, and the shortcut gives 23.2−19.36=3.8423.2 - 19.36 = 3.84 again. The standard deviation is 3.84≈1.96\sqrt{3.84} \approx 1.96 units. Temperature deviates from 1515 by −5-5 or 55, so Var⁡(T)=25\operatorname{Var}(T) = 25 square degrees and σT=5\sigma_T = 5 degrees. Measuring demand in tenths of a unit multiplies every value by 1010 and the variance by 100100, to 384384. For the three observed values 11, 33 and 55, the squared deviations from yˉ=3\bar{y} = 3 total 88, so the empirical variance is 8/38/3 and the sample variance is 8/2=48/2 = 4.

Covariance and correlation

Definition For random variables XX and YY with finite second moments and means μX\mu_X and μY\mu_Y, the covariance is

Cov⁡(X,Y) =E[(X−μX)(Y−μY)] =E[XY]−μXμY.\begin{aligned} &\operatorname{Cov}(X, Y) \\[0.4em] &\ = E\bigl[(X - \mu_X)(Y - \mu_Y)\bigr] \\[0.4em] &\ = E[XY] - \mu_X \mu_Y. \end{aligned}

It is symmetric in XX and YY, it satisfies Cov⁡(X,X)=Var⁡(X)\operatorname{Cov}(X, X) = \operatorname{Var}(X), and Cov⁡(aX+b,Y)=aCov⁡(X,Y)\operatorname{Cov}(aX + b, Y) = a \operatorname{Cov}(X, Y) for constants aa and bb, so its value depends on the units of both variables. When both standard deviations are positive, the correlation is

ρXY=Cov⁡(X,Y)σX σY.\rho_{XY} = \frac{\operatorname{Cov}(X, Y)}{\sigma_X\, \sigma_Y}.

It has no units, it lies in [−1,1][-1, 1], and it equals 11 or −1-1 exactly when Y=α+βXY = \alpha + \beta X with probability 11 for some constants α\alpha and β≠0\beta \ne 0. If either standard deviation is 00, the correlation is undefined. Independent variables with finite second moments have covariance 00, but covariance 00 does not imply independence.

In plain words Covariance averages the product of the two deviations from the means. It is positive when the products of same-sign deviations, weighted by probability, outweigh those of opposite-sign deviations, and negative in the reverse case. Correlation rescales the covariance to a number between −1-1 and 11 that measures linear association only, so a strong curved relation can have correlation 00. A correlation in data describes an association and does not by itself show that either variable causes the other.

Example In the joint model the centered products (t−15)(y−4.4)(t - 15)(y - 4.4) for the cells (10,2)(10, 2), (10,6)(10, 6), (20,2)(20, 2) and (20,6)(20, 6) are 1212, −8-8, −12-12 and 88. Weighting them by 0.30.3, 0.20.2, 0.10.1 and 0.40.4 gives 3.6−1.6−1.2+3.2=43.6 - 1.6 - 1.2 + 3.2 = 4, in degrees times units of demand. The shortcut agrees, since E[TY]=70E[TY] = 70 and 70−15×4.4=470 - 15 \times 4.4 = 4. The correlation is

ρTY=453.84=16≈0.408.\begin{aligned} \rho_{TY} &= \frac{4}{5\sqrt{3.84}} \\[0.4em] &= \frac{1}{\sqrt{6}} \approx 0.408. \end{aligned}

With the temperature in Fahrenheit the covariance becomes 1.8×4=7.21.8 \times 4 = 7.2, while the correlation stays 0.4080.408. For a dependence that covariance misses, let VV be −1-1, 00 or 11 with probability 13\tfrac13 each. Then Cov⁡(V,V2)=E[V3]−E[V] E[V2]=0\operatorname{Cov}(V, V^2) = E[V^3] - E[V]\,E[V^2] = 0, yet V2V^2 is a function of VV, and P(V=0,V2=0)=13P(V = 0, V^2 = 0) = \tfrac13 differs from P(V=0) P(V2=0)=19P(V = 0)\,P(V^2 = 0) = \tfrac19.

Covariance matrix

Definition Let Z=(Z1,…,Zd)⊤\mathbf{Z} = (Z_1, \dots, Z_d)^{\top} be a random vector whose components have finite second moments, with mean vector μ=E[Z]\boldsymbol{\mu} = E[\mathbf{Z}]. Its covariance matrix is the d×dd \times d matrix

Σ=E[(Z−μ)(Z−μ)⊤],\begin{aligned} \Sigma &= E\bigl[(\mathbf{Z} - \boldsymbol{\mu}) \\[0.4em] &\qquad (\mathbf{Z} - \boldsymbol{\mu})^{\top}\bigr], \end{aligned}

whose entry in row jj and column kk is Cov⁡(Zj,Zk)\operatorname{Cov}(Z_j, Z_k), with the variances on the diagonal. It is symmetric and positive semidefinite, because for every constant vector v\mathbf{v}

v⊤Σ v=Var⁡(v⊤Z)≥0.\mathbf{v}^{\top}\Sigma\,\mathbf{v} = \operatorname{Var}(\mathbf{v}^{\top}\mathbf{Z}) \ge 0.

For data, center the columns of an n×pn \times p design matrix XX by subtracting each column’s average, call the result XcX_c, and let n≥2n \ge 2. The sample covariance matrix Xc⊤Xc/(n−1)X_c^{\top} X_c/(n - 1) is an estimate of the covariance matrix of the feature vector when the rows are independent and identically distributed draws of it.

In plain words A covariance matrix lists every variance and every pairwise covariance of a set of variables in one array. Each entry carries the units of its two variables, so the entries are not interchangeable numbers. Positive semidefiniteness says that no weighted combination of the variables can have negative variance.

Example For temperature and demand, in that order,

Σ=[25443.84].\Sigma = \begin{bmatrix} 25 & 4 \\ 4 & 3.84 \end{bmatrix}.

The weights v=(−0.16,1)⊤\mathbf{v} = (-0.16, 1)^{\top}, with −0.16-0.16 in units of demand per degree, give the combination Y−0.16TY - 0.16T, and

v⊤Σ v=0.0256×25−2×0.16×4+3.84=3.2,\begin{aligned} \mathbf{v}^{\top}\Sigma\,\mathbf{v} &= 0.0256 \times 25 \\[0.4em] &\quad - 2 \times 0.16 \times 4 \\[0.4em] &\quad + 3.84 \\[0.4em] &= 3.2, \end{aligned}

which a direct computation of Var⁡(Y−0.16T)\operatorname{Var}(Y - 0.16T) from the table confirms. The weight 0.16=4/250.16 = 4/25 is Cov⁡(T,Y)/Var⁡(T)\operatorname{Cov}(T, Y)/\operatorname{Var}(T), and it gives the smallest variance of Y−wTY - wT over all numbers ww, a fact that returns with linear regression.

Conditional expectation

Definition Let TT be a discrete random variable, let tt be a value with P(T=t)>0P(T = t) > 0, and let YY be a discrete random variable with a finite expected value. Write p(y∣t)=P(Y=y∣T=t)p(y \mid t) = P(Y = y \mid T = t). The conditional expectation of YY given T=tT = t is the expected value of this conditional distribution,

E[Y∣T=t]=∑yy p(y∣t).\begin{aligned} &E[Y \mid T = t] \\[0.4em] &\quad = \sum_{y} y\, p(y \mid t). \end{aligned}

The law of total expectation says that E[Y]E[Y] is the sum of E[Y∣T=t] P(T=t)E[Y \mid T = t]\,P(T = t) over the support of TT. If YY also has a finite second moment, then for every number aa

E[(Y−a)2∣T=t]=Var⁡(Y∣T=t)+(a−E[Y∣T=t])2,\begin{aligned} &E\bigl[(Y - a)^2 \mid T = t\bigr] \\[0.4em] &\quad = \operatorname{Var}(Y \mid T = t) \\[0.4em] &\qquad + \bigl(a - E[Y \mid T = t]\bigr)^2, \end{aligned}

where Var⁡(Y∣T=t)\operatorname{Var}(Y \mid T = t) is the variance of the conditional distribution. So the conditional mean is the single prediction with the smallest expected squared error given T=tT = t. Read as a function of tt, it is the prediction rule with the smallest expected squared error, which is why regression under squared loss aims at it. When TT is continuous, P(T=t)=0P(T = t) = 0, so conditional probabilities given T=tT = t cannot be defined as ratios, and a more general construction is needed.

In plain words A conditional expectation is an average taken inside a group, with the probabilities rescaled to sum to 11 within that group. Knowing the condition can shift the prediction, and the overall mean is the average of the group means weighted by the group probabilities. A conditional mean is still an average, not a promise about any single case, and other losses lead to other summaries, such as a median for absolute error.

Example Given T=10T = 10, divide the row probabilities 0.30.3 and 0.20.2 by 0.50.5 to get 0.60.6 and 0.40.4, so E[Y∣T=10]=2×0.6+6×0.4=3.6E[Y \mid T = 10] = 2 \times 0.6 + 6 \times 0.4 = 3.6 units. Given T=20T = 20 the rescaled probabilities are 0.20.2 and 0.80.8, and E[Y∣T=20]=5.2E[Y \mid T = 20] = 5.2 units. The overall mean comes back as 0.5×3.6+0.5×5.2=4.40.5 \times 3.6 + 0.5 \times 5.2 = 4.4. Predicting 3.63.6 on cool days and 5.25.2 on warm days gives an expected squared error of 3.23.2 square units, less than the 3.843.84 from predicting 4.44.4 every day.

Check yourself

  1. A variable WW takes the value 11 with probability 34\tfrac34 and the value 55 with probability 14\tfrac14. Find E[W]E[W], Var⁡(W)\operatorname{Var}(W) and the standard deviation, and check the variance with E[W2]E[W^2].
Show the answer

The mean is E[W]=1×34+5×14=2E[W] = 1 \times \tfrac34 + 5 \times \tfrac14 = 2. The deviations from 22 are −1-1 and 33, with squares 11 and 99, so

Var⁡(W)=34×1+14×9=3.\begin{aligned} \operatorname{Var}(W) &= \tfrac34 \times 1 + \tfrac14 \times 9 \\[0.4em] &= 3. \end{aligned}

The standard deviation is 3≈1.732\sqrt{3} \approx 1.732. As a check, E[W2]=1×34+25×14=7E[W^2] = 1 \times \tfrac34 + 25 \times \tfrac14 = 7, and 7−22=37 - 2^2 = 3.

  1. Let XX be equally likely to be −2-2, −1-1, 11 or 22, and let R=X2R = X^2. Show that Cov⁡(X,R)=0\operatorname{Cov}(X, R) = 0. Does it follow that joint probabilities of XX and RR are products of their separate probabilities, as for independent variables?
Show the answer

By symmetry E[X]=0E[X] = 0. The variable RR is 11 or 44 with probability 12\tfrac12 each, so E[R]=2.5E[R] = 2.5, and

E[XR]=E[X3]=−8−1+1+84=0.\begin{aligned} E[XR] &= E[X^3] \\[0.4em] &= \frac{-8 - 1 + 1 + 8}{4} \\[0.4em] &= 0. \end{aligned}

So Cov⁡(X,R)=0−0×2.5=0\operatorname{Cov}(X, R) = 0 - 0 \times 2.5 = 0. It does not follow that they are independent. The event X=1X = 1 and R=4R = 4 is impossible, so its probability is 00, while P(X=1) P(R=4)=14×12=18P(X = 1)\,P(R = 4) = \tfrac14 \times \tfrac12 = \tfrac18. Knowing RR fixes the size of XX exactly. Zero covariance rules out only a linear association, and it does not allow multiplying marginal probabilities.

  1. In the joint model of this lesson, a forecaster must give one number for the demand on a day known to be at 2020 degrees, and is judged by squared error. Compare the expected squared error of predicting the overall mean 4.44.4 with that of predicting E[Y∣T=20]E[Y \mid T = 20].
Show the answer

Given T=20T = 20, demand is 22 with probability 0.20.2 and 66 with probability 0.80.8, so E[Y∣T=20]=5.2E[Y \mid T = 20] = 5.2. Predicting 5.25.2 gives the conditional variance,

0.2×(2−5.2)2+0.8×(6−5.2)2=2.56.\begin{aligned} &0.2 \times (2 - 5.2)^2 \\[0.4em] &\quad + 0.8 \times (6 - 5.2)^2 \\[0.4em] &\quad = 2.56. \end{aligned}

Predicting 4.44.4 gives 0.2×5.76+0.8×2.56=3.20.2 \times 5.76 + 0.8 \times 2.56 = 3.2 square units. The difference 3.2−2.56=0.643.2 - 2.56 = 0.64 equals (5.2−4.4)2(5.2 - 4.4)^2, as the decomposition into conditional variance plus squared distance from the conditional mean predicts. Using the known temperature lowers the expected squared error, because the overall mean averages over cool days that do not apply.

How this lesson connects