Practice / Probability for ML

Variance, covariance and correlation

Ten problems on variance, covariance and correlation: the shortcut formulas, the variance of a sum, bilinearity and the quadratic form aᵀΣa, the bounds on a correlation, a dependent pair with zero covariance, the covariance matrix as a positive semidefinite matrix, the covariance of a linear map and whitening, the variance of an average of correlated variables, the sample covariance as XᵀHX, and the correlation matrix with the bound on a common correlation, with worked solutions and the mistakes that add variances of correlated terms or read zero covariance as independence.

Before you start

Variance and covariance are the second moments that nearly every calculation in machine learning runs on: the variance of a weighted sum is what initialisation schemes control, the covariance matrix is what PCA diagonalises and what the multivariate Gaussian is built from, and the variance of an average of correlated predictions is why ensembles stop improving. All of it follows from one algebraic fact, that covariance is bilinear, and from one inequality, that a variance is never negative. These ten problems derive the shortcut formulas, the variance of a sum and of an arbitrary linear combination, the bounds on a correlation, a pair of variables that are dependent but uncorrelated, the properties of a covariance matrix, how it transforms under a linear map, the variance of a mean of correlated terms, the sample covariance in matrix form and the correlation matrix, ending with the smallest correlation that dd variables can all share. The five mistakes at the end are the ones that produce a plausible number: variances added across correlated terms, a linear map's covariance with its transposes swapped, zero covariance read as independence, a variance scaled by a factor instead of its square, and a sample covariance built from uncentred data.

  • E\mathbb{E} is expectation, which is linear: E[au+bv]=a E[u]+b E[v]\mathbb{E}[au + bv] = a\,\mathbb{E}[u] + b\,\mathbb{E}[v] for constants a,ba, b. A constant cc has E[c]=c\mathbb{E}[c] = c.
  • The variance of a random variable uu with mean mu=E[u]m_u = \mathbb{E}[u] is Var⁡(u)=E[(u−mu)2]\operatorname{Var}(u) = \mathbb{E}[(u - m_u)^2], and its standard deviation is σu=Var⁡(u)\sigma_u = \sqrt{\operatorname{Var}(u)}. The covariance of uu and vv is Cov⁡(u,v)=E[(u−mu)(v−mv)]\operatorname{Cov}(u, v) = \mathbb{E}[(u - m_u)(v - m_v)], so Var⁡(u)=Cov⁡(u,u)\operatorname{Var}(u) = \operatorname{Cov}(u, u). When σu,σv>0\sigma_u, \sigma_v > 0 the correlation is ρ=Cov⁡(u,v)/(σuσv)\rho = \operatorname{Cov}(u, v)/(\sigma_u\sigma_v).
  • uu and vv are uncorrelated if Cov⁡(u,v)=0\operatorname{Cov}(u, v) = 0. They are independent if every event about uu is independent of every event about vv; independence gives E[g(u)h(v)]=E[g(u)] E[h(v)]\mathbb{E}[g(u)h(v)] = \mathbb{E}[g(u)]\,\mathbb{E}[h(v)] for any functions g,hg, h.
  • A random vector x∈Rdx \in \mathbb{R}^d has mean μ=E[x]\mu = \mathbb{E}[x] (entry by entry) and covariance matrix Σ=E[(x−μ)(x−μ)⊤]\Sigma = \mathbb{E}[(x - \mu)(x - \mu)^\top], with Σij=Cov⁡(xi,xj)\Sigma_{ij} = \operatorname{Cov}(x_i, x_j) and Σii=Var⁡(xi)\Sigma_{ii} = \operatorname{Var}(x_i).
  • Data are X∈RN×dX \in \mathbb{R}^{N\times d} with rows xn⊤x_n^\top (examples), sample mean xˉ=1N∑nxn=1NX⊤1\bar x = \tfrac1N\sum_n x_n = \tfrac1N X^\top\mathbf{1} with 1\mathbf{1} the all-ones vector, and sample covariance S=1N−1∑n(xn−xˉ)(xn−xˉ)⊤S = \tfrac{1}{N-1}\sum_n (x_n - \bar x)(x_n - \bar x)^\top, which is what np.cov computes; the maximum-likelihood page's estimate divides by NN instead.
  • A symmetric matrix MM is positive semidefinite, written M⪰0M \succeq 0, if a⊤Ma≥0a^\top Ma \ge 0 for every aa, equivalently if its eigenvalues are all ≥0\ge 0 (the eigenvalues page).

