SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 5.5.1 Joint, marginal and conditional distributionsConcept · 30 min
      2. 5.5.2 Covariance, correlation and independenceConcept · 35 min
      3. 5.5.3 The multivariate normal distributionConcept · 35 min
      4. 5.5.4 Practice set: joint distributionsPractice set · 25 min
      5. 5.5.5 Case study: portfolio risk and conditional expectationCase study · 35 min

Level 5, Probability and statistics 5.5 Joint distributions Lesson 2 of 5

ConceptLesson 5.5.2 in Level 5, Probability and statistics. About 35 minutes.

Covariance, correlation and independence

The mean of a function of two variables

Lesson 5.3.1 found E[g(X)]\mathbb{E}[g(X)] by weighting each value g(x)g(x) by its probability. With two variables, weight each g(x,y)g(x, y) by the joint probability of its cell (lesson 5.5.1):

Rule: Expectation of g(X, Y)

E[g(X,Y)]=∑x∑yg(x,y) pX,Y(x,y)=∬g(x,y) fX,Y(x,y) dA\begin{gathered} \mathbb{E}[g(X, Y)] \\ = \textstyle\sum_x\sum_y g(x, y)\,p_{X,Y}(x, y) \\[4pt] = \textstyle\iint g(x, y)\,f_{X,Y}(x, y)\,dA \end{gathered}

The sum for a joint pmf, the double integral for a joint density. No distribution of g(X,Y)g(X, Y) is needed.

Take lesson 5.5.1's three coins: XX counts heads in tosses 1 and 2, YY in tosses 2 and 3. Only the cells where both are positive add to E[XY]\mathbb{E}[XY]. The cell (1,1)(1, 1) has xy=1xy = 1 and two outcomes; (1,2)(1, 2), (2,1)(2, 1) and (2,2)(2, 2) have xy=2xy = 2, 2 and 4 and one outcome each:

E[XY]=18(1⋅2+2+2+4)=108=54\begin{aligned} \mathbb{E}[XY] &= \tfrac{1}{8}(1 \cdot 2 + 2 + 2 + 4) \\ &= \tfrac{10}{8} = \tfrac{5}{4} \end{aligned}

With g(x,y)=x+yg(x, y) = x + y the same sum gives E[X+Y]=2=E[X]+E[Y]\mathbb{E}[X + Y] = 2 = \mathbb{E}[X] + \mathbb{E}[Y], the linearity of lesson 5.3.2. Products behave differently: E[XY]=1.25\mathbb{E}[XY] = 1.25, but E[X] E[Y]=1\mathbb{E}[X]\,\mathbb{E}[Y] = 1.

For independent XX and YY the product does split, E[XY]=E[X] E[Y]\mathbb{E}[XY] = \mathbb{E}[X]\,\mathbb{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)]\mathbb{E}[g(X)\,h(Y)] = \mathbb{E}[g(X)]\,\mathbb{E}[h(Y)].

Covariance

Write μX=E[X]\mu_X = \mathbb{E}[X] and μY=E[Y]\mu_Y = \mathbb{E}[Y] for the two means.

