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 complete observations in rows, centre each column and compute 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 and , the population matrix has the form
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.
How do you calculate the sample or population matrix?
Let column be one variable and row be one complete observation. Subtract the mean of column from each of its entries. That produces the centred matrix . For sample observations, use
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 . For an underlying random vector with finite second moments, , where .
The variance guide shows what happens when . Then every product is a square, so the covariance of a variable with itself is its variance. NumPy uses 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 is measured in metres and in seconds, then their covariance has units metre-seconds. Rewriting in centimetres multiplies its covariance with by 100 and its variance by . Pearson correlation divides each covariance by the product of the two positive standard deviations
The same positive unit conversion leaves this correlation unchanged. Correlations are dimensionless and range from 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 . To see why the matrix is positive semidefinite, take any vector of weights . The sample variance of the weighted combination is
The entries in each column of sum to zero. Its rank is at most . If you have variables, the -by- 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 , , and .
- The column means are 2 and 2. The centred rows are , , and .
- The sums of squared deviations are and . The sum of cross-products is .
- Divide each by to get .
Example 2
Treat those three rows as the whole population. What changes?
- The centred rows and their products stay the same.
- Divide the sums of 2, 8, and 2 by 3.
- The population matrix is .
Example 3
Convert the sample matrix from Example 1 to a correlation matrix.
- The standard deviations are and .
- Divide the covariance by their product to get .
- 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?
- Its centred deviations are multiplied by 3.
- The second variance is multiplied by , and each covariance with the first variable is multiplied by 3.
- The matrix becomes . The correlation is still .
Example 5
Add a third variable to the sample in Example 1. Find the new matrix entries.
- The sample covariance of and is the variance of plus the covariance of and , or .
- The covariance of and is . The variance of is .
- The three-variable sample matrix is . The third column is the sum of the first two, so its rank is below 3.
Example 6
Find the sample covariance of and .
- Their means are 2 and 3. The centred columns are and .
- The sum of cross-products is . Dividing by 2 gives covariance .
- The variances are 1 and 4, so the correlation is . 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
- A covariance matrix has diagonal entries 9 and 16 and off-diagonal entry . Find the correlation.
- If a constant 10 is added to every value, what happens to its covariance with ?
- Three observations are used to compute a sample covariance matrix for five variables. What is the largest possible rank?
Answers and Reasoning
- and , so .
- Nothing changes. Each value and the mean rise equally, leaving the centred deviations unchanged.
- The rank is at most . 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 using 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
- Calculate the inverse of a nonsingular matrix
Research Behind This Article
- Purdue University, Data Mining and Machine Learning — Principal Component Analysis and Linear Algebra. Purdue
- NumPy, Covariance Matrix Estimation and Axis Conventions. NumPy reference
- CASRAI, Covariance Matrix: Formula, Example and Code. CASRAI
- Wiley, Multivariate Statistical Analysis — Chapter Excerpt. Wiley