Problems

  1. ·

    Show that Var⁡(u)=E[u2]−(E[u])2\operatorname{Var}(u) = \mathbb{E}[u^2] - (\mathbb{E}[u])^2, that Cov⁡(u,v)=E[uv]−E[u] E[v]\operatorname{Cov}(u, v) = \mathbb{E}[uv] - \mathbb{E}[u]\,\mathbb{E}[v], and that Var⁡(au+b)=a2Var⁡(u)\operatorname{Var}(au + b) = a^2\operatorname{Var}(u) for constants a,ba, b.

  2. ·

    Show that Var⁡(u+v)=Var⁡(u)+Var⁡(v)+2Cov⁡(u,v)\operatorname{Var}(u + v) = \operatorname{Var}(u) + \operatorname{Var}(v) + 2\operatorname{Cov}(u, v) and Var⁡(u−v)=Var⁡(u)+Var⁡(v)−2Cov⁡(u,v)\operatorname{Var}(u - v) = \operatorname{Var}(u) + \operatorname{Var}(v) - 2\operatorname{Cov}(u, v). When do the variances simply add?

  3. ··

    Show that Cov⁡(∑iaiui,∑jbjvj)=∑i,jaibjCov⁡(ui,vj)\operatorname{Cov}\big(\sum_i a_i u_i, \sum_j b_j v_j\big) = \sum_{i,j} a_i b_j\operatorname{Cov}(u_i, v_j). Deduce that for a random vector xx with covariance Σ\Sigma and constant vectors a,ba, b, Cov⁡(a⊤x,b⊤x)=a⊤Σb\operatorname{Cov}(a^\top x, b^\top x) = a^\top\Sigma b and Var⁡(a⊤x)=a⊤Σa\operatorname{Var}(a^\top x) = a^\top\Sigma a.

  4. ··

    Show that −1≤ρ≤1-1 \le \rho \le 1, that ρ=±1\rho = \pm 1 exactly when v=au+bv = au + b for constants with a≠0a \ne 0 having the sign of ρ\rho, and that corr⁡(au+b,cv+d)=sgn⁡(ac) ρ\operatorname{corr}(au + b, cv + d) = \operatorname{sgn}(ac)\,\rho for ac≠0ac \ne 0.

  5. ··

    Let uu be uniform on {−1,0,1}\{-1, 0, 1\} and v=u2v = u^2. Compute Cov⁡(u,v)\operatorname{Cov}(u, v), show that uu and vv are not independent, and compute Var⁡(u+v)\operatorname{Var}(u + v) directly and by Problem 2.

  6. ··

    For a random vector x∈Rdx \in \mathbb{R}^d with mean μ\mu and covariance Σ\Sigma, show that Σ=E[xx⊤]−μμ⊤\Sigma = \mathbb{E}[xx^\top] - \mu\mu^\top, that Σ\Sigma is symmetric and positive semidefinite, and that Σ\Sigma is singular exactly when some a≠0a \ne 0 makes a⊤xa^\top x almost surely constant.

  7. ··

    Let y=Ax+by = Ax + b with A∈Rm×dA \in \mathbb{R}^{m\times d} and b∈Rmb \in \mathbb{R}^m constant. Show that E[y]=Aμ+b\mathbb{E}[y] = A\mu + b and Cov⁡(y)=AΣA⊤\operatorname{Cov}(y) = A\Sigma A^\top. Then, with Σ=QΛQ⊤\Sigma = Q\Lambda Q^\top and every eigenvalue positive, show that W=Λ−1/2Q⊤W = \Lambda^{-1/2}Q^\top gives Cov⁡(Wx)=I\operatorname{Cov}(Wx) = I.

  8. ··

    Let x1,…,xNx_1, \dots, x_N each have variance σ2\sigma^2, and xˉ=1N∑nxn\bar x = \tfrac1N\sum_n x_n. Show that Var⁡(xˉ)=1N21⊤Σ1\operatorname{Var}(\bar x) = \tfrac{1}{N^2}\mathbf{1}^\top\Sigma\mathbf{1} in general, that it is σ2/N\sigma^2/N when the xnx_n are uncorrelated, and that it is σ2(ρ+1−ρN)\sigma^2\big(\rho + \tfrac{1 - \rho}{N}\big) when every pair has correlation ρ\rho. What happens as N→∞N \to \infty in the last case?

  9. ··

    Let H=I−1N11⊤H = I - \tfrac1N\mathbf{1}\mathbf{1}^\top (N×NN\times N). Show that HX=X−1xˉ⊤HX = X - \mathbf{1}\bar x^\top, the centred data, that HH is a symmetric projection (H⊤=HH^\top = H and H2=HH^2 = H), and that S=1N−1X⊤HXS = \tfrac{1}{N-1}X^\top HX.

  10. ···

    The correlation matrix is R=D−1/2ΣD−1/2R = D^{-1/2}\Sigma D^{-1/2} with D=diag⁡(Σ11,…,Σdd)D = \operatorname{diag}(\Sigma_{11}, \dots, \Sigma_{dd}). Show that Rij=corr⁡(xi,xj)R_{ij} = \operatorname{corr}(x_i, x_j), that RR has unit diagonal and R⪰0R \succeq 0. Then, for R=(1−r)I+r11⊤R = (1 - r)I + r\mathbf{1}\mathbf{1}^\top, where every pair has the same correlation rr, find the eigenvalues of RR and deduce that it is a valid correlation matrix exactly when −1d−1≤r≤1-\tfrac{1}{d-1} \le r \le 1.

Worked solutions

Problem 1

Show that Var⁡(u)=E[u2]−(E[u])2\operatorname{Var}(u) = \mathbb{E}[u^2] - (\mathbb{E}[u])^2, that Cov⁡(u,v)=E[uv]−E[u] E[v]\operatorname{Cov}(u, v) = \mathbb{E}[uv] - \mathbb{E}[u]\,\mathbb{E}[v], and that Var⁡(au+b)=a2Var⁡(u)\operatorname{Var}(au + b) = a^2\operatorname{Var}(u) for constants a,ba, b.

  1. With m=E[u]m = \mathbb{E}[u]: (u−m)2=u2−2mu+m2(u - m)^2 = u^2 - 2mu + m^2.Expand the square; mm is a number.
  2. Var⁡(u)=E[u2]−2m E[u]+m2=E[u2]−2m2+m2=E[u2]−m2\operatorname{Var}(u) = \mathbb{E}[u^2] - 2m\,\mathbb{E}[u] + m^2 = \mathbb{E}[u^2] - 2m^2 + m^2 = \mathbb{E}[u^2] - m^2.Linearity of E\mathbb{E}, then E[u]=m\mathbb{E}[u] = m.
  3. (u−mu)(v−mv)=uv−muv−mvu+mumv(u - m_u)(v - m_v) = uv - m_u v - m_v u + m_u m_v, with expectation E[uv]−mumv−mvmu+mumv=E[uv]−mumv\mathbb{E}[uv] - m_u m_v - m_v m_u + m_u m_v = \mathbb{E}[uv] - m_u m_v.Expand, then linearity with E[v]=mv\mathbb{E}[v] = m_v and E[u]=mu\mathbb{E}[u] = m_u.
  4. E[au+b]=am+b\mathbb{E}[au + b] = am + b, so au+b−(am+b)=a(u−m)au + b - (am + b) = a(u - m) and Var⁡(au+b)=E[a2(u−m)2]=a2Var⁡(u)\operatorname{Var}(au + b) = \mathbb{E}[a^2(u - m)^2] = a^2\operatorname{Var}(u).The shift bb cancels in the deviation, and the constant a2a^2 comes out of the expectation.
  5. Var⁡(u)=E[u2]−(E[u])2\operatorname{Var}(u) = \mathbb{E}[u^2] - (\mathbb{E}[u])^2; Cov⁡(u,v)=E[uv]−E[u] E[v]\operatorname{Cov}(u, v) = \mathbb{E}[uv] - \mathbb{E}[u]\,\mathbb{E}[v]; Var⁡(au+b)=a2Var⁡(u)\operatorname{Var}(au + b) = a^2\operatorname{Var}(u)The variance formula is the covariance formula with v=uv = u. A shift leaves spread unchanged, and scaling by aa scales the standard deviation by ∣a∣|a|, so the variance by a2a^2 (Mistake 4). Since a variance is an expectation of a square, E[u2]≥(E[u])2\mathbb{E}[u^2] \ge (\mathbb{E}[u])^2 always, which is the initialisation page's starting point.

