Covariance Matrix

Formal
(Covariance)

The covariance between two random variables XX and YY measures how they vary together:

Cov(X,Y)E ⁣[(XE[X])(YE[Y])]=E[XY]E[X]E[Y].(1)\text{Cov}(X, Y) \coloneqq \mathbb{E}\!\left[(X - \mathbb{E}[X])(Y - \mathbb{E}[Y])\right] = \mathbb{E}[XY] - \mathbb{E}[X]\,\mathbb{E}[Y]. \tag{1}

If XX and YY are independent, Cov(X,Y)=0\text{Cov}(X, Y) = 0 (but not vice versa in general).

(Correlation) — a normalised, dimensionless version of covariance:

ρXY=Cov(X,Y)σXσY[1,1].(2)\rho_{XY} = \frac{\text{Cov}(X, Y)}{\sigma_X \sigma_Y} \in [-1, 1]. \tag{2}
(Covariance Matrix)

For a random vector X=[X1,,Xn]\mathbf{X} = [X_1, \dots, X_n]^\top, the covariance matrix is the n×nn \times n matrix:

ΣE ⁣[(Xμ)(Xμ)],μ=E[X].(3)\boldsymbol{\Sigma} \coloneqq \mathbb{E}\!\left[(\mathbf{X} - \boldsymbol{\mu})(\mathbf{X} - \boldsymbol{\mu})^\top\right], \quad \boldsymbol{\mu} = \mathbb{E}[\mathbf{X}]. \tag{3}

The (i,j)(i,j) entry is Σij=Cov(Xi,Xj)\Sigma_{ij} = \text{Cov}(X_i, X_j), and the diagonal is Σii=Var(Xi)\Sigma_{ii} = \text{Var}(X_i).

Properties:

  • Σ\boldsymbol{\Sigma} is always symmetric: Σ=Σ\boldsymbol{\Sigma} = \boldsymbol{\Sigma}^\top.
  • Σ\boldsymbol{\Sigma} is always positive semi-definite: vΣv0\mathbf{v}^\top \boldsymbol{\Sigma}\, \mathbf{v} \geq 0 for all v\mathbf{v}.
  • If all variables have non-zero variance and no perfect linear dependencies, Σ\boldsymbol{\Sigma} is positive definite — a valid inner product (see Symmetric, Positive Definite Matrices).
(Sample Covariance Matrix)

Given a dataset {x1,,xN}\{\mathbf{x}_1, \dots, \mathbf{x}_N\} with sample mean xˉ\bar{\mathbf{x}}, the empirical estimate is:

Σ^=1N1i=1N(xixˉ)(xixˉ).(4)\hat{\boldsymbol{\Sigma}} = \frac{1}{N-1}\sum_{i=1}^N (\mathbf{x}_i - \bar{\mathbf{x}})(\mathbf{x}_i - \bar{\mathbf{x}})^\top. \tag{4}

The factor 1N1\frac{1}{N-1} (Bessel’s correction) makes this an unbiased estimator of the population covariance.

Intuition

Think of your microservice cluster emitting a vector of metrics per minute: [p99_latency,  error_rate,  cpu_usage][p99\_latency, \; error\_rate, \; cpu\_usage]. The covariance matrix is the correlation dashboard for these metrics:

  • Diagonal entries: variance of each individual metric (how much it fluctuates).
  • Off-diagonal entries: how two metrics move together. If cpu_usage and p99_latency have high positive covariance, one predicts the other.

The shape of the covariance matrix defines the geometry of the data cloud. Its eigenvectors point along the principal axes of the ellipsoid; its eigenvalues are the squared radii along those axes. This is precisely what PCA computes.

ML Applications
  • PCA — decomposes Σ^\hat{\boldsymbol{\Sigma}} via eigendecomposition. Eigenvectors = principal components; eigenvalues = variance explained along each component.
  • Multivariate Gaussian — fully parameterised by (μ,Σ)(\boldsymbol{\mu}, \boldsymbol{\Sigma}). The covariance matrix controls the shape, orientation, and scale of the distribution’s ellipsoidal level sets.
  • Gaussian Discriminant Analysis (LDA/QDA) — models each class as a Gaussian with its own Σ\boldsymbol{\Sigma}.
  • Mahalanobis distanced(x,μ)=(xμ)Σ1(xμ)d(\mathbf{x}, \boldsymbol{\mu}) = \sqrt{(\mathbf{x}-\boldsymbol{\mu})^\top \boldsymbol{\Sigma}^{-1}(\mathbf{x}-\boldsymbol{\mu})}; a distance metric that accounts for feature correlations and scales. Generalises Euclidean distance.
  • Regularisation of Σ\boldsymbol{\Sigma} — in practice, Σ^\hat{\boldsymbol{\Sigma}} is often rank-deficient (more features than samples). Ridge-style regularisation Σ^+λI\hat{\boldsymbol{\Sigma}} + \lambda \mathbf{I} ensures positive definiteness.
Connections
  • #type/definition
  • #probability
  • #covariance
  • #linear-algebra