When XX and YY tend to be above their means together and below them together, the product (X−μX)(Y−μY)(X - \mu_X)(Y - \mu_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\begin{gathered} \operatorname{Cov}(X, Y) \\ = \mathbb{E}\big[(X - \mu_X)(Y - \mu_Y)\big] \\[4pt] = \mathbb{E}[XY] - \mu_X\,\mu_Y \end{gathered}

Positive: the two move together; negative: they move oppositely. Its units are XX's units times YY's. Cov⁡(X,X)=Var⁡(X)\operatorname{Cov}(X, X) = \operatorname{Var}(X), and Cov⁡(Y,X)=Cov⁡(X,Y)\operatorname{Cov}(Y, X) = \operatorname{Cov}(X, Y).

The coins. Here μX=μY=1\mu_X = \mu_Y = 1. The product (x−1)(y−1)(x - 1)(y - 1) is 1 in the cells (0,0)(0, 0) and (2,2)(2, 2), each with probability 18\frac{1}{8}.

In the cells (0,2)(0, 2) and (2,0)(2, 0), where it would be −1-1, the probability is 0, and every other cell gives 0. So Cov⁡(X,Y)=28=14\operatorname{Cov}(X, Y) = \frac{2}{8} = \frac{1}{4}, and the shortcut agrees: 54−1⋅1=14\frac{5}{4} - 1 \cdot 1 = \frac{1}{4}.

A density. For lesson 5.5.1's fX,Y(x,y)=x+yf_{X,Y}(x, y) = x + y on the unit square, double integrals give μX=μY=712\mu_X = \mu_Y = \frac{7}{12} and E[XY]=13\mathbb{E}[XY] = \frac{1}{3}. So Cov⁡(X,Y)=13−49144=−1144\operatorname{Cov}(X, Y) = \frac{1}{3} - \frac{49}{144} = -\frac{1}{144}, about −0.00694-0.00694. It is slightly negative, which matches lesson 5.5.1's finding: the conditional mean of YY fell from 0.6111 at x=0.25x = 0.25 to 0.5667 at x=0.75x = 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 YY depends completely on XX.

Try it

E(g) adds g(x,y)g(x, y) times each cell's probability, exactly as the rule says. The cell builds the coin table, then prints E[XY]\mathbb{E}[XY], the covariance both ways and E[X+Y]\mathbb{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 XX in cents instead of dollars and it grows 100 times. Dividing by both standard deviations, σX=Var⁡(X)\sigma_X = \sqrt{\operatorname{Var}(X)} and σY\sigma_Y, removes the units.

Rule: Correlation

ρ=Cov⁡(X,Y)σX σY,−1≤ρ≤1Cov⁡(X,Y)=ρ σX σY\begin{gathered} \rho = \frac{\operatorname{Cov}(X, Y)}{\sigma_X\,\sigma_Y}, \quad -1 \le \rho \le 1 \\[4pt] \operatorname{Cov}(X, Y) = \rho\,\sigma_X\,\sigma_Y \end{gathered}

ρ\rho (rho) has no units. It is 1 or −1-1 exactly when Y=a+bXY = a + bX for some numbers aa and b≠0b \neq 0, with the sign of bb. It is undefined when either variable is constant.

For the coins, Var⁡(X)=Var⁡(Y)=12\operatorname{Var}(X) = \operatorname{Var}(Y) = \frac{1}{2} (two tosses, lesson 5.3.2), so ρ=1/41/2=0.5\rho = \frac{1/4}{1/2} = 0.5. For the density x+yx + y, Var⁡(X)=512−49144=11144\operatorname{Var}(X) = \frac{5}{12} - \frac{49}{144} = \frac{11}{144}, so ρ=−1/14411/144=−111≈−0.0909\rho = \frac{-1/144}{11/144} = -\frac{1}{11} \approx -0.0909: a weak link.

Case study 3.1.4 met ρ\rho as the cosine of an angle between centred return vectors, which is why it lies between −1-1 and 1. Case study 3.2.5 took the covariance of two assets to be ρijσiσj\rho_{ij}\sigma_i\sigma_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 ρ\rho for the coins and for the density.

The last lines draw from the density x+yx + y as lesson 5.5.1 did, with the same seed. In this run the sample covariance is −0.0069-0.0069 and the sample correlation −0.0905-0.0905.

⌘/Ctrl + Enter runs the cell

Scaling changes covariance but not correlation. Cov⁡(aX+b, cY+d)=ac Cov⁡(X,Y)\operatorname{Cov}(aX + b,\ cY + d) = ac\,\operatorname{Cov}(X, Y), while the correlation of aX+baX + b and cY+dcY + d is ρ\rho when ac>0ac > 0 and −ρ-\rho when ac<0ac < 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)\begin{gathered} \operatorname{Cov}(aX + bY,\ W) \\ = a\operatorname{Cov}(X, W) + b\operatorname{Cov}(Y, W) \end{gathered}

Since Var⁡(X+Y)=Cov⁡(X+Y, X+Y)\operatorname{Var}(X + Y) = \operatorname{Cov}(X + Y,\ X + Y), multiplying out gives four terms: Var⁡(X)\operatorname{Var}(X), Var⁡(Y)\operatorname{Var}(Y) and Cov⁡(X,Y)\operatorname{Cov}(X, Y) twice.

Rule: Variance of a sum

