Math glossary

Covariance Matrix: Formula, Interpretation, and Examples

Calculate sample and population covariance matrices. See diagonal variances, correlations, and six examples that explain units and rank.

TL;DR

A covariance matrix stores variances on its diagonal and covariances elsewhere. For nn complete observations in rows, centre each column and compute S=XcTXc/(n−1)S=X_c^{\mathsf T}X_c/(n-1) for the sample estimate.

Key Points

  • The matrix is square with one row and column per variable.
  • Diagonal entries measure each variable's variance.
  • Off-diagonal entries describe how pairs vary together, in the product of their units.
  • Centre the columns and use a consistent denominator for all entries.
  • A covariance matrix computed from the same complete observations is symmetric and positive semidefinite.

What is a covariance matrix?

A covariance matrix arranges each variable's variance and every pair's covariance in one table. For two variables XX and YY, the population matrix has the form

Σ=(Var(X)Cov(X,Y)Cov(Y,X)Var(Y)).\Sigma=\begin{pmatrix} \operatorname{Var}(X)&\operatorname{Cov}(X,Y)\\ \operatorname{Cov}(Y,X)&\operatorname{Var}(Y) \end{pmatrix}.

A positive covariance means products of deviations from the means are positive overall. Same-direction deviations outweigh opposite-direction deviations after accounting for their sizes. Negative covariance means the opposite-direction deviations outweigh the same-direction ones. Zero covariance can occur even when variables have a nonlinear relationship.

A two by two sample covariance matrix places variance values one and four on the diagonal and symmetric covariance one off the diagonal
Diagonal entries describe individual variables. The two off-diagonal entries describe the same pair.

How do you calculate the sample or population matrix?

Let column jj be one variable and row ii be one complete observation. Subtract the mean of column jj from each of its entries. That produces the centred matrix XcX_c. For n>1n>1 sample observations, use

sjk=∑i=1n(xij−x¯j)(xik−x¯k)n−1,S=XcTXcn−1.s_{jk}=\frac{\sum_{i=1}^n(x_{ij}-\bar x_j)(x_{ik}-\bar x_k)}{n-1}, \qquad S=\frac{X_c^{\mathsf T}X_c}{n-1}.

Each product in the numerator must come from the same row. If the observed rows describe a whole finite population, divide by the number of population rows NN. For an underlying random vector with finite second moments, Σjk=E[(Xj−μj)(Xk−μk)]\Sigma_{jk}=\operatorname{E}[(X_j-\mu_j)(X_k-\mu_k)], where μj=E[Xj]\mu_j=\operatorname{E}[X_j].

The variance guide shows what happens when j=kj=k. Then every product is a square, so the covariance of a variable with itself is its variance. NumPy uses n−1n-1 by default for unweighted sample covariance. With rows as observations, use np.cov(data, rowvar=False, ddof=1) to make the layout and denominator explicit.

How do you interpret covariance and correlation?

The matrix keeps the variables' original units. If XX is measured in metres and YY in seconds, then their covariance has units metre-seconds. Rewriting XX in centimetres multiplies its covariance with YY by 100 and its variance by 1002100^2. Pearson correlation divides each covariance by the product of the two positive standard deviations

rjk=sjksjjskk.r_{jk}=\frac{s_{jk}}{\sqrt{s_{jj}}\sqrt{s_{kk}}}.

The same positive unit conversion leaves this correlation unchanged. Correlations are dimensionless and range from −1-1 to 1. If a variable is constant, its variance and covariance entries are zero, but its correlation with another variable is undefined because the denominator is zero. The statistics practice guide explores what an observed relationship can support.

Why is a covariance matrix symmetric and positive semidefinite?

Swapping the order of two factors in each product leaves it unchanged, so sjk=skjs_{jk}=s_{kj}. To see why the matrix is positive semidefinite, take any vector of weights aa. The sample variance of the weighted combination is

aTSa=‖Xca‖2n−1≥0.a^{\mathsf T}Sa=\frac{\lVert X_ca\rVert^2}{n-1}\ge0.

The entries in each column of XcX_c sum to zero. Its rank is at most n−1n-1. If you have p≥np\ge n variables, the pp-by-pp sample covariance matrix is therefore singular. A linear dependency or constant variable can also make it singular even with a large sample. See the matrix rank guide for interpreting dependencies.

The same quadratic form explains a use in principal component analysis. After centring, directions along which the observations vary most are found from the covariance matrix's leading eigenvectors. Purdue's PCA material explains that link. Feature units can change which direction varies most, so decide whether to work with covariances or with standardised variables.

Worked Covariance Matrix Examples

Example 1

