[Home](https://learningfolio.de5.net/) / [Math](https://learningfolio.de5.net/math/) / [Math glossary](https://learningfolio.de5.net/math/glossary/) / Covariance Matrix: Formula, Interpretation, and Examples

[Math glossary](https://learningfolio.de5.net/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.

Written by **Learningfolio Team**

We’re subject-matter experts in math, science, parent support, and education, with 100 years of combined experience. We believe in learning, experimenting, and growing, and we review one another’s work.

## On this page

* [TL;DR](https://learningfolio.de5.net/math/covariance-matrix/#tldr)
* [Key Points](https://learningfolio.de5.net/math/covariance-matrix/#key-points)
* [What is a covariance matrix?](https://learningfolio.de5.net/math/covariance-matrix/#what-is-a-covariance-matrix)
* [How do you calculate the sample or population matrix?](https://learningfolio.de5.net/math/covariance-matrix/#how-do-you-calculate-the-sample-or-population-matrix)
* [How do you interpret covariance and correlation?](https://learningfolio.de5.net/math/covariance-matrix/#how-do-you-interpret-covariance-and-correlation)
* [Why is a covariance matrix symmetric and positive semidefinite?](https://learningfolio.de5.net/math/covariance-matrix/#why-is-a-covariance-matrix-symmetric-and-positive-semidefinite)
* [Worked Covariance Matrix Examples](https://learningfolio.de5.net/math/covariance-matrix/#worked-covariance-matrix-examples)
* [Mistakes That Change a Covariance Matrix](https://learningfolio.de5.net/math/covariance-matrix/#mistakes-that-change-a-covariance-matrix)
* [Try It Yourself](https://learningfolio.de5.net/math/covariance-matrix/#try-it-yourself)
* [Questions About Covariance Matrices](https://learningfolio.de5.net/math/covariance-matrix/#questions-about-covariance-matrices)
* [Conclusion](https://learningfolio.de5.net/math/covariance-matrix/#conclusion)
* [Read More](https://learningfolio.de5.net/math/covariance-matrix/#read-more)
* [Research Behind This Article](https://learningfolio.de5.net/math/covariance-matrix/#research-behind-this-article)

## TL;DR

**A covariance matrix stores variances on its diagonal and covariances elsewhere.** For $n$ complete observations in rows, centre each column and compute $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 $X$ and $Y$, the population matrix has the form

$$
\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](https://learningfolio.de5.net/assets/versioned/covariance-matrix-variances-and-covariances.f71ba87b68f5.webp)


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 $j$ be one variable and row $i$ be one complete observation. Subtract the mean of column $j$ from each of its entries. That produces the centred matrix $X_c$. For $n>1$ sample observations, use

$$
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 $N$. For an underlying random vector with finite second moments, $\Sigma_{jk}=\operatorname{E}[(X_j-\mu_j)(X_k-\mu_k)]$, where $\mu_j=\operatorname{E}[X_j]$.

The [variance guide](https://learningfolio.de5.net/math/variance/) shows what happens when $j=k$. Then every product is a square, so the covariance of a variable with itself is its variance. NumPy uses $n-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 $X$ is measured in metres and $Y$ in seconds, then their covariance has units metre-seconds. Rewriting $X$ in centimetres multiplies its covariance with $Y$ by 100 and its variance by $100^2$. Pearson correlation divides each covariance by the product of the two positive standard deviations

$$
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$ 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](https://learningfolio.de5.net/math/statistics-questions/) 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 $s_{jk}=s_{kj}$. To see why the matrix is positive semidefinite, take any vector of weights $a$. The sample variance of the weighted combination is

$$
a^{\mathsf T}Sa=\frac{\lVert X_ca\rVert^2}{n-1}\ge0.
$$

The entries in each column of $X_c$ sum to zero. Its rank is at most $n-1$. If you have $p\ge n$ variables, the $p$-by-$p$ 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](https://learningfolio.de5.net/math/rank-of-matrix/) 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)$, $(2,0)$, and $(3,4)$.

1. The column means are 2 and 2. The centred rows are $(-1,0)$, $(0,-2)$, and $(1,2)$.
2. The sums of squared deviations are $2$ and $8$. The sum of cross-products is $(-1)(0)+(0)(-2)+(1)(2)=2$.
3. Divide each by $n-1=2$ to get $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 $\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 $\sqrt1=1$ and $\sqrt4=2$.
2. Divide the covariance by their product to get $1/(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 $3^2=9$, and each covariance with the first variable is multiplied by 3.
3. The matrix becomes $\begin{pmatrix}1&3\\3&36\end{pmatrix}$. The correlation is still $3/(1\cdot6)=0.5$.

### Example 5

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

1. The sample covariance of $X$ and $Z$ is the variance of $X$ plus the covariance of $X$ and $Y$, or $1+1=2$.
2. The covariance of $Y$ and $Z$ is $1+4=5$. The variance of $Z$ is $1+4+2(1)=7$.
3. The three-variable sample matrix is $\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)$ and $Y=(5,3,1)$.

1. Their means are 2 and 3. The centred columns are $(-1,0,1)$ and $(2,0,-2)$.
2. The sum of cross-products is $-2+0-2=-4$. Dividing by 2 gives covariance $-2$.
3. The variances are 1 and 4, so the correlation is $-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$. Find the correlation.
2. If a constant 10 is added to every $X$ value, what happens to its covariance with $Y$?
3. Three observations are used to compute a sample covariance matrix for five variables. What is the largest possible rank?

### Answers and Reasoning

1. $\sqrt9=3$ and $\sqrt{16}=4$, so $r=-6/(3\cdot4)=-0.5$.
2. Nothing changes. Each $X$ value and the mean rise equally, leaving the centred deviations unchanged.
3. The rank is at most $3-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=1$ using $n-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

* [Standardise values and read standard normal probabilities](https://learningfolio.de5.net/math/z-score-table/)
* [Calculate the inverse of a nonsingular matrix](https://learningfolio.de5.net/math/inverse-of-matrix/)

## Research Behind This Article

1. Purdue University, *Data Mining and Machine Learning — Principal Component Analysis and Linear Algebra*. [Purdue](https://www.cs.purdue.edu/homes/ribeirob/courses/Fall2025/lectures/Week12/PCA_linear_algebra.html)
2. NumPy, *Covariance Matrix Estimation and Axis Conventions*. [NumPy reference](https://numpy.org/doc/stable/reference/generated/numpy.cov.html)
3. CASRAI, *Covariance Matrix: Formula, Example and Code*. [CASRAI](https://casrai.org/guides/covariance-matrix)
4. Wiley, *Multivariate Statistical Analysis — Chapter Excerpt*. [Wiley](https://catalogimages.wiley.com/images/db/pdf/9781118738023.excerpt.pdf)