Var⁡(X±Y)=Var⁡(X)+Var⁡(Y)±2Cov⁡(X,Y)Var⁡(aX+bY)=a2Var⁡(X)+b2Var⁡(Y)+2abCov⁡(X,Y)\begin{gathered} \operatorname{Var}(X \pm Y) = \operatorname{Var}(X) \\ + \operatorname{Var}(Y) \pm 2\operatorname{Cov}(X, Y) \\[8pt] \operatorname{Var}(aX + bY) = a^2\operatorname{Var}(X) \\ + b^2\operatorname{Var}(Y) + 2ab\operatorname{Cov}(X, Y) \end{gathered}

Read ±\pm as ++ for X+YX + Y and −- for X−YX - Y. Variances never subtract: Var⁡(−Y)=Var⁡(Y)\operatorname{Var}(-Y) = \operatorname{Var}(Y), so only the covariance term carries the sign. With nn variables, Var⁡(X1+⋯+Xn)\operatorname{Var}(X_1 + \cdots + X_n) is the sum of all nn variances plus twice every covariance Cov⁡(Xi,Xj)\operatorname{Cov}(X_i, X_j) with i<ji < j.

Check it on the coins, two ways.

From the table. X+YX + Y takes the values 0 to 4 with probabilities 18,14,14,14,18\frac{1}{8}, \frac{1}{4}, \frac{1}{4}, \frac{1}{4}, \frac{1}{8}, so its variance is 32\frac{3}{2}. The rule says 12+12+2⋅14=32\frac{1}{2} + \frac{1}{2} + 2 \cdot \frac{1}{4} = \frac{3}{2}. For X−YX - Y it says 12+12−12=12\frac{1}{2} + \frac{1}{2} - \frac{1}{2} = \frac{1}{2}, and the table agrees.

By algebra. Write the coins as C1C_1, C2C_2 and C3C_3, each 1 for heads and 0 for tails, independent, each with variance 14\frac{1}{4}. Then X=C1+C2X = C_1 + C_2 and Y=C2+C3Y = C_2 + C_3.

By bilinearity, Cov⁡(X,Y)\operatorname{Cov}(X, Y) splits into four covariances, and only Cov⁡(C2,C2)=14\operatorname{Cov}(C_2, C_2) = \frac{1}{4} is not 0. The shared coin is the whole covariance.

Then X+Y=C1+2C2+C3X + Y = C_1 + 2C_2 + C_3 has variance 14+4⋅14+14=32\frac{1}{4} + 4 \cdot \frac{1}{4} + \frac{1}{4} = \frac{3}{2}, and X−Y=C1−C3X - Y = C_1 - C_3 has variance 12\frac{1}{2}: 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−X7 - X, so Cov⁡(X,7−X)=−Var⁡(X)\operatorname{Cov}(X, 7 - X) = -\operatorname{Var}(X), and the rule gives Var⁡(X)+Var⁡(X)−2Var⁡(X)=0\operatorname{Var}(X) + \operatorname{Var}(X) - 2\operatorname{Var}(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)np(1 - p), a sum of nn 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+YX + Y from the coin table and its variance. Then Var⁡(X−Y)\operatorname{Var}(X - Y) from the table, and both rules.

The last line uses the seeded simulation: sample variances of X+YX + Y and X−YX - Y, 1.5018 and 0.5011.

⌘/Ctrl + Enter runs the cell

Independent means uncorrelated, not the reverse

If XX and YY are independent, E[XY]=E[X] E[Y]\mathbb{E}[XY] = \mathbb{E}[X]\,\mathbb{E}[Y], so Cov⁡(X,Y)=0\operatorname{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)(1, 2, 3, 4) and (4,2,2,4)(4, 2, 2, 4), in percent. As random variables: let XX be 1, 2, 3 or 4, each with probability 14\frac{1}{4}. Let Y=4Y = 4 when XX is 1 or 4, and Y=2Y = 2 when XX is 2 or 3.

Then E[XY]=4+4+6+164=7.5\mathbb{E}[XY] = \frac{4 + 4 + 6 + 16}{4} = 7.5 and E[X] E[Y]=2.5×3=7.5\mathbb{E}[X]\,\mathbb{E}[Y] = 2.5 \times 3 = 7.5, so the covariance is 0.