Problem 2

Show that Var⁡(u+v)=Var⁡(u)+Var⁡(v)+2Cov⁡(u,v)\operatorname{Var}(u + v) = \operatorname{Var}(u) + \operatorname{Var}(v) + 2\operatorname{Cov}(u, v) and Var⁡(u−v)=Var⁡(u)+Var⁡(v)−2Cov⁡(u,v)\operatorname{Var}(u - v) = \operatorname{Var}(u) + \operatorname{Var}(v) - 2\operatorname{Cov}(u, v). When do the variances simply add?

  1. E[u+v]=mu+mv\mathbb{E}[u + v] = m_u + m_v, so (u+v)−(mu+mv)=(u−mu)+(v−mv)(u + v) - (m_u + m_v) = (u - m_u) + (v - m_v).Linearity of E\mathbb{E}.
  2. ((u−mu)+(v−mv))2=(u−mu)2+2(u−mu)(v−mv)+(v−mv)2\big((u - m_u) + (v - m_v)\big)^2 = (u - m_u)^2 + 2(u - m_u)(v - m_v) + (v - m_v)^2.Expand the square.
  3. Var⁡(u+v)=Var⁡(u)+2Cov⁡(u,v)+Var⁡(v)\operatorname{Var}(u + v) = \operatorname{Var}(u) + 2\operatorname{Cov}(u, v) + \operatorname{Var}(v).Take expectations term by term; each is a definition.
  4. Cov⁡(u,−v)=−Cov⁡(u,v)\operatorname{Cov}(u, -v) = -\operatorname{Cov}(u, v) and Var⁡(−v)=Var⁡(v)\operatorname{Var}(-v) = \operatorname{Var}(v), so step 3 with −v-v in place of vv gives the difference.−v-v has mean −mv-m_v and deviation −(v−mv)-(v - m_v), which flips the sign of the cross term and leaves the square alone; Problem 1 with a=−1a = -1.
  5. Var⁡(u±v)=Var⁡(u)+Var⁡(v)±2Cov⁡(u,v)\operatorname{Var}(u \pm v) = \operatorname{Var}(u) + \operatorname{Var}(v) \pm 2\operatorname{Cov}(u, v); the variances add exactly when uu and vv are uncorrelated, which independent variables areIndependence gives E[uv]=E[u] E[v]\mathbb{E}[uv] = \mathbb{E}[u]\,\mathbb{E}[v], so Cov⁡=0\operatorname{Cov} = 0 by Problem 1; the converse fails (Problem 5). A sum of correlated variables can have variance anywhere between (σu−σv)2(\sigma_u - \sigma_v)^2 and (σu+σv)2(\sigma_u + \sigma_v)^2 (Problem 4), and adding variances regardless is Mistake 1. Problem 8 is this formula applied to NN terms.

Problem 3

Show that Cov⁡(∑iaiui,∑jbjvj)=∑i,jaibjCov⁡(ui,vj)\operatorname{Cov}\big(\sum_i a_i u_i, \sum_j b_j v_j\big) = \sum_{i,j} a_i b_j\operatorname{Cov}(u_i, v_j). Deduce that for a random vector xx with covariance Σ\Sigma and constant vectors a,ba, b, Cov⁡(a⊤x,b⊤x)=a⊤Σb\operatorname{Cov}(a^\top x, b^\top x) = a^\top\Sigma b and Var⁡(a⊤x)=a⊤Σa\operatorname{Var}(a^\top x) = a^\top\Sigma a.

  1. Let U=∑iaiuiU = \sum_i a_i u_i and V=∑jbjvjV = \sum_j b_j v_j. Then U−E[U]=∑iai(ui−E[ui])U - \mathbb{E}[U] = \sum_i a_i(u_i - \mathbb{E}[u_i]) and likewise V−E[V]=∑jbj(vj−E[vj])V - \mathbb{E}[V] = \sum_j b_j(v_j - \mathbb{E}[v_j]).Linearity of E\mathbb{E} gives E[U]=∑iaiE[ui]\mathbb{E}[U] = \sum_i a_i\mathbb{E}[u_i]; subtract.
  2. (U−E[U])(V−E[V])=∑i∑jaibj(ui−E[ui])(vj−E[vj])(U - \mathbb{E}[U])(V - \mathbb{E}[V]) = \sum_i\sum_j a_i b_j(u_i - \mathbb{E}[u_i])(v_j - \mathbb{E}[v_j]).Multiply the two sums term by term.
  3. Cov⁡(U,V)=∑i,jaibj E[(ui−E[ui])(vj−E[vj])]=∑i,jaibjCov⁡(ui,vj)\operatorname{Cov}(U, V) = \sum_{i,j} a_i b_j\,\mathbb{E}[(u_i - \mathbb{E}[u_i])(v_j - \mathbb{E}[v_j])] = \sum_{i,j} a_i b_j\operatorname{Cov}(u_i, v_j).Linearity of E\mathbb{E} over the double sum.
  4. With u=v=xu = v = x: Cov⁡(a⊤x,b⊤x)=∑i,jaiΣijbj=a⊤Σb\operatorname{Cov}(a^\top x, b^\top x) = \sum_{i,j} a_i\Sigma_{ij}b_j = a^\top\Sigma b, and b=ab = a gives Var⁡(a⊤x)=a⊤Σa\operatorname{Var}(a^\top x) = a^\top\Sigma a.Σij=Cov⁡(xi,xj)\Sigma_{ij} = \operatorname{Cov}(x_i, x_j), and a⊤Σb=∑i,jaiΣijbja^\top\Sigma b = \sum_{i,j} a_i\Sigma_{ij}b_j is the definition of the matrix product.
  5. Cov⁡(∑iaiui,∑jbjvj)=∑i,jaibjCov⁡(ui,vj)\operatorname{Cov}\big(\sum_i a_i u_i, \sum_j b_j v_j\big) = \sum_{i,j} a_i b_j\operatorname{Cov}(u_i, v_j); Cov⁡(a⊤x,b⊤x)=a⊤Σb\operatorname{Cov}(a^\top x, b^\top x) = a^\top\Sigma b; Var⁡(a⊤x)=a⊤Σa\operatorname{Var}(a^\top x) = a^\top\Sigma aCovariance is bilinear, so it behaves like an inner product on random variables, and Σ\Sigma is the matrix of that inner product on the coordinates of xx. The quadratic form a⊤Σaa^\top\Sigma a is never negative because it is a variance, which is Problem 6, and it is the quantity PCA maximises over unit vectors aa on the PCA page.