Find the sample covariance matrix for the paired observations (1,2)(1,2), (2,0)(2,0), and (3,4)(3,4).

  1. The column means are 2 and 2. The centred rows are (−1,0)(-1,0), (0,−2)(0,-2), and (1,2)(1,2).
  2. The sums of squared deviations are 22 and 88. The sum of cross-products is (−1)(0)+(0)(−2)+(1)(2)=2(-1)(0)+(0)(-2)+(1)(2)=2.
  3. Divide each by n−1=2n-1=2 to get S=(1114)S=\begin{pmatrix}1&1\\1&4\end{pmatrix}.

Example 2

Treat those three rows as the whole population. What changes?

  1. The centred rows and their products stay the same.
  2. Divide the sums of 2, 8, and 2 by 3.
  3. The population matrix is Σ=(2/32/32/38/3)\Sigma=\begin{pmatrix}2/3&2/3\\2/3&8/3\end{pmatrix}.

Example 3

Convert the sample matrix from Example 1 to a correlation matrix.

  1. The standard deviations are 1=1\sqrt1=1 and 4=2\sqrt4=2.
  2. Divide the covariance by their product to get 1/(1·2)=0.51/(1\cdot2)=0.5.
  3. The correlation matrix has 1 on the diagonal and 0.5 in both off-diagonal positions.

Example 4

What happens to the sample matrix if you multiply the second variable by 3?

  1. Its centred deviations are multiplied by 3.
  2. The second variance is multiplied by 32=93^2=9, and each covariance with the first variable is multiplied by 3.
  3. The matrix becomes (13336)\begin{pmatrix}1&3\\3&36\end{pmatrix}. The correlation is still 3/(1·6)=0.53/(1\cdot6)=0.5.

Example 5

Add a third variable Z=X+YZ=X+Y to the sample in Example 1. Find the new matrix entries.

  1. The sample covariance of XX and ZZ is the variance of XX plus the covariance of XX and YY, or 1+1=21+1=2.
  2. The covariance of YY and ZZ is 1+4=51+4=5. The variance of ZZ is 1+4+2(1)=71+4+2(1)=7.
  3. The three-variable sample matrix is (112145257)\begin{pmatrix}1&1&2\\1&4&5\\2&5&7\end{pmatrix}. The third column is the sum of the first two, so its rank is below 3.

Example 6

Find the sample covariance of X=(1,2,3)X=(1,2,3) and Y=(5,3,1)Y=(5,3,1).

  1. Their means are 2 and 3. The centred columns are (−1,0,1)(-1,0,1) and (2,0,−2)(2,0,-2).
  2. The sum of cross-products is −2+0−2=−4-2+0-2=-4. Dividing by 2 gives covariance −2-2.
  3. The variances are 1 and 4, so the correlation is −2/(1·2)=−1-2/(1\cdot2)=-1. Here the negative relationship is linear and exact.

Mistakes That Change a Covariance Matrix

Mistake 1: Pairing different observations

Keep rows aligned. If you sort one column independently, you destroy its original pairing with the other variables.

Mistake 2: Substituting a correlation of zero for an undefined one

Check each diagonal variance before standardising. A zero variance means the corresponding correlation divides by zero.

Mistake 3: Estimating each pair from a different subset without saying so

If some records are missing values, using every available pair can mean the entries come from different sets of observations. The result can fail to be positive semidefinite. State how missing values were handled and what observations contribute. Using only complete rows produces one common set for all entries, though it can also discard information.

Try It Yourself

  1. A covariance matrix has diagonal entries 9 and 16 and off-diagonal entry −6-6. Find the correlation.
  2. If a constant 10 is added to every XX value, what happens to its covariance with YY?
  3. Three observations are used to compute a sample covariance matrix for five variables. What is the largest possible rank?

Answers and Reasoning

  1. 9=3\sqrt9=3 and 16=4\sqrt{16}=4, so r=−6/(3·4)=−0.5r=-6/(3\cdot4)=-0.5.
  2. Nothing changes. Each XX value and the mean rise equally, leaving the centred deviations unchanged.
  3. The rank is at most 3−1=23-1=2. There are only two independent directions possible after centring three rows.

Questions About Covariance Matrices

Can variance and covariance be negative?

A diagonal variance is nonnegative because it averages squared deviations. An off-diagonal covariance can be negative, positive, or zero.

Can you calculate covariance with just one row?

You cannot estimate sample covariance with n=1n=1 using n−1n-1 as the denominator. A population consisting of one point has zero variance and covariance.

Conclusion

  • Centre each variable and pair deviations from the same observation.
  • Use their cross-products to form the matrix, with the correct denominator.
  • Read variance on the diagonal and use correlation when a unitless comparison is needed.

Read More

Research Behind This Article

  1. Purdue University, Data Mining and Machine Learning — Principal Component Analysis and Linear Algebra. Purdue
  2. NumPy, Covariance Matrix Estimation and Axis Conventions. NumPy reference
  3. CASRAI, Covariance Matrix: Formula, Example and Code. CASRAI
  4. Wiley, Multivariate Statistical Analysis — Chapter Excerpt. Wiley