Lesson 5.3.1 found E[g(X)] by weighting each value g(x) by its probability. With two variables, weight each g(x,y) by the joint probability of its cell (lesson 5.5.1):
The sum for a joint pmf, the double integral for a joint density. No distribution of g(X,Y) is needed.
Take lesson 5.5.1's three coins: X counts heads in tosses 1 and 2, Y in tosses 2 and 3. Only the cells where both are positive add to E[XY]. The cell (1,1) has xy=1 and two outcomes; (1,2), (2,1) and (2,2) have xy=2, 2 and 4 and one outcome each:
E[XY]=81(1⋅2+2+2+4)=810=45
With g(x,y)=x+y the same sum gives E[X+Y]=2=E[X]+E[Y], the linearity of lesson 5.3.2. Products behave differently: E[XY]=1.25, but E[X]E[Y]=1.
For independentX and Y the product does split, E[XY]=E[X]E[Y], because the joint is the product of the marginals and the double sum factors. More generally E[g(X)h(Y)]=E[g(X)]E[h(Y)].
Covariance
Write μX=E[X] and μY=E[Y] for the two means.
When X and Y tend to be above their means together and below them together, the product (X−μX)(Y−μY) is mostly positive. When one tends to be high while the other is low, it is mostly negative. Its mean measures which pattern wins.
Rule: Covariance
Cov(X,Y)=E[(X−μX)(Y−μY)]=E[XY]−μXμY
Positive: the two move together; negative: they move oppositely. Its units are X's units times Y's. Cov(X,X)=Var(X), and Cov(Y,X)=Cov(X,Y).
The coins. Here μX=μY=1. The product (x−1)(y−1) is 1 in the cells (0,0) and (2,2), each with probability 81.
In the cells (0,2) and (2,0), where it would be −1, the probability is 0, and every other cell gives 0. So Cov(X,Y)=82=41, and the shortcut agrees: 45−1⋅1=41.
A density. For lesson 5.5.1's fX,Y(x,y)=x+y on the unit square, double integrals give μX=μY=127 and E[XY]=31. So Cov(X,Y)=31−14449=−1441, about −0.00694. It is slightly negative, which matches lesson 5.5.1's finding: the conditional mean of Y fell from 0.6111 at x=0.25 to 0.5667 at x=0.75.
Each dot adds the product of its two deviations from the means: positive in the shaded quarters, negative in the others. Covariance is their average. In the second picture about half the dots are shaded and the products cancel, yet Y depends completely on X.
Try it
E(g) adds g(x,y) times each cell's probability, exactly as the rule says. The cell builds the coin table, then prints E[XY], the covariance both ways and E[X+Y].
The last lines simulate 100,000 tosses with a fixed seed. np.cov gives a sample covariance of 0.2502 in this run, against the exact 0.25.
⌘/Ctrl + Enter runs the cell
Correlation
A covariance depends on units: measure X in cents instead of dollars and it grows 100 times. Dividing by both standard deviations, σX=Var(X) and σY, removes the units.
Rule: Correlation
ρ=σXσYCov(X,Y),−1≤ρ≤1Cov(X,Y)=ρσXσY
ρ (rho) has no units. It is 1 or −1 exactly when Y=a+bX for some numbers a and b=0, with the sign of b. It is undefined when either variable is constant.
For the coins, Var(X)=Var(Y)=21 (two tosses, lesson 5.3.2), so ρ=1/21/4=0.5. For the density x+y, Var(X)=125−14449=14411, so ρ=11/144−1/144=−111≈−0.0909: a weak link.
Case study 3.1.4 met ρ as the cosine of an angle between centred return vectors, which is why it lies between −1 and 1. Case study 3.2.5 took the covariance of two assets to be ρijσiσj. That is the second line of the rule: covariance defined from probability, then written with the correlation.
Try it
The first line computes the density example's moments with dblquad (lesson 4.7.1), whose function takes the inner variable first. Then ρ for the coins and for the density.
The last lines draw from the density x+y as lesson 5.5.1 did, with the same seed. In this run the sample covariance is −0.0069 and the sample correlation −0.0905.
⌘/Ctrl + Enter runs the cell
Scaling changes covariance but not correlation. Cov(aX+b,cY+d)=acCov(X,Y), while the correlation of aX+b and cY+d is ρ when ac>0 and −ρ when ac<0. Adding a constant changes neither.
Variances of sums
Covariance obeys the same algebra as multiplying out brackets: it is bilinear. Constants come out of each slot, sums split, and a constant variable has covariance 0 with anything:
Cov(aX+bY,W)=aCov(X,W)+bCov(Y,W)
Since Var(X+Y)=Cov(X+Y,X+Y), multiplying out gives four terms: Var(X), Var(Y) and Cov(X,Y) twice.
Read ± as + for X+Y and − for X−Y. Variances never subtract: Var(−Y)=Var(Y), so only the covariance term carries the sign. With n variables, Var(X1+⋯+Xn) is the sum of all n variances plus twice every covariance Cov(Xi,Xj) with i<j.
Check it on the coins, two ways.
From the table.X+Y takes the values 0 to 4 with probabilities 81,41,41,41,81, so its variance is 23. The rule says 21+21+2⋅41=23. For X−Y it says 21+21−21=21, and the table agrees.
By algebra. Write the coins as C1, C2 and C3, each 1 for heads and 0 for tails, independent, each with variance 41. Then X=C1+C2 and Y=C2+C3.
By bilinearity, Cov(X,Y) splits into four covariances, and only Cov(C2,C2)=41 is not 0. The shared coin is the whole covariance.
Then X+Y=C1+2C2+C3 has variance 41+4⋅41+41=23, and X−Y=C1−C3 has variance 21: the shared coin cancels.
Lesson 5.3.2's top face plus bottom face of one die is the opposite case. The bottom face is 7−X, so Cov(X,7−X)=−Var(X), and the rule gives Var(X)+Var(X)−2Var(X)=0: the total is always 7.
With every covariance 0, the variances simply add. That is how lesson 5.3.2 found the binomial variance np(1−p), a sum of n independent Bernoulli variables. Case study 5.5.5 applies the rule with many variables to a portfolio, and to returns added over time.
Try it
The first lines find the distribution of X+Y from the coin table and its variance. Then Var(X−Y) from the table, and both rules.
The last line uses the seeded simulation: sample variances of X+Y and X−Y, 1.5018 and 0.5011.
⌘/Ctrl + Enter runs the cell
Independent means uncorrelated, not the reverse
If X and Y are independent, E[XY]=E[X]E[Y], so Cov(X,Y)=0 and the variances add. The reverse fails, and case study 3.1.4 already had an example.
Its four months of returns were (1,2,3,4) and (4,2,2,4), in percent. As random variables: let X be 1, 2, 3 or 4, each with probability 41. Let Y=4 when X is 1 or 4, and Y=2 when X is 2 or 3.
Then E[XY]=44+4+6+16=7.5 and E[X]E[Y]=2.5×3=7.5, so the covariance is 0.
Yet Y is fixed by X. P(Y=2)=21, but P(Y=2∣X=1)=0, so they are dependent (lesson 5.5.1).
The same happens with X standard normal and Y=X2, the second picture above. Large values of Y come with large ∣X∣ of either sign, and the positive and negative products cancel.
Rule: Independence and correlation
independent⟹Cov(X,Y)=0Cov(X,Y)=0⟹independent
Covariance measures only how closely Y follows a straight line in X. A curved link, or one that changes sign, can have covariance 0. Lesson 5.5.3 shows that for jointly normal variables zero correlation does mean independence.
Try it
The four values give E[XY]−E[X]E[Y]=0.
Then 100,000 standard normal draws with a fixed seed. In this run the sample correlation of X and X2 is 0.0029, near 0. The correlation of ∣X∣ and X2 is 0.9373: the link is there, just not a straight line through both signs.
⌘/Ctrl + Enter runs the cell
Sample covariance, standard deviation and correlation
With data instead of a distribution, each expectation becomes an average over the sample. For n pairs (xt,yt), write xˉ and yˉ for the sample means. The usual estimates divide the sums by n−1 rather than n; lesson 5.8.1 explains why.
sx2 is the sample variance and its square root sx the sample standard deviation; sxy is the sample covariance and r the sample correlation. The factors n−1 cancel in r.
Case study 3.1.4's ten months of returns for stocks X and Y give, in decimals:
value
as in case study 3.1.4
sxy
0.000679
x~⋅y~ divided by 9
sx
0.050284
∥x~∥ divided by 3
sy
0.022088
∥y~∥ divided by 3
r
0.6113
the cosine of 52.32°
These are the sample standard deviation and covariance that case study 3.1.4 promised to define. The sample correlation is the cosine it computed.
In NumPy, np.cov(x, y) and np.corrcoef(x, y) give the sample covariance matrix and correlation. np.var and np.std divide by n unless you pass ddof=1: for X's returns, 0.002276 instead of 0.002528.
Try it
The cell rebuilds case study 3.1.4's ten months from its seed. It computes sxy, sx, sy and r by the formulas, then with NumPy.
The last line shows the two divisors of np.var, and that percent units multiply the covariance by 10,000 but leave r alone.
⌘/Ctrl + Enter runs the cell
The covariance matrix
Several random variables X1,…,Xn form a random vectorX, a column written in bold capitals. Its mean is the vector of means, μ=E[X], taken entry by entry.
The product (X−μ)(X−μ)⊤ is an n×n matrix whose entry (i,j) is (Xi−μi)(Xj−μj). Its expectation, entry by entry, holds every covariance.
Rule: Covariance matrix
Σ=E[(X−μ)(X−μ)⊤]Σij=Cov(Xi,Xj)=ρijσiσj
The diagonal holds the variances σi2; off the diagonal are the covariances. Σ is symmetric, because Cov(Xi,Xj)=Cov(Xj,Xi).
For the coins, with X=(X,Y):
Σ=(1/21/41/41/2)
Case study 3.2.5 built the three assets' Σ as DRD from volatilities and correlations. By the rule above, that is this matrix for their random returns.
Lesson 3.7.1 showed that a covariance matrix is positive semidefinite. np.cov(data, rowvar=False) is the sample version S (case study 3.2.5), with one row per observation.
Try it
The cell adds p⋅(x−μ)(x−μ)⊤ over the coin table's cells, which is the expectation in the rule. np.outer(d, d) is the matrix dd⊤.
Then the sample covariance matrix of the seeded simulation, close to it.
⌘/Ctrl + Enter runs the cell
Lesson 5.5.3 builds random vectors with a chosen Σ, and case study 5.5.5 uses these rules to derive the variance of a portfolio.
Common mistakes
Variances add only without covariance. For the coins Var(X+Y)=23, not 21+21=1.
Variances never subtract.Var(X−Y)=Var(X)+Var(Y)−2Cov(X,Y), here 21, not 21−21=0.
Constants come out squared.Var(2X)=4Var(X), while Cov(2X,Y)=2Cov(X,Y).
Zero covariance is not independence.X and X2 have covariance 0 when X is standard normal.
Compare correlations, not covariances. Percent units multiply a covariance by 10,000 and leave the correlation alone.
Check the divisor.np.var and np.std divide by n by default; np.cov divides by n−1.
Covariance, correlation and independence
0 of 12 correct
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
How to type answers
Powers with ^: x^7, 2^(1/3), a^-2.
Multiply with * or by writing side by side: 3x^2, 2(x+1).
ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
Your first answer to each question is saved: it counts towards your score and brings the question back for review.
Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.
Summary card
E[g(X,Y)]=∑∑gpX,Y or ∬gfX,YdA. For independent variables, E[XY]=E[X]E[Y].
Cov(X,Y)=E[(X−μX)(Y−μY)]=E[XY]−μXμY, in X's units times Y's. ρ=σXσYCov lies in [−1,1], and ±1 means an exact line.
Covariance is bilinear: Var(aX+bY)=a2Var(X)+b2Var(Y)+2abCov(X,Y).
Independent implies uncorrelated, so variances add; uncorrelated does not imply independent (X and X2).
Samples: sx2, sxy divide by n−1, and r=sxsysxy; np.cov, np.corrcoef, and ddof=1 for np.var and np.std.
The covariance matrix Σ=E[(X−μ)(X−μ)⊤] has Σij=Cov(Xi,Xj)=ρijσiσj.