Problem 4

Show that −1≤ρ≤1-1 \le \rho \le 1, that ρ=±1\rho = \pm 1 exactly when v=au+bv = au + b for constants with a≠0a \ne 0 having the sign of ρ\rho, and that corr⁡(au+b,cv+d)=sgn⁡(ac) ρ\operatorname{corr}(au + b, cv + d) = \operatorname{sgn}(ac)\,\rho for ac≠0ac \ne 0.

  1. Let u~=(u−mu)/σu\tilde u = (u - m_u)/\sigma_u and v~=(v−mv)/σv\tilde v = (v - m_v)/\sigma_v. Then E[u~]=E[v~]=0\mathbb{E}[\tilde u] = \mathbb{E}[\tilde v] = 0, Var⁡(u~)=Var⁡(v~)=1\operatorname{Var}(\tilde u) = \operatorname{Var}(\tilde v) = 1 and Cov⁡(u~,v~)=Cov⁡(u,v)/(σuσv)=ρ\operatorname{Cov}(\tilde u, \tilde v) = \operatorname{Cov}(u, v)/(\sigma_u\sigma_v) = \rho.Problem 1 with a=1/σua = 1/\sigma_u and b=−mu/σub = -m_u/\sigma_u for the variance; Problem 3 for the covariance.
  2. 0≤Var⁡(u~−v~)=1+1−2ρ0 \le \operatorname{Var}(\tilde u - \tilde v) = 1 + 1 - 2\rho, so ρ≤1\rho \le 1.A variance is the expectation of a square; Problem 2 for the difference.
  3. 0≤Var⁡(u~+v~)=2+2ρ0 \le \operatorname{Var}(\tilde u + \tilde v) = 2 + 2\rho, so ρ≥−1\rho \ge -1.Problem 2 for the sum.
  4. If ρ=1\rho = 1 then Var⁡(u~−v~)=0\operatorname{Var}(\tilde u - \tilde v) = 0, so u~−v~\tilde u - \tilde v equals its mean 00 and v=mv+(σv/σu)(u−mu)v = m_v + (\sigma_v/\sigma_u)(u - m_u), a line of positive slope; ρ=−1\rho = -1 gives v~=−u~\tilde v = -\tilde u, a line of negative slope. Conversely, if v=au+bv = au + b then Cov⁡(u,v)=aVar⁡(u)\operatorname{Cov}(u, v) = a\operatorname{Var}(u) and σv=∣a∣σu\sigma_v = |a|\sigma_u, so ρ=a/∣a∣=sgn⁡(a)\rho = a/|a| = \operatorname{sgn}(a).A random variable with zero variance is constant; Problems 3 and 1 for the converse.
  5. Cov⁡(au+b,cv+d)=acCov⁡(u,v)\operatorname{Cov}(au + b, cv + d) = ac\operatorname{Cov}(u, v) and σau+b σcv+d=∣a∣∣c∣σuσv\sigma_{au+b}\,\sigma_{cv+d} = |a||c|\sigma_u\sigma_v, so the correlation is ac∣ac∣ρ\dfrac{ac}{|ac|}\rho.Problem 3 for the covariance, with the shifts dropping out as in Problem 1; Problem 1 for the standard deviations.
  6. −1≤ρ≤1-1 \le \rho \le 1; ρ=±1\rho = \pm 1 exactly when v=au+bv = au + b with sgn⁡(a)=ρ\operatorname{sgn}(a) = \rho; corr⁡(au+b,cv+d)=sgn⁡(ac) ρ\operatorname{corr}(au + b, cv + d) = \operatorname{sgn}(ac)\,\rhoThis is the Cauchy–Schwarz inequality for the inner product of Problem 3. Correlation measures only linear association and ignores units: metres to feet or Celsius to Fahrenheit leaves it unchanged, and negating one variable flips its sign. ρ=0\rho = 0 says nothing about nonlinear dependence (Problem 5).

Problem 5

Let uu be uniform on {−1,0,1}\{-1, 0, 1\} and v=u2v = u^2. Compute Cov⁡(u,v)\operatorname{Cov}(u, v), show that uu and vv are not independent, and compute Var⁡(u+v)\operatorname{Var}(u + v) directly and by Problem 2.

  1. E[u]=0\mathbb{E}[u] = 0 and E[u2]=13(1+0+1)=23\mathbb{E}[u^2] = \tfrac13(1 + 0 + 1) = \tfrac23, so Var⁡(u)=23\operatorname{Var}(u) = \tfrac23.Each value has probability 13\tfrac13; Problem 1.
  2. vv is 11 with probability 23\tfrac23 and 00 with probability 13\tfrac13, so E[v]=23\mathbb{E}[v] = \tfrac23, E[v2]=E[v]=23\mathbb{E}[v^2] = \mathbb{E}[v] = \tfrac23 and Var⁡(v)=23−49=29\operatorname{Var}(v) = \tfrac23 - \tfrac49 = \tfrac29.v2=vv^2 = v for a 00–11 variable; Problem 1.
  3. E[uv]=E[u3]=13(−1+0+1)=0\mathbb{E}[uv] = \mathbb{E}[u^3] = \tfrac13(-1 + 0 + 1) = 0, so Cov⁡(u,v)=0−0⋅23=0\operatorname{Cov}(u, v) = 0 - 0\cdot\tfrac23 = 0.Problem 1's shortcut.
  4. P(v=1∣u=0)=0P(v = 1 \mid u = 0) = 0 while P(v=1)=23P(v = 1) = \tfrac23, so uu and vv are dependent.Knowing u=0u = 0 fixes v=0v = 0; independence would need every conditional probability to equal the marginal (the Bayes page).
  5. u+vu + v takes the values 00 (at u=−1u = -1), 00 (at u=0u = 0) and 22 (at u=1u = 1), so E[u+v]=23\mathbb{E}[u + v] = \tfrac23, E[(u+v)2]=43\mathbb{E}[(u + v)^2] = \tfrac43 and Var⁡(u+v)=43−49=89\operatorname{Var}(u + v) = \tfrac43 - \tfrac49 = \tfrac89.Enumerate the three cases; Problem 1.
  6. Cov⁡(u,u2)=0\operatorname{Cov}(u, u^2) = 0 although v=u2v = u^2 is a function of uu; Var⁡(u)=23\operatorname{Var}(u) = \tfrac23, Var⁡(v)=29\operatorname{Var}(v) = \tfrac29 and Var⁡(u+v)=89=Var⁡(u)+Var⁡(v)\operatorname{Var}(u + v) = \tfrac89 = \operatorname{Var}(u) + \operatorname{Var}(v), the cross term being 00Covariance sees only linear association, and vv is an even function of a symmetric uu, so the positive and negative sides cancel. Zero correlation is enough for variances to add (Problem 2) but not for independence (Mistake 3): E[u2v]=E[u4]=23\mathbb{E}[u^2 v] = \mathbb{E}[u^4] = \tfrac23 while E[u2] E[v]=49\mathbb{E}[u^2]\,\mathbb{E}[v] = \tfrac49. For jointly Gaussian variables the two notions coincide, which is the case people generalise from.

