Dictionary of Applied Machine Learning: 151 machine-learning terms, each with a definition, a figure, citations and a Python demo · All terms · Demos · Start here
Updated on 2026-11-26
Readiness 90/100
See also covariance matrix matrix variance principal component analysis
The sample covariance matrix of a dataset is the covariance matrix of the random vector obtained by selecting one of its data points uniformly at random. Its diagonal entries are the sample variances of the individual features, and its off-diagonal entries are the covariances between different features. Two divisor conventions are in use: dividing the sum of outer products by the number $m$ of data points gives the covariance matrix under the empirical distribution, while dividing by $m-1$ gives an unbiased estimator of the covariance matrix of an underlying probability distribution. The two differ by the factor $m/(m-1)$ and therefore have the same eigenvectors, so a dimensionality reduction built on them does not depend on the choice.

samplecovmtx.py, which recomputes what the entry states: the script block by block · open in ColabB-kremsConsider a dataset $\mathcal{D}$ of $m$ data points, characterized by feature vectors ${\mathbf x}^{(1)}, \,\ldots, \,{\mathbf x}^{(m)} \in \mathbb{R}^{d}$. A running example is the dataset of the $1824$ days of the years $2020$ to $2024$ at Krems an der Donau (Austria) with both readings on record, each day characterized by its air temperature at $06{:}00$ and at $15{:}00$ (UTC). The sample covariance matrix records how much each feature varies across the data points and how strongly two features vary together, without assuming any probabilistic model behind the measurements.
The sample covariance matrix of $\mathcal{D}$ is defined as the
covariance matrix with respect to the empirical distribution
$\mathbb{P}^{(\mathcal{D})}$ induced by $\mathcal{D}$. It is given
explicitly by
\[\widehat{{\mathbf C}} = \frac{1}{m} \sum_{r=1}^{m} \big( {\mathbf x}^{(r)}\!-\!\widehat{\boldsymbol \mu} \big) \big( {\mathbf x}^{(r)}\!-\!\widehat{\boldsymbol \mu} \big)^{\top} \text{,}\]
where $\widehat{\boldsymbol \mu}$ denotes the sample mean of
$\mathcal{D}$. Equivalently, $\widehat{{\mathbf C}}$ is the
covariance matrix of the random vector obtained by selecting a
data point of $\mathcal{D}$ uniformly at random. For the
weather measurements, the two diagonal entries of
$\widehat{{\mathbf C}}$ are $63.01$ and $85.72$ squared degrees
Celsius and both off-diagonal entries are $65.40$
(pythondemos/samplecovmtx.py).
B-uniformTwo conventions for the divisor are in use. The definition above divides by $m$, which is what makes $\widehat{{\mathbf C}}$ the covariance matrix of a data point selected uniformly at random, and what the maximum likelihood method returns for a Gaussian probabilistic model (Bishop, 2006, Sect. 2.3.4). Many texts and software libraries divide by $m - 1$ instead. Under the independent and identically distributed assumption (i.i.d. assumption), with the data points modeled as realizations of independent and identically distributed (i.i.d.) random vectors, that second version is an unbiased estimator of the covariance matrix of the underlying probability distribution, while the version above is smaller on average by the factor $(m-1)/m$ (Bishop, 2006, Sect. 2.3.4). The reason is that $\widehat{\boldsymbol \mu}$ is computed from the same data points: the centered vectors sum to the zero vector, so only $m-1$ of them can be chosen freely.
B-smallThe two versions differ by the factor
$m/(m-1)$, so the choice matters only when
the dataset is small. For the $1824$ days it moves the
first diagonal entry from $63.01$ to $63.05$ squared degrees
Celsius. Fig. 1 shows what happens
for few data points: averaged over many datasets
drawn from the $1824$ days, the divisor $m$ gives
half the variance of the temperature at $06{:}00$ for two
data points and is still two percent below it for one
hundred, while the divisor $m-1$ sits on it
throughout.
pythondemos/samplecovmtx.py
B-axesIn machine learning (ML), the sample covariance matrix of the
feature vectors of a training set is what a method reads
when it compares the features with each other rather than
with the label. Principal component analysis (PCA) extracts the eigenvectors
of $\widehat{{\mathbf C}}$ belonging to the largest
eigenvalues and uses them as the new features (see
dimensionality reduction). The choice of divisor does not move these
directions: the two versions differ by a positive scalar factor,
so they have the same eigenvectors, and their
eigenvalues differ by that same factor
(pythondemos/samplecovmtx.py). With centered
features, $\widehat{{\mathbf C}}$ is also the matrix
$(1/m) {\mathbf X}^{\top} {\mathbf X}$ built from the
feature matrix ${\mathbf X}$, whose eigenvalues set the
condition number that controls how many steps gradient descent (GD) needs for the
empirical risk minimization (ERM) of linear regression (see condition number).
See also: covariance matrix, covariance, matrix, sample mean, sample variance, empirical distribution, variance, estimator, principal component analysis.
@misc{dictml_samplecovmtx,
author = {Jung, Alexander},
editor = {Olioumtsevits, Konstantina and Schnoor, Ekkehard},
title = {sample covariance matrix},
howpublished = {Dictionary of Applied Machine Learning (course edition)},
year = {2026},
doi = {10.5281/zenodo.21569296},
note = {ISBN 978-952-64-3013-3, CC BY 4.0, retrieved 2026-11-26},
url = {https://dictionaryofml.org/terms/samplecovmtx.html}
}