Yet YY is fixed by XX. P(Y=2)=12P(Y = 2) = \frac{1}{2}, but P(Y=2∣X=1)=0P(Y = 2 \mid X = 1) = 0, so they are dependent (lesson 5.5.1).

The same happens with XX standard normal and Y=X2Y = X^2, the second picture above. Large values of YY come with large ∣X∣\lvert X \rvert of either sign, and the positive and negative products cancel.

Rule: Independence and correlation

independent  ⟹  Cov⁡(X,Y)=0Cov⁡(X,Y)=0⟹̸independent\begin{gathered} \text{independent} \\ \implies \operatorname{Cov}(X, Y) = 0 \\[6pt] \operatorname{Cov}(X, Y) = 0 \\ \not\Longrightarrow \text{independent} \end{gathered}

Covariance measures only how closely YY follows a straight line in XX. 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\mathbb{E}[XY] - \mathbb{E}[X]\,\mathbb{E}[Y] = 0.

Then 100,000 standard normal draws with a fixed seed. In this run the sample correlation of XX and X2X^2 is 0.0029, near 0. The correlation of ∣X∣\lvert X \rvert and X2X^2 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 nn pairs (xt,yt)(x_t, y_t), write xˉ\bar{x} and yˉ\bar{y} for the sample means. The usual estimates divide the sums by n−1n - 1 rather than nn; lesson 5.8.1 explains why.

Rule: Sample versions

sx2=1n−1∑t(xt−xˉ)2sxy=1n−1∑t(xt−xˉ)(yt−yˉ)r=sxysx sy\begin{gathered} s_x^2 = \frac{1}{n - 1}\textstyle\sum_t (x_t - \bar{x})^2 \\[4pt] s_{xy} = \frac{1}{n - 1}\textstyle\sum_t (x_t - \bar{x})(y_t - \bar{y}) \\[4pt] r = \frac{s_{xy}}{s_x\,s_y} \end{gathered}

sx2s_x^2 is the sample variance and its square root sxs_x the sample standard deviation; sxys_{xy} is the sample covariance and rr the sample correlation. The factors n−1n - 1 cancel in rr.

Case study 3.1.4's ten months of returns for stocks X and Y give, in decimals:

valueas in case study 3.1.4
sxys_{xy}0.000679x~⋅y~\tilde{\mathbf{x}} \cdot \tilde{\mathbf{y}} divided by 9
sxs_x0.050284∥x~∥\lVert \tilde{\mathbf{x}} \rVert divided by 3
sys_y0.022088∥y~∥\lVert \tilde{\mathbf{y}} \rVert divided by 3
rr0.6113the 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 nn 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 sxys_{xy}, sxs_x, sys_y and rr 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 rr alone.

⌘/Ctrl + Enter runs the cell

The covariance matrix

Several random variables X1,…,XnX_1, \ldots, X_n form a random vector X\mathbf{X}, a column written in bold capitals. Its mean is the vector of means, μ=E[X]\boldsymbol{\mu} = \mathbb{E}[\mathbf{X}], taken entry by entry.

The product (X−μ)(X−μ)⊤(\mathbf{X} - \boldsymbol{\mu})(\mathbf{X} - \boldsymbol{\mu})^\top is an n×nn \times n matrix whose entry (i,j)(i, j) is (Xi−μi)(Xj−μj)(X_i - \mu_i)(X_j - \mu_j). Its expectation, entry by entry, holds every covariance.

Rule: Covariance matrix

Σ=E[(X−μ)(X−μ)⊤]Σij=Cov⁡(Xi,Xj)=ρij σi σj\begin{gathered} \Sigma = \mathbb{E}\big[(\mathbf{X} - \boldsymbol{\mu})(\mathbf{X} - \boldsymbol{\mu})^\top\big] \\[4pt] \Sigma_{ij} = \operatorname{Cov}(X_i, X_j) = \rho_{ij}\,\sigma_i\,\sigma_j \end{gathered}

The diagonal holds the variances σi2\sigma_i^2; off the diagonal are the covariances. Σ\Sigma is symmetric, because Cov⁡(Xi,Xj)=Cov⁡(Xj,Xi)\operatorname{Cov}(X_i, X_j) = \operatorname{Cov}(X_j, X_i).