Problem 6

For a random vector x∈Rdx \in \mathbb{R}^d with mean μ\mu and covariance Σ\Sigma, show that Σ=E[xx⊤]−μμ⊤\Sigma = \mathbb{E}[xx^\top] - \mu\mu^\top, that Σ\Sigma is symmetric and positive semidefinite, and that Σ\Sigma is singular exactly when some a≠0a \ne 0 makes a⊤xa^\top x almost surely constant.

  1. (x−μ)(x−μ)⊤=xx⊤−xμ⊤−μx⊤+μμ⊤(x - \mu)(x - \mu)^\top = xx^\top - x\mu^\top - \mu x^\top + \mu\mu^\top, with expectation E[xx⊤]−μμ⊤−μμ⊤+μμ⊤=E[xx⊤]−μμ⊤\mathbb{E}[xx^\top] - \mu\mu^\top - \mu\mu^\top + \mu\mu^\top = \mathbb{E}[xx^\top] - \mu\mu^\top.Expand the outer product; E\mathbb{E} applies entry by entry and E[x]=μ\mathbb{E}[x] = \mu.
  2. Σ⊤=E[((x−μ)(x−μ)⊤)⊤]=Σ\Sigma^\top = \mathbb{E}\big[\big((x - \mu)(x - \mu)^\top\big)^\top\big] = \Sigma.(ab⊤)⊤=ba⊤(ab^\top)^\top = ba^\top, and here a=ba = b.
  3. For every a∈Rda \in \mathbb{R}^d, a⊤Σa=Var⁡(a⊤x)≥0a^\top\Sigma a = \operatorname{Var}(a^\top x) \ge 0.Problem 3; a variance is nonnegative.
  4. If Σa=0\Sigma a = 0 for some a≠0a \ne 0 then a⊤Σa=0a^\top\Sigma a = 0, so Var⁡(a⊤x)=0\operatorname{Var}(a^\top x) = 0 and a⊤xa^\top x equals its mean almost surely. Conversely, if Var⁡(a⊤x)=0\operatorname{Var}(a^\top x) = 0 then a⊤Σa=0a^\top\Sigma a = 0; writing Σ=QΛQ⊤\Sigma = Q\Lambda Q^\top, that is ∑jλj(qj⊤a)2=0\sum_j\lambda_j(q_j^\top a)^2 = 0 with every λj≥0\lambda_j \ge 0, so λjqj⊤a=0\lambda_j q_j^\top a = 0 for each jj and Σa=∑jλjqj(qj⊤a)=0\Sigma a = \sum_j\lambda_j q_j(q_j^\top a) = 0.A singular matrix has a nonzero null vector; the eigenvalues page's decomposition of a symmetric matrix, with step 3 giving λj≥0\lambda_j \ge 0.
  5. Σ=E[xx⊤]−μμ⊤\Sigma = \mathbb{E}[xx^\top] - \mu\mu^\top; Σ=Σ⊤\Sigma = \Sigma^\top; a⊤Σa=Var⁡(a⊤x)≥0a^\top\Sigma a = \operatorname{Var}(a^\top x) \ge 0 for all aa; Σ\Sigma is singular exactly when some a≠0a \ne 0 has a⊤xa^\top x almost surely constantA singular covariance says the data lie on a hyperplane. One-hot encoding a category with all KK columns does this, since the columns sum to 11 on every example, which is the dummy-variable trap of linear regression. The multivariate-Gaussian page needs Σ\Sigma invertible to write its density, and the PCA page reads the eigenvalues of Σ\Sigma as variances along the qjq_j.

Problem 7

Let y=Ax+by = Ax + b with A∈Rm×dA \in \mathbb{R}^{m\times d} and b∈Rmb \in \mathbb{R}^m constant. Show that E[y]=Aμ+b\mathbb{E}[y] = A\mu + b and Cov⁡(y)=AΣA⊤\operatorname{Cov}(y) = A\Sigma A^\top. Then, with Σ=QΛQ⊤\Sigma = Q\Lambda Q^\top and every eigenvalue positive, show that W=Λ−1/2Q⊤W = \Lambda^{-1/2}Q^\top gives Cov⁡(Wx)=I\operatorname{Cov}(Wx) = I.

  1. E[y]=A E[x]+b=Aμ+b\mathbb{E}[y] = A\,\mathbb{E}[x] + b = A\mu + b.Linearity, entry by entry: yi=∑jAijxj+biy_i = \sum_j A_{ij}x_j + b_i.
  2. y−E[y]=A(x−μ)y - \mathbb{E}[y] = A(x - \mu).Subtract step 1 from yy; bb cancels.
  3. Cov⁡(y)=E[A(x−μ)(x−μ)⊤A⊤]=A E[(x−μ)(x−μ)⊤] A⊤=AΣA⊤\operatorname{Cov}(y) = \mathbb{E}\big[A(x - \mu)(x - \mu)^\top A^\top\big] = A\,\mathbb{E}[(x - \mu)(x - \mu)^\top]\,A^\top = A\Sigma A^\top.(Av)(Av)⊤=Avv⊤A⊤(Av)(Av)^\top = Avv^\top A^\top, and the constant matrices come out of the expectation.
  4. Cov⁡(Wx)=WΣW⊤=Λ−1/2Q⊤ QΛQ⊤ QΛ−1/2=Λ−1/2ΛΛ−1/2=I\operatorname{Cov}(Wx) = W\Sigma W^\top = \Lambda^{-1/2}Q^\top\,Q\Lambda Q^\top\,Q\Lambda^{-1/2} = \Lambda^{-1/2}\Lambda\Lambda^{-1/2} = I.Step 3 with A=WA = W; Q⊤Q=IQ^\top Q = I because QQ is orthogonal, and diagonal matrices multiply entry by entry.
  5. E[Ax+b]=Aμ+b\mathbb{E}[Ax + b] = A\mu + b; Cov⁡(Ax+b)=AΣA⊤\operatorname{Cov}(Ax + b) = A\Sigma A^\top; W=Λ−1/2Q⊤W = \Lambda^{-1/2}Q^\top whitens: Cov⁡(Wx)=I\operatorname{Cov}(Wx) = IShapes: (m×d)(d×d)(d×m)=m×m(m\times d)(d\times d)(d\times m) = m\times m, symmetric and positive semidefinite as a covariance must be; the version with the transposes swapped is Mistake 2. The multivariate-Gaussian page proves the same for Gaussian xx and adds that yy is Gaussian; nothing here needed a distribution. Any WW with WΣW⊤=IW\Sigma W^\top = I whitens, for instance L−1L^{-1} from the Cholesky factor Σ=LL⊤\Sigma = LL^\top; the PCA page's scores are Q⊤(x−μ)Q^\top(x - \mu), whitened by Λ−1/2\Lambda^{-1/2}.

