Written and maintained by CASRAI Editorial Board
Last updated
A covariance matrix (also known as a variance-covariance or dispersion matrix) is a square, symmetric mathematical matrix that displays the pairwise variances and covariances across a multivariate set of variables. In research methodology and quantitative data analysis, it serves as the foundational data structure for multivariate statistical techniques, including principal component analysis (PCA), factor analysis, multivariate analysis of variance (MANOVA), and structural equation modeling (SEM). The diagonal elements capture the variance of each individual variable, while the off-diagonal elements quantify the linear co-movement between pairs of variables. Because the linear association between variable i and variable j is identical to the association between variable j and variable i, the matrix is symmetric across its main diagonal.
Definition in One Paragraph
What is a covariance matrix? A covariance matrix is a p × p symmetric matrix that summarizes the joint dispersion of p random variables, where each diagonal entry sii represents the variance of variable i and each off-diagonal entry sij represents the covariance between variables i and j. Defined formally in multivariate analysis (NIST/SEMATECH e-Handbook §6.4.5.1; Johnson & Wichern, 2007), it encapsulates individual variable spread and joint linear relationships, providing the essential mathematical input required to calculate correlation matrices, rotate multidimensional coordinates, and evaluate multivariate distributions.
Formula With Every Symbol Explained
When working with empirical research data, analysts distinguish between the theoretical population covariance matrix and the sample covariance matrix calculated from observed measurements.
Population Covariance Matrix (Σ)
For a random vector X = [X1, X2, …, Xp]T with population mean vector μ = E[X], the population covariance matrix Σ (Greek capital sigma) is defined as:
Σ = E[(X − μ)(X − μ)T]
For any individual element located at row i and column j:
σij = E[(Xi − μi)(Xj − μj)]
- Σ: The p × p population covariance matrix.
- E: The mathematical expectation operator.
- X: p-dimensional column vector of random variables.
- μ: p-dimensional population mean vector, where μi = E[Xi].
- (X − μ)T: The transpose of the mean-deviation vector.
- σii = σi2: The population variance of variable i along the diagonal.
- σij = σji: The population covariance between variables i and j on the off-diagonals.
Sample Covariance Matrix (S)
In observational studies and experimental trials, researchers estimate the population parameters using an unbiased sample covariance matrix S. Given a dataset of n independent observations measured across p continuous variables, the sample covariance matrix is computed as:
S = (1 / (n − 1)) ∑k=1n (xk − x̄)(xk − x̄)T
In matrix notation, let X be the n × p data matrix and Xc be the n × p mean-centered data matrix where each column has mean zero. Then S is calculated as an outer product:
S = (1 / (n − 1)) XcT Xc
- S: The p × p sample covariance matrix.
- n: Sample size (number of observed cases or participants).
- n − 1: Degrees of freedom (Bessel’s correction), guaranteeing that S is an unbiased estimator of Σ.
- xk: The p × 1 observation vector for case k.
- x̄: The p × 1 sample mean vector across all n observations.
- Xc: The n × p mean-centered data matrix.
- sij: The sample covariance between variables i and j. For scalar bivariate calculations, see the covariance formula guide.
Worked Example With Real Numbers and Arithmetic Shown
Consider an educational research dataset measuring p = 3 variables across n = 5 graduate students:
- X1 (Study Hours): Weekly hours devoted to study.
- X2 (Practice Tests): Number of diagnostic mock exams completed.
- X3 (Exam Score): Final examination score (0–100 scale).
Step 1: Raw Data and Mean Vector
The raw observations and their marginal means are:
| Student | X1 (Hours) | X2 (Tests) | X3 (Score) |
|---|---|---|---|
| 1 | 4 | 2 | 60 |
| 2 | 6 | 4 | 70 |
| 3 | 8 | 5 | 80 |
| 4 | 10 | 5 | 85 |
| 5 | 12 | 9 | 95 |
| Mean (x̄) | 8.0 | 5.0 | 78.0 |
The sample mean vector is x̄ = [8.0, 5.0, 78.0]T.
Step 2: Mean-Centered Deviations (Xc)
Subtracting column means from each observation gives the centered deviations (xki − x̄i):
- Student 1: [4 − 8 = −4, 2 − 5 = −3, 60 − 78 = −18]
- Student 2: [6 − 8 = −2, 4 − 5 = −1, 70 − 78 = −8]
- Student 3: [8 − 8 = 0, 5 − 5 = 0, 80 − 78 = +2]
- Student 4: [10 − 8 = +2, 5 − 5 = 0, 85 − 78 = +7]
- Student 5: [12 − 8 = +4, 9 − 5 = +4, 95 − 78 = +17]
Step 3: Sums of Squares and Cross-Products (XcT Xc)
Computing the sum of squared deviations for each variable yields the diagonal terms:
- SS(X1) = (−4)2 + (−2)2 + 02 + 22 + 42 = 16 + 4 + 0 + 4 + 16 = 40
- SS(X2) = (−3)2 + (−1)2 + 02 + 02 + 42 = 9 + 1 + 0 + 0 + 16 = 26
- SS(X3) = (−18)2 + (−8)2 + 22 + 72 + 172 = 324 + 64 + 4 + 49 + 289 = 730
Computing the cross-products between pairs of variables yields the off-diagonal terms:
- CP(X1, X2) = (−4)(−3) + (−2)(−1) + (0)(0) + (2)(0) + (4)(4) = 12 + 2 + 0 + 0 + 16 = 30
- CP(X1, X3) = (−4)(−18) + (−2)(−8) + (0)(2) + (2)(7) + (4)(17) = 72 + 16 + 0 + 14 + 68 = 170
- CP(X2, X3) = (−3)(−18) + (−1)(−8) + (0)(2) + (0)(7) + (4)(17) = 54 + 8 + 0 + 0 + 68 = 130
Step 4: Division by Degrees of Freedom (n − 1 = 4)
Dividing each sum by n − 1 = 4 produces the unbiased sample variances and covariances:
- s11 = 40 / 4 = 10.0
- s22 = 26 / 4 = 6.5
- s33 = 730 / 4 = 182.5
- s12 = s21 = 30 / 4 = 7.5
- s13 = s31 = 170 / 4 = 42.5
- s23 = s32 = 130 / 4 = 32.5
The resulting 3 × 3 sample covariance matrix S is:
| Variable | X1 (Study Hours) | X2 (Practice Tests) | X3 (Exam Score) |
|---|---|---|---|
| X1 (Study Hours) | 10.0 | 7.5 | 42.5 |
| X2 (Practice Tests) | 7.5 | 6.5 | 32.5 |
| X3 (Exam Score) | 42.5 | 32.5 | 182.5 |
Interpretation
Interpreting a covariance matrix involves evaluating both the individual variances on the main diagonal and the joint covariances on the off-diagonals.
| Element | Mathematical Property | Result in Example | Research Interpretation |
|---|---|---|---|
| Diagonal (i = j) | Sample variance sii ≥ 0 | s11 = 10.0, s22 = 6.5, s33 = 182.5 | Reflects individual variable spread. Study hours has variance 10.0 (SD = 3.16 hours); exam score has variance 182.5 (SD = 13.51 points). |
| Off-Diagonal (i ≠ j) > 0 | Positive covariance | s12 = 7.5, s13 = 42.5, s23 = 32.5 | Positive co-movement. Students completing more study hours also complete more mock tests and achieve higher exam scores. |
| Off-Diagonal (i ≠ j) < 0 | Negative covariance | None observed | Inverse linear co-movement. Higher values on one variable pair with lower values on the other. |
| Off-Diagonal (i ≠ j) = 0 | Zero covariance | None observed | No linear relationship. Variables fluctuate independently of one another. |
| Symmetry | sij = sji | s13 = s31 = 42.5 | Covariance is non-directional; joint variation of X with Y equals that of Y with X. |
| Positive Semi-Definite | All eigenvalues λi ≥ 0 | λ1 = 198.21, λ2 = 0.70, λ3 = 0.09 | Ensures that any linear composite of the variables has non-negative variance, allowing matrix inversion and Cholesky factorization. |
Standardized Correlation Matrix (R)
Because raw covariances depend on the measurement scales of the variables, researchers convert S to a correlation matrix R using the diagonal standard deviation matrix D = diag(s111/2, s221/2, s331/2):
R = D−1 S D−1
Dividing each off-diagonal covariance by the product of its marginal standard deviations yields standardized Pearson correlations: r12 = 7.5 / (3.162 × 2.550) = 0.930; r13 = 42.5 / (3.162 × 13.509) = 0.995; and r23 = 32.5 / (2.550 × 13.509) = 0.944.
Assumptions and Common Mistakes
Applying covariance matrices in empirical research requires addressing several methodological considerations:
1. Missing Data Handling: Listwise vs. Pairwise Deletion
Missing observations are common in survey and clinical data. Computing covariances using pairwise deletion evaluates each cell sij using only cases observed on both variable i and variable j. As analyzed in the listwise vs pairwise deletion guide, pairwise deletion often produces a matrix that is not positive semi-definite (containing negative eigenvalues). Downstream methods like structural equation modeling or factor analysis will crash or fail to converge. Researchers should use listwise deletion or model-based full-information maximum likelihood (FIML).
2. High-Dimensional Singularity (n < p)
When the number of variables p exceeds the sample size n, the sample covariance matrix has mathematical rank of at most n − 1. In this setting, S is singular and non-invertible. Multivariate techniques requiring the inverse covariance matrix (such as MANOVA, discriminant analysis, and Hotelling’s T2 tests) cannot run without regularization or shrinkage estimators (such as Ledoit-Wolf shrinkage).
3. Scale Dependence
Covariance magnitudes reflect the original units of measurement. An unstandardized covariance of 42.5 does not imply a stronger relationship than a covariance of 7.5 if the underlying variables use different metrics. Analysts should convert to correlation coefficients when comparing relationship strengths across different domains.
How to Report It (APA Example)
Under American Psychological Association style (APA 7th edition, Section 7.15), covariance and correlation matrices are presented in a structured table. A common convention places sample variances on the diagonal, covariances in the upper triangle above the diagonal, and standardized correlations in the lower triangle below the diagonal.
Sample APA Table
Table 1
Means, Standard Deviations, and Variance-Covariance Matrix for Study Variables
| Variable | M | SD | 1 | 2 | 3 |
|---|---|---|---|---|---|
| 1. Study Hours | 8.00 | 3.16 | 10.00 | 7.50 | 42.50 |
| 2. Practice Tests | 5.00 | 2.55 | .93** | 6.50 | 32.50 |
| 3. Exam Score | 78.00 | 13.51 | .99** | .94** | 182.50 |
Note. N = 5. Variances appear on the diagonal; covariances appear in the upper triangle above the diagonal; bivariate Pearson correlation coefficients appear in the lower triangle below the diagonal. In latent variable and factor modeling, hypothesized constructs are evaluated against observed covariance matrices, as described in our review of confirmatory vs exploratory factor analysis.
** p < .01.
Narrative Text Example
“Multivariate normality was examined, and the sample variance-covariance matrix was calculated (see Table 1). Study hours exhibited substantial positive covariance with exam scores (s13 = 42.50, r = .99), and the matrix was confirmed to be positive definite.”
How to Calculate It in Excel, R or Python
Sample covariance matrices can be calculated using built-in functions across standard scientific software tools.
Python Snippet
In Python, NumPy calculates the covariance matrix using numpy.cov. Setting rowvar=False ensures columns represent variables. In parameter estimation and optimization workflows, residual covariance matrices can also be retrieved from Jacobian approximations in SciPy least_squares or state estimation in a Kalman filter.
import numpy as np
# Tabular data: 5 rows (observations), 3 columns (variables)
X = np.array([
[4, 2, 60],
[6, 4, 70],
[8, 5, 80],
[10, 5, 85],
[12, 9, 95]
], dtype=float)
# Calculate sample covariance matrix (rowvar=False for column variables)
cov_matrix = np.cov(X, rowvar=False)
print("Sample Covariance Matrix (S):")
print(np.round(cov_matrix, 2))
R Snippet
In R, the base stats::cov function estimates the covariance matrix. Setting use = "complete.obs" enforces listwise deletion.
# Define data frame of observations
df <- data.frame(
hours = c(4, 6, 8, 10, 12),
tests = c(2, 4, 5, 5, 9),
score = c(60, 70, 80, 85, 95)
)
# Compute sample covariance matrix with complete observations
cov_matrix <- cov(df, use = "complete.obs")
print("Sample Covariance Matrix (S):")
print(round(cov_matrix, 2))
Excel Instructions
To calculate covariances in Microsoft Excel:
- Formulas: Use
=COVARIANCE.S(array1, array2)for pairwise off-diagonal covariances and=VAR.S(array)for diagonal variances. - Data Analysis Toolpak: On the Data tab, select Data Analysis, then choose Covariance. Important: Excel’s Data Analysis Toolpak computes population covariance (dividing by N). To convert to sample covariance, multiply the output by N / (N − 1).
Frequently Asked Questions
What is a covariance matrix?
A covariance matrix is a square, symmetric mathematical table of dimensions p × p that summarizes the variances and covariances of p variables. The diagonal entries show each variable’s variance (dispersion around its mean), and the off-diagonal entries show the directional linear co-movement between pairs of variables.
What is the difference between a covariance matrix and a correlation matrix?
A covariance matrix expresses joint variability in raw measurement units (unstandardized). In contrast, a correlation matrix is standardized: every diagonal value equals 1.0, and every off-diagonal value is a Pearson correlation coefficient bounded between −1.0 and +1.0.
Why must a covariance matrix be positive semi-definite?
A covariance matrix must be positive semi-definite (possessing no negative eigenvalues) because the variance of any linear composite of variables must be non-negative. If a matrix had negative eigenvalues, a linear combination of those variables would yield negative variance, which is mathematically impossible. A non-positive semi-definite matrix causes multivariate software to fail during inversion or decomposition.
What happens if the sample size n is smaller than the number of variables p?
When the sample size n is smaller than or equal to the number of variables p, the sample covariance matrix has mathematical rank of at most n − 1 and is singular. This means the matrix cannot be inverted, preventing the execution of standard multivariate procedures like MANOVA and linear discriminant analysis. Researchers resolve this using regularized or shrinkage covariance estimation techniques.
Can values in a covariance matrix be negative?
Off-diagonal elements (covariances) can be negative, zero, or positive, depending on whether two variables vary inversely, independently, or directly together. However, diagonal elements (variances) represent squared deviations and can never be negative; they are strictly non-negative (sii ≥ 0).
Sources
- NIST/SEMATECH. (2012). e-Handbook of Statistical Methods. Section 6.4.5.1: Mean Vector and Covariance Matrix. National Institute of Standards and Technology. https://www.itl.nist.gov/div898/handbook/pmc/section4/pmc451.htm (Accessed 2026-11-07).
- Johnson, R. A., & Wichern, D. W. (2007). Applied Multivariate Statistical Analysis (6th ed.). Pearson Prentice Hall.
- NumPy Developers. (2026). numpy.cov Reference Manual (v2.0). NumPy Documentation. https://numpy.org/doc/stable/reference/generated/numpy.cov.html (Accessed 2026-11-07).
- R Core Team. (2026). stats::cov: Covariance, Correlation and Covariance (or Correlation) Matrices. R Manuals. The R Foundation for Statistical Computing. https://stat.ethz.ch/R-manual/R-patched/library/stats/html/cor.html (Accessed 2026-11-07).
- American Psychological Association. (2020). Publication Manual of the American Psychological Association (7th ed.). Section 7.15: Correlation and Covariance Matrices. American Psychological Association.