For the coins, with X=(X,Y)\mathbf{X} = (X, Y):

Σ=(1/21/41/41/2)\Sigma = \begin{pmatrix} 1/2 & 1/4 \\ 1/4 & 1/2 \end{pmatrix}

Case study 3.2.5 built the three assets' Σ\Sigma as DRDDRD 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 SS (case study 3.2.5), with one row per observation.

Try it

The cell adds p⋅(x−μ)(x−μ)⊤p \cdot (\mathbf{x} - \boldsymbol{\mu})(\mathbf{x} - \boldsymbol{\mu})^\top over the coin table's cells, which is the expectation in the rule. np.outer(d, d) is the matrix dd⊤\mathbf{d}\mathbf{d}^\top.

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 Σ\Sigma, and case study 5.5.5 uses these rules to derive the variance of a portfolio.

Common mistakes

  1. Variances add only without covariance. For the coins Var⁡(X+Y)=32\operatorname{Var}(X + Y) = \frac{3}{2}, not 12+12=1\frac{1}{2} + \frac{1}{2} = 1.
  2. Variances never subtract. Var⁡(X−Y)=Var⁡(X)+Var⁡(Y)−2Cov⁡(X,Y)\operatorname{Var}(X - Y) = \operatorname{Var}(X) + \operatorname{Var}(Y) - 2\operatorname{Cov}(X, Y), here 12\frac{1}{2}, not 12−12=0\frac{1}{2} - \frac{1}{2} = 0.
  3. Constants come out squared. Var⁡(2X)=4Var⁡(X)\operatorname{Var}(2X) = 4\operatorname{Var}(X), while Cov⁡(2X,Y)=2Cov⁡(X,Y)\operatorname{Cov}(2X, Y) = 2\operatorname{Cov}(X, Y).
  4. Zero covariance is not independence. XX and X2X^2 have covariance 0 when XX is standard normal.
  5. Compare correlations, not covariances. Percent units multiply a covariance by 10,000 and leave the correlation alone.
  6. Check the divisor. np.var and np.std divide by nn by default; np.cov divides by n−1n - 1.

Covariance, correlation and independence

0 of 12 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 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)]=∑∑g pX,Y\mathbb{E}[g(X, Y)] = \sum\sum g\,p_{X,Y} or ∬g fX,Y dA\iint g\,f_{X,Y}\,dA. For independent variables, E[XY]=E[X] E[Y]\mathbb{E}[XY] = \mathbb{E}[X]\,\mathbb{E}[Y].
  • Cov⁡(X,Y)=E[(X−μX)(Y−μY)]=E[XY]−μXμY\operatorname{Cov}(X, Y) = \mathbb{E}[(X - \mu_X)(Y - \mu_Y)] = \mathbb{E}[XY] - \mu_X\mu_Y, in XX's units times YY's. ρ=Cov⁡σXσY\rho = \frac{\operatorname{Cov}}{\sigma_X\sigma_Y} lies in [−1,1][-1, 1], and ±1\pm 1 means an exact line.
  • Covariance is bilinear: Var⁡(aX+bY)=a2Var⁡(X)+b2Var⁡(Y)+2abCov⁡(X,Y)\operatorname{Var}(aX + bY) = a^2\operatorname{Var}(X) + b^2\operatorname{Var}(Y) + 2ab\operatorname{Cov}(X, Y).
  • Independent implies uncorrelated, so variances add; uncorrelated does not imply independent (XX and X2X^2).
  • Samples: sx2s_x^2, sxys_{xy} divide by n−1n - 1, and r=sxysxsyr = \frac{s_{xy}}{s_xs_y}; np.cov, np.corrcoef, and ddof=1 for np.var and np.std.
  • The covariance matrix Σ=E[(X−μ)(X−μ)⊤]\Sigma = \mathbb{E}[(\mathbf{X} - \boldsymbol{\mu})(\mathbf{X} - \boldsymbol{\mu})^\top] has Σij=Cov⁡(Xi,Xj)=ρijσiσj\Sigma_{ij} = \operatorname{Cov}(X_i, X_j) = \rho_{ij}\sigma_i\sigma_j.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.