Problem 8

Let x1,…,xNx_1, \dots, x_N each have variance σ2\sigma^2, and xˉ=1N∑nxn\bar x = \tfrac1N\sum_n x_n. Show that Var⁡(xˉ)=1N21⊤Σ1\operatorname{Var}(\bar x) = \tfrac{1}{N^2}\mathbf{1}^\top\Sigma\mathbf{1} in general, that it is σ2/N\sigma^2/N when the xnx_n are uncorrelated, and that it is σ2(ρ+1−ρN)\sigma^2\big(\rho + \tfrac{1 - \rho}{N}\big) when every pair has correlation ρ\rho. What happens as N→∞N \to \infty in the last case?

  1. xˉ=a⊤x\bar x = a^\top x with a=1N1a = \tfrac1N\mathbf{1}, so Var⁡(xˉ)=a⊤Σa=1N21⊤Σ1=1N2∑n,mΣnm\operatorname{Var}(\bar x) = a^\top\Sigma a = \tfrac{1}{N^2}\mathbf{1}^\top\Sigma\mathbf{1} = \tfrac{1}{N^2}\sum_{n,m}\Sigma_{nm}.Problem 3; 1⊤Σ1\mathbf{1}^\top\Sigma\mathbf{1} adds up every entry of Σ\Sigma.
  2. Uncorrelated: Σ=σ2I\Sigma = \sigma^2 I, so 1⊤Σ1=Nσ2\mathbf{1}^\top\Sigma\mathbf{1} = N\sigma^2 and Var⁡(xˉ)=σ2/N\operatorname{Var}(\bar x) = \sigma^2/N.Only the NN diagonal entries are nonzero.
  3. Common correlation: Σnn=σ2\Sigma_{nn} = \sigma^2 and Σnm=ρσ2\Sigma_{nm} = \rho\sigma^2 for n≠mn \ne m, so 1⊤Σ1=Nσ2+N(N−1)ρσ2\mathbf{1}^\top\Sigma\mathbf{1} = N\sigma^2 + N(N - 1)\rho\sigma^2.NN diagonal entries and N(N−1)N(N - 1) off-diagonal ones.
  4. Var⁡(xˉ)=σ2 N+N(N−1)ρN2=σ2 1+(N−1)ρN=σ2(ρ+1−ρN)\operatorname{Var}(\bar x) = \sigma^2\,\dfrac{N + N(N - 1)\rho}{N^2} = \sigma^2\,\dfrac{1 + (N - 1)\rho}{N} = \sigma^2\Big(\rho + \dfrac{1 - \rho}{N}\Big).Divide by N2N^2, then write 1+(N−1)ρ=Nρ+(1−ρ)1 + (N - 1)\rho = N\rho + (1 - \rho).
  5. Var⁡(xˉ)=1N21⊤Σ1\operatorname{Var}(\bar x) = \tfrac{1}{N^2}\mathbf{1}^\top\Sigma\mathbf{1}; σ2/N\sigma^2/N when uncorrelated; σ2(ρ+1−ρN)\sigma^2\big(\rho + \tfrac{1 - \rho}{N}\big) under a common correlation ρ\rho, which tends to ρσ2\rho\sigma^2 rather than 00 as N→∞N \to \inftyAveraging removes only the uncorrelated part of the variance. This is the ensemble calculation: averaging NN models whose errors have correlation ρ\rho can reduce the variance by at most a factor ρ\rho however many models are used, which is why random forests decorrelate their trees by subsampling features, and why Mistake 1 at scale, σ2/N\sigma^2/N for correlated models, overstates the gain. Problem 10 says which values of ρ\rho are possible.

Problem 9

