AI Glossary / Statistics and probability / Intermediate

Sample covariance matrix

Definition

A sample covariance matrix is a square table describing the estimated variances and pairwise covariances of several variables measured on the same observations. With n complete observations, the usual unbiased version averages centered outer products using denominator n−1. Its diagonal contains sample variances; its off-diagonal entries describe how pairs of variables vary together.

What it means

A single variance describes one variable's spread. A covariance matrix extends that view to a collection of variables, showing both individual variation and patterns of joint movement.
For p variables it has p rows and p columns, regardless of how many observations were collected. Covariance depends on measurement units, so a large numerical entry does not by itself establish a stronger relationship than a smaller entry measured on another scale. A correlation matrix standardizes this comparison differently.

How it works

Arrange the data so that each observation has the same ordered set of variables. Decide whether rows or columns represent observations; software conventions differ. Misinterpreting this orientation can produce a matrix of the wrong quantities while still yielding plausible-looking numbers.
Calculate the sample mean of each variable and subtract it from that variable's observations. For variables j and k, sum the products of their centered values and divide by n−1. The result is the estimated covariance S[j,k].
In matrix notation with observations in rows, let Xc be the centered data matrix. Then S = Xc-transpose times Xc divided by n−1. This calculation requires at least two observations for the usual unweighted estimator.
The matrix is symmetric because the product of two centered values is unchanged when their order is reversed. The diagonal entries are nonnegative. For complete data and a consistent construction, S is positive semidefinite: every linear combination of variables has a nonnegative estimated variance.
The n−1 normalization is unbiased for the population covariance matrix under independent identically distributed observations with finite second moments. A denominator of n has other uses, including the Gaussian maximum-likelihood estimate when the mean is estimated. State the convention rather than assuming every software output agrees.
Check missing-data handling. Pairwise deletion can use different observation sets for different entries and may produce a matrix that is not positive semidefinite. Complete-case analysis, imputation, and model-based approaches involve different assumptions and tradeoffs.
Assess dimensionality and numerical stability. A centered matrix based on n observations has rank at most n−1. When p is at least n, the ordinary sample covariance matrix is necessarily singular; near-collinearity can make inversion unstable even with more observations.
Use regularization or a suitably justified alternative when needed. Shrinkage can improve stability, and robust covariance methods address particular contamination concerns. These choices alter the estimator and should be reported. Neither a covariance matrix nor its inverse automatically establishes a causal relationship.

Three practical examples

  • AI preprocessing: analysts inspect joint feature variation before choosing a scaling or dimensionality-reduction approach.
  • Multivariate monitoring: engineers assess whether several sensor readings change together.
  • Simulation: researchers estimate a dependence structure for a model, checking whether the sample and assumptions support its intended use.

Think of it this way

Think of a table that records how much each instrument varies and how pairs move together. The diagonal describes each instrument alone; the mirrored off-diagonal cells describe pairwise movement. It is a compact summary of second-order relationships, not a complete recording of every possible dependency.

Worked example

Three fictional observations contain two variables: (1,2), (2,1), and (3,6). The sample means are 2 and 3. Centered pairs are (−1,−1), (0,−2), and (1,3).
The first variable's sum of squared deviations is 2, so its sample variance is 2/(3−1) = 1. The second's squared deviations total 14, giving variance 7. The cross-products total 1+0+3 = 4, giving covariance 2.
The sample covariance matrix is therefore [[1,2],[2,7]]. Its determinant is 1×7−2² = 3, and it is symmetric.
As an independent check, the sum variable has observations 3, 3, and 9, whose sample variance is 12. The matrix gives the same result: 1+7+2×2 = 12. This tiny dataset illustrates the calculation without establishing a dependable population estimate.

Illustrative case study

Fictional case: Elmstone Systems builds an anomaly detector using many sensor features from a small calibration dataset. An attempted covariance inversion becomes unstable because several features are redundant and the observation count is low.
The team checks rank, measurement units, and feature definitions before selecting a regularized estimator. It evaluates the revised detector on separate operating periods rather than interpreting a numerically invertible matrix as proof of reliable anomaly detection.

Common misconceptions and mistakes

Zero covariance does not generally imply independence; nonlinear relationships can remain.
Positive covariance does not prove that one variable causes another.
Covariance entries have units formed from the product of the two variables' units, and diagonal entries have squared units.
Symmetry alone does not guarantee a valid covariance matrix; positive semidefiniteness also matters.
More columns than observations can make the ordinary estimator singular even if no two individual columns look identical.

Business use

Use a sample covariance matrix when a decision or model needs a joint summary of variability. Record variable order, units, normalization, weights, and missing-data handling so results can be reproduced. In AI workflows, estimate preprocessing quantities on the training data where appropriate and avoid allowing evaluation data to influence model preparation.

People also ask

How large is the matrix for five variables?

Five by five.

Is it the same as a correlation matrix?

No; correlation standardizes covariance using standard deviations.

Must it be invertible?

No; singularity is common when data are limited or variables are linearly dependent.

Check your understanding

What is found on the diagonal?

Answer: Sample variances.

Why do S[1,2] and S[2,1] agree?

Answer: They average the same products.

In the example, why is the variance of the sum 12 rather than 8?

Answer: The covariance contributes twice, adding 4 to the two variances.

Related terms

  • Covariance
  • Sample variance
  • Correlation matrix
  • Principal component analysis
  • Shrinkage
  • Multicollinearity
  • positive semidefinite matrix

Learn next

Compute a two-variable covariance matrix by hand and compare it with software after confirming orientation and denominator. Then examine how changing one variable's units changes the matrix.

  • Covariance matrix

More in Statistics and probability

  • Sample - A sample is a collection of units or observations selected from a population or generated by a specified data-collection process.
  • Sample augmentation - Sample augmentation, in survey statistics, means maintaining and adding units to an existing sample so that it continues to cover the evolving population...
  • Sample complexity - Sample complexity describes how many observations a learning or estimation procedure needs to meet a specified performance criterion under stated assumptions.
  • Sample frame - A sample frame, more commonly called a sampling frame, is the list, map, register, or other operational device used to identify and access units for sample...
  • Sample mean - The sample mean is the arithmetic average of the observed values of a numeric variable.
  • Sample ratio mismatch - Sample ratio mismatch, abbreviated SRM, is a statistically significant discrepancy between the observed numbers of experimental units in treatment groups...

Sources

NumPy, numpy.cov primary documentation: https://numpy.org/doc/stable/reference/generated/numpy.cov.html The paired-data example, matrix checks, and fictional sensor case are original illustrations.

Previous: Sample complexity Next: Sample frame

  • Home
  • Daily AI Updates
  • Blog
  • AIFBL Academy
  • AI Glossary
  • Products and Tools
  • Store
  • Free CFO AI Prompt Pack
  • About Shihan Sheriff
  • Privacy Policy
  • Terms of Use
  • Refund Policy
  • Cookie Policy
  • Earnings Disclaimer