Let H=I−1N11⊤H = I - \tfrac1N\mathbf{1}\mathbf{1}^\top (N×NN\times N). Show that HX=X−1xˉ⊤HX = X - \mathbf{1}\bar x^\top, the centred data, that HH is a symmetric projection (H⊤=HH^\top = H and H2=HH^2 = H), and that S=1N−1X⊤HXS = \tfrac{1}{N-1}X^\top HX.

  1. 1⊤X=Nxˉ⊤\mathbf{1}^\top X = N\bar x^\top, so 1N11⊤X=1xˉ⊤\tfrac1N\mathbf{1}\mathbf{1}^\top X = \mathbf{1}\bar x^\top and HX=X−1xˉ⊤HX = X - \mathbf{1}\bar x^\top.1⊤X\mathbf{1}^\top X is the row of column sums, which is NN times the row of column means; 1xˉ⊤\mathbf{1}\bar x^\top repeats xˉ⊤\bar x^\top in every row, so row nn of HXHX is (xn−xˉ)⊤(x_n - \bar x)^\top.
  2. H⊤=I−1N(11⊤)⊤=HH^\top = I - \tfrac1N(\mathbf{1}\mathbf{1}^\top)^\top = H.(11⊤)⊤=11⊤(\mathbf{1}\mathbf{1}^\top)^\top = \mathbf{1}\mathbf{1}^\top.
  3. H2=I−2N11⊤+1N21(1⊤1)1⊤=I−2N11⊤+1N11⊤=HH^2 = I - \tfrac2N\mathbf{1}\mathbf{1}^\top + \tfrac{1}{N^2}\mathbf{1}(\mathbf{1}^\top\mathbf{1})\mathbf{1}^\top = I - \tfrac2N\mathbf{1}\mathbf{1}^\top + \tfrac1N\mathbf{1}\mathbf{1}^\top = H.Expand; 1⊤1=N\mathbf{1}^\top\mathbf{1} = N.
  4. ∑n(xn−xˉ)(xn−xˉ)⊤=(HX)⊤(HX)=X⊤H⊤HX=X⊤HX\sum_n(x_n - \bar x)(x_n - \bar x)^\top = (HX)^\top(HX) = X^\top H^\top HX = X^\top HX.M⊤MM^\top M is the sum of the outer products of the rows of MM, and row nn of HXHX is (xn−xˉ)⊤(x_n - \bar x)^\top; then steps 2 and 3.
  5. HX=X−1xˉ⊤HX = X - \mathbf{1}\bar x^\top; H⊤=H=H2H^\top = H = H^2; S=1N−1X⊤HXS = \tfrac{1}{N-1}X^\top HX, and the maximum-likelihood page's 1N\tfrac1N version is 1NX⊤HX\tfrac1N X^\top HXHH projects onto the vectors whose entries sum to zero (the least-squares page's projections), and centering is that projection applied to each column. The two normalisations differ by a scalar, so they share eigenvectors; np.cov(X.T) divides by N−1N - 1, bias=True by NN. Skipping the HH is Mistake 5.

Problem 10

The correlation matrix is R=D−1/2ΣD−1/2R = D^{-1/2}\Sigma D^{-1/2} with D=diag⁡(Σ11,…,Σdd)D = \operatorname{diag}(\Sigma_{11}, \dots, \Sigma_{dd}). Show that Rij=corr⁡(xi,xj)R_{ij} = \operatorname{corr}(x_i, x_j), that RR has unit diagonal and R⪰0R \succeq 0. Then, for R=(1−r)I+r11⊤R = (1 - r)I + r\mathbf{1}\mathbf{1}^\top, where every pair has the same correlation rr, find the eigenvalues of RR and deduce that it is a valid correlation matrix exactly when −1d−1≤r≤1-\tfrac{1}{d-1} \le r \le 1.

  1. (D−1/2ΣD−1/2)ij=ΣijΣiiΣjj=Cov⁡(xi,xj)σiσj(D^{-1/2}\Sigma D^{-1/2})_{ij} = \dfrac{\Sigma_{ij}}{\sqrt{\Sigma_{ii}}\sqrt{\Sigma_{jj}}} = \dfrac{\operatorname{Cov}(x_i, x_j)}{\sigma_i\sigma_j}, which is 11 when i=ji = j.A diagonal matrix on the left scales row ii by its entry and on the right scales column jj.
  2. R=Cov⁡(D−1/2x)⪰0R = \operatorname{Cov}(D^{-1/2}x) \succeq 0.Problem 7 with A=D−1/2A = D^{-1/2}, which is symmetric: RR is the covariance of the standardised variables xi/σix_i/\sigma_i, and every covariance is positive semidefinite by Problem 6.
  3. R1=(1−r)1+r1(1⊤1)=(1+(d−1)r)1R\mathbf{1} = (1 - r)\mathbf{1} + r\mathbf{1}(\mathbf{1}^\top\mathbf{1}) = \big(1 + (d - 1)r\big)\mathbf{1}.1⊤1=d\mathbf{1}^\top\mathbf{1} = d; so 1\mathbf{1} is an eigenvector with eigenvalue 1+(d−1)r1 + (d - 1)r.
  4. For any aa with 1⊤a=0\mathbf{1}^\top a = 0: Ra=(1−r)a+r1(1⊤a)=(1−r)aRa = (1 - r)a + r\mathbf{1}(\mathbf{1}^\top a) = (1 - r)a.The zero-sum vectors form a (d−1)(d - 1)-dimensional space, so 1−r1 - r is an eigenvalue with multiplicity d−1d - 1, and with step 3 all dd eigenvalues are found.
  5. R⪰0R \succeq 0 exactly when 1−r≥01 - r \ge 0 and 1+(d−1)r≥01 + (d - 1)r \ge 0, that is when −1d−1≤r≤1-\tfrac{1}{d-1} \le r \le 1.A symmetric matrix is positive semidefinite exactly when its eigenvalues are nonnegative (the eigenvalues page).
  6. Rij=corr⁡(xi,xj)R_{ij} = \operatorname{corr}(x_i, x_j), Rii=1R_{ii} = 1, R⪰0R \succeq 0; (1−r)I+r11⊤(1 - r)I + r\mathbf{1}\mathbf{1}^\top has eigenvalues 1+(d−1)r1 + (d - 1)r (once, eigenvector 1\mathbf{1}) and 1−r1 - r (d−1d - 1 times), so it is a correlation matrix exactly when −1d−1≤r≤1-\tfrac{1}{d-1} \le r \le 1Three variables cannot all be pairwise correlated at −0.9-0.9: the lower bound is −12-\tfrac12 for d=3d = 3 and tends to 00 as dd grows, so a large set of variables can be only slightly negatively correlated on average. At r=−1d−1r = -\tfrac{1}{d-1} the sum 1⊤x\mathbf{1}^\top x has variance 1⊤R1=d(1+(d−1)r)=0\mathbf{1}^\top R\mathbf{1} = d\big(1 + (d - 1)r\big) = 0, Problem 6's singular case. Problem 8's variance of the mean is nonnegative on exactly this range.

Where this goes wrong

1. Adding the variances of correlated terms

Variances add for independent variables, which is the case every first example uses.

  1. Var⁡(u+v)=Var⁡(u)+Var⁡(v)+2Cov⁡(u,v)\operatorname{Var}(u + v) = \operatorname{Var}(u) + \operatorname{Var}(v) + 2\operatorname{Cov}(u, v)Right so far: Problem 2.
  2. “Variances add.”The habit that causes the mistake: additivity learned on independent variables and carried over to a pair that is correlated.
  3. Var⁡(u+v)=Var⁡(u)+Var⁡(v)\operatorname{Var}(u + v) = \operatorname{Var}(u) + \operatorname{Var}(v)With ρ=12\rho = \tfrac12 and equal variances σ2\sigma^2 the true variance is 3σ23\sigma^2, not 2σ22\sigma^2, and with v=c−uv = c - u the sum is constant while the formula says 2σ22\sigma^2. At scale it is Problem 8 with the correlations dropped: an average of NN correlated models is credited with variance σ2/N\sigma^2/N, which it cannot reach, because ρσ2\rho\sigma^2 remains however large NN is.

2. Covariance of a linear map written with the transposes swapped

Quadratic forms are written a⊤Σaa^\top\Sigma a, and the matrix version looks like the same thing with AA in place of aa.

  1. y=Ax+by = Ax + b with Cov⁡(y)=E[(y−E[y])(y−E[y])⊤]\operatorname{Cov}(y) = \mathbb{E}[(y - \mathbb{E}[y])(y - \mathbb{E}[y])^\top]Right so far: the definition, as in Problem 7.
  2. “Var⁡(a⊤x)=a⊤Σa\operatorname{Var}(a^\top x) = a^\top\Sigma a, so Cov⁡(Ax)=A⊤ΣA\operatorname{Cov}(Ax) = A^\top\Sigma A.”The analogy that causes the mistake: the vector formula has its transpose on the left because the map is a⊤xa^\top x, a row times xx; for AxAx the map's transpose lands on the right.
  3. Cov⁡(Ax+b)=A⊤ΣA\operatorname{Cov}(Ax + b) = A^\top\Sigma AFor A∈Rm×dA \in \mathbb{R}^{m\times d} with m≠dm \ne d the product A⊤ΣAA^\top\Sigma A is not even defined, since A⊤A^\top is d×md\times m and Σ\Sigma is d×dd\times d. When m=dm = d it is a different matrix: for A=(1101)A = \begin{pmatrix} 1 & 1 \\ 0 & 1\end{pmatrix} and Σ=I\Sigma = I, AΣA⊤=(2111)A\Sigma A^\top = \begin{pmatrix} 2 & 1 \\ 1 & 1\end{pmatrix} while A⊤ΣA=(1112)A^\top\Sigma A = \begin{pmatrix} 1 & 1 \\ 1 & 2\end{pmatrix}. The vector case is the m=1m = 1 instance of Problem 7 with A=a⊤A = a^\top, and (a⊤)Σ(a⊤)⊤=a⊤Σa(a^\top)\Sigma(a^\top)^\top = a^\top\Sigma a agrees.

3. Zero covariance read as independence

Independence implies zero covariance, and the implication is usually learned in that direction only.

  1. Cov⁡(u,v)=0\operatorname{Cov}(u, v) = 0Right so far: a computed fact, as for uu and u2u^2 in Problem 5.
  2. “Zero covariance means no relation, so uu and vv are independent.”The converse that causes the mistake: true for jointly Gaussian variables, and generalised from there.
  3. P(v=1∣u=0)=P(v=1)P(v = 1 \mid u = 0) = P(v = 1) for v=u2v = u^2Problem 5: the left side is 00 and the right is 23\tfrac23. Covariance is one number measuring linear association, and a symmetric nonlinear dependence leaves it at 00. The consequences are not only probabilistic: E[uv]\mathbb{E}[uv] factorises but E[u2v]=23≠E[u2] E[v]=49\mathbb{E}[u^2 v] = \tfrac23 \ne \mathbb{E}[u^2]\,\mathbb{E}[v] = \tfrac49, so any calculation that uses independence beyond the second moment is wrong.

4. Scaling a variance by the factor instead of its square

Expectations scale linearly, and a variance is an expectation.

  1. E[au]=a E[u]\mathbb{E}[au] = a\,\mathbb{E}[u]Right so far: linearity.
  2. “Variance is an expectation, so it scales the same way.”The analogy that causes the mistake: variance is the expectation of a square, and the factor sits inside the square.
  3. Var⁡(au)=aVar⁡(u)\operatorname{Var}(au) = a\operatorname{Var}(u)Problem 1: Var⁡(au)=a2Var⁡(u)\operatorname{Var}(au) = a^2\operatorname{Var}(u). Converting metres to centimetres multiplies a variance by 10410^4, not 100100, and halving a layer's outputs quarters their variance, which is the square in the initialisation page's Var⁡(wx)=σw2 E[x2]\operatorname{Var}(wx) = \sigma_w^2\,\mathbb{E}[x^2]. For a<0a < 0 the wrong formula makes a variance negative. It is correct for the standard deviation, which scales by ∣a∣|a|, and that is where it comes from.

5. Sample covariance from uncentred data

X⊤XX^\top X is the Gram matrix of least squares and of every normal equation, and the covariance looks like it with a 1N−1\tfrac{1}{N-1} in front.

  1. S=1N−1∑n(xn−xˉ)(xn−xˉ)⊤S = \tfrac{1}{N-1}\sum_n(x_n - \bar x)(x_n - \bar x)^\topRight so far: the definition, as in Problem 9.
  2. “The sum of the outer products of the rows is X⊤XX^\top X.”The shortcut that causes the mistake: that is right for the rows of XX, but the rows summed here are those of the centred HXHX.
  3. S=1N−1X⊤XS = \tfrac{1}{N-1}X^\top XExpanding xn=(xn−xˉ)+xˉx_n = (x_n - \bar x) + \bar x gives ∑nxnxn⊤=∑n(xn−xˉ)(xn−xˉ)⊤+Nxˉxˉ⊤\sum_n x_nx_n^\top = \sum_n(x_n - \bar x)(x_n - \bar x)^\top + N\bar x\bar x^\top, so the line computes S+NN−1xˉxˉ⊤S + \tfrac{N}{N-1}\bar x\bar x^\top: the covariance plus a rank-one term that dominates whenever the means are large next to the spread. Its leading eigenvector then points towards xˉ\bar x rather than along the direction of greatest spread, which is the PCA page's first mistake. The fix is X⊤HXX^\top HX, or subtracting the column means before forming the product.

Print this set: variance-covariance-and-correlation.pdf (problems, answers, and worked solutions on separate pages).