Covariance#Relationship to inner products
{{Short description|Measure of the joint variability}}
{{About|the degree to which random variables vary similarly}}
In probability theory and statistics, covariance is a measure of the joint variability of two random variables.{{Cite book | title=Mathematical Statistics and Data Analysis | last=Rice | first=John | publisher=Brooks/Cole Cengage Learning | year=2007 | isbn=9780534399429 | page=138}}
The sign of the covariance, therefore, shows the tendency in the linear relationship between the variables. If greater values of one variable mainly correspond with greater values of the other variable, and the same holds for lesser values (that is, the variables tend to show similar behavior), the covariance is positive.{{MathWorld | urlname=Covariance | title=Covariance}} In the opposite case, when greater values of one variable mainly correspond to lesser values of the other (that is, the variables tend to show opposite behavior), the covariance is negative. The magnitude of the covariance is the geometric mean of the variances that are in common for the two random variables. The correlation coefficient normalizes the covariance by dividing by the geometric mean of the total variances for the two random variables.
A distinction must be made between (1) the covariance of two random variables, which is a population parameter that can be seen as a property of the joint probability distribution, and (2) the sample covariance, which in addition to serving as a descriptor of the sample, also serves as an estimated value of the population parameter.
Definition
For two jointly distributed real-valued random variables and with finite second moments, the covariance is defined as the expected value (or mean) of the product of their deviations from their individual expected values:Oxford Dictionary of Statistics, Oxford University Press, 2002, p. 104.{{cite book | author=Park, Kun Il| title=Fundamentals of Probability and Stochastic Processes with Applications to Communications| publisher=Springer | year=2018 | isbn=9783319680743 }}{{rp|p = 119}}
where is the expected value of , also known as the mean of . The covariance is also sometimes denoted or , in analogy to variance. By using the linearity property of expectations, this can be simplified to the expected value of their product minus the product of their expected values:
\begin{align}
\operatorname{cov}(X, Y)
&= \operatorname{E}\left[\left(X - \operatorname{E}\left[X\right]\right) \left(Y - \operatorname{E}\left[Y\right]\right)\right] \\
&= \operatorname{E}\left[X Y - X \operatorname{E}\left[Y\right] - \operatorname{E}\left[X\right] Y + \operatorname{E}\left[X\right] \operatorname{E}\left[Y\right]\right] \\
&= \operatorname{E}\left[X Y\right] - \operatorname{E}\left[X\right] \operatorname{E}\left[Y\right] - \operatorname{E}\left[X\right] \operatorname{E}\left[Y\right] + \operatorname{E}\left[X\right] \operatorname{E}\left[Y\right] \\
&= \operatorname{E}\left[X Y\right] - \operatorname{E}\left[X\right] \operatorname{E}\left[Y\right].
\end{align}
This identity is useful for mathematical derivations. From the viewpoint of numerical computation, however, it is susceptible to catastrophic cancellation (see the section on numerical computation below).
The units of measurement of the covariance are those of times those of . By contrast, correlation coefficients, which depend on the covariance, are a dimensionless measure of linear dependence. (In fact, correlation coefficients can simply be understood as a normalized version of covariance.)
=Complex random variables=
{{main|Complex random variable#Covariance}}
The covariance between two complex random variables is defined as{{rp|p= 119}}
\operatorname{E}\left[(Z - \operatorname{E}[Z])\overline{(W - \operatorname{E}[W])}\right] =
\operatorname{E}\left[Z\overline{W}\right] - \operatorname{E}[Z]\operatorname{E}\left[\overline{W}\right]
Notice the complex conjugation of the second factor in the definition.
A related pseudo-covariance can also be defined.
=Discrete random variables=
If the (real) random variable pair can take on the values for , with equal probabilities , then the covariance can be equivalently written in terms of the means and as
It can also be equivalently expressed, without directly referring to the means, as{{cite conference |author=Yuli Zhang |author2=Huaiyu Wu |author3=Lei Cheng |title=Some new deformation formulas about variance and covariance|book-title=Proceedings of 4th International Conference on Modelling, Identification and Control(ICMIC2012)|date=June 2012 |pages=987–992}}
More generally, if there are possible realizations of , namely but with possibly unequal probabilities for , then the covariance is
In the case where two discrete random variables and have a joint probability distribution, represented by elements corresponding to the joint probabilities of , the covariance is calculated using a double summation over the indices of the matrix:
Examples
Consider three independent random variables and two constants .
\begin{align}
X &= qA + B \\
Y &= rA + C \\
\operatorname{cov}(X, Y)
&= qr \operatorname{var}(A)
\end{align}
In the special case, and , the covariance between and is just the variance of and the name covariance is entirely appropriate.
File:Covariance_geometric_visualisation.svg bounding box of its point {{nowrap|(x, y, f (x, y)),}} and the {{nowrap|X and Y means}} (magenta point). {{nowrap|The covariance}} is the sum of the volumes of the cuboids in the 1st and 3rd quadrants (red) and in the 2nd and 4th (blue).]]
Suppose that and have the following joint probability mass function,{{Cite web| url=https://onlinecourses.science.psu.edu/stat414/node/109| title=Covariance of X and Y {{!}} STAT 414/415|publisher=The Pennsylvania State University | access-date=August 4, 2019 | archive-url=https://web.archive.org/web/20170817034656/https://onlinecourses.science.psu.edu/stat414/node/109 | archive-date=August 17, 2017}} in which the six central cells give the discrete joint probabilities of the six hypothetical realizations {{nowrap|:}}
class="wikitable" style="text-align:center;"
!rowspan="2" colspan="2"| !colspan="3"|x |rowspan="6" style="padding:1px;"| !rowspan="2"| |
5
!6 !7 |
---|
rowspan="2"|y
!8 |0 |0.4 |0.1 |0.5 |
9
|0.3 |0 |0.2 |0.5 |
colspan="7" style="padding:1px;"| |
colspan="2"|
|0.3 |0.4 |0.3 |1 |
can take on three values (5, 6 and 7) while can take on two (8 and 9). Their means are and . Then,
\operatorname{cov}(X, Y)
={} &\sigma_{XY} = \sum_{(x,y)\in S}f(x, y) \left(x - \mu_X\right)\left(y - \mu_Y\right) \\[4pt]
={} &(0)(5 - 6)(8 - 8.5) + (0.4)(6 - 6)(8 - 8.5) + (0.1)(7 - 6)(8 - 8.5) +{} \\[4pt]
&(0.3)(5 - 6)(9 - 8.5) + (0)(6 - 6)(9 - 8.5) + (0.2)(7 - 6)(9 - 8.5) \\[4pt]
={} &{-0.1} \; .
\end{align}
Properties
=Covariance with itself=
The variance is a special case of the covariance in which the two variables are identical:{{rp|p=121}}
=Covariance of linear combinations=
If , , , and are real-valued random variables and are real-valued constants, then the following facts are a consequence of the definition of covariance:
\begin{align}
\operatorname{cov}(X, a) &= 0 \\
\operatorname{cov}(X, X) &= \operatorname{var}(X) \\
\operatorname{cov}(X, Y) &= \operatorname{cov}(Y, X) \\
\operatorname{cov}(aX, bY) &= ab\, \operatorname{cov}(X, Y) \\
\operatorname{cov}(X+a, Y+b) &= \operatorname{cov}(X, Y) \\
\operatorname{cov}(aX+bY, cW+dV) &= ac\,\operatorname{cov}(X,W)+ad\,\operatorname{cov}(X,V)+bc\,\operatorname{cov}(Y,W)+bd\,\operatorname{cov}(Y,V)
\end{align}
For a sequence of random variables in real-valued, and constants , we have
=Hoeffding's covariance identity=
A useful identity to compute the covariance between two random variables is the Hoeffding's covariance identity:{{cite book| last1=Papoulis| title=Probability, Random Variables and Stochastic Processes| date=1991| publisher=McGraw-Hill}}
where is the joint cumulative distribution function of the random vector and are the marginals.
= Relationship to inner products =
Many of the properties of covariance can be extracted elegantly by observing that it satisfies similar properties to those of an inner product:
- bilinear: for constants and and random variables
- symmetric:
- positive semi-definite: for all random variables , and implies that is constant almost surely.
In fact these properties imply that the covariance defines an inner product over the quotient vector space obtained by taking the subspace of random variables with finite second moment and identifying any two that differ by a constant. (This identification turns the positive semi-definiteness above into positive definiteness.) That quotient vector space is isomorphic to the subspace of random variables with finite second moment and mean zero; on that subspace, the covariance is exactly the L2 inner product of real-valued functions on the sample space.
As a result, for random variables with finite variance, the inequality
holds via the Cauchy–Schwarz inequality.
Proof: If , then it holds trivially. Otherwise, let random variable
Then we have
0 \le \sigma^2(Z)
&= \operatorname{cov}\left(
X - \frac{\operatorname{cov}(X, Y)}{\sigma^2(Y)} Y,\;
X - \frac{\operatorname{cov}(X, Y)}{\sigma^2(Y)} Y
\right) \\[12pt]
&= \sigma^2(X) - \frac{(\operatorname{cov}(X, Y))^2}{\sigma^2(Y)} \\
\implies (\operatorname{cov}(X, Y))^2 &\le \sigma^2(X)\sigma^2(Y) \\
\left|\operatorname{cov}(X, Y)\right| &\le \sqrt{\sigma^2(X)\sigma^2(Y)}
\end{align}
Calculating the sample covariance
{{further|Sample mean and sample covariance}}
The sample covariances among variables based on observations of each, drawn from an otherwise unobserved population, are given by the matrix with the entries
:
which is an estimate of the covariance between variable and variable .
The sample mean and the sample covariance matrix are unbiased estimates of the mean and the covariance matrix of the random vector , a vector whose jth element is one of the random variables. The reason the sample covariance matrix has in the denominator rather than is essentially that the population mean is not known and is replaced by the sample mean . If the population mean is known, the analogous unbiased estimate is given by
: .
Generalizations
= Auto-covariance matrix of real random vectors =
{{main|Auto-covariance matrix}}
For a vector of jointly distributed random variables with finite second moments, its auto-covariance matrix (also known as the variance–covariance matrix or simply the covariance matrix) (also denoted by or ) is defined as{{cite book |first=John A. |last=Gubner |year=2006 |title=Probability and Random Processes for Electrical and Computer Engineers |publisher=Cambridge University Press |isbn=978-0-521-86470-1}}{{rp|p=335}}
\operatorname{K}_\mathbf{XX} = \operatorname{cov}(\mathbf{X}, \mathbf{X})
&= \operatorname{E}\left[(\mathbf{X} - \operatorname{E}[\mathbf{X}]) (\mathbf{X} - \operatorname{E}[\mathbf{X}])^\mathrm{T}\right] \\
&= \operatorname{E}\left[\mathbf{XX}^\mathrm{T}\right] - \operatorname{E}[\mathbf{X}]\operatorname{E}[\mathbf{X}]^\mathrm{T}.
\end{align}
Let be a random vector with covariance matrix {{math|Σ}}, and let {{math|A}} be a matrix that can act on on the left. The covariance matrix of the matrix-vector product {{math|A X}} is:
\operatorname{cov}(\mathbf{AX},\mathbf{AX}) &=
\operatorname{E}\left[\mathbf{AX(A}\mathbf{X)}^\mathrm{T}\right] - \operatorname{E}[\mathbf{AX}] \operatorname{E}\left[(\mathbf{A}\mathbf{X})^\mathrm{T}\right] \\
&= \operatorname{E}\left[\mathbf{AXX}^\mathrm{T}\mathbf{A}^\mathrm{T}\right] - \operatorname{E}[\mathbf{AX}] \operatorname{E}\left[\mathbf{X}^\mathrm{T}\mathbf{A}^\mathrm{T}\right] \\
&= \mathbf{A}\operatorname{E}\left[\mathbf{XX}^\mathrm{T}\right]\mathbf{A}^\mathrm{T} - \mathbf{A}\operatorname{E}[\mathbf{X}] \operatorname{E}\left[\mathbf{X}^\mathrm{T}\right]\mathbf{A}^\mathrm{T} \\
&= \mathbf{A}\left(\operatorname{E}\left[\mathbf{XX}^\mathrm{T}\right] - \operatorname{E}[\mathbf{X}] \operatorname{E}\left[\mathbf{X}^\mathrm{T}\right]\right)\mathbf{A}^\mathrm{T} \\
&= \mathbf{A}\Sigma\mathbf{A}^\mathrm{T}.
\end{align}
This is a direct result of the linearity of expectation and is useful
when applying a linear transformation, such as a whitening transformation, to a vector.
= Cross-covariance matrix of real random vectors =
{{main|Cross-covariance matrix}}
For real random vectors and , the cross-covariance matrix is equal to{{rp|p=336}}
{{Equation box 1
|indent = :
|title =
|equation = {{NumBlk||
\operatorname{K}_{\mathbf{X}\mathbf{Y}} = \operatorname{cov}(\mathbf{X},\mathbf{Y})
&= \operatorname{E}\left[
(\mathbf{X} - \operatorname{E}[\mathbf{X}])
(\mathbf{Y} - \operatorname{E}[\mathbf{Y}])^\mathrm{T}
\right] \\
&= \operatorname{E}\left[\mathbf{X} \mathbf{Y}^\mathrm{T}\right] - \operatorname{E}[\mathbf{X}]\operatorname{E}[\mathbf{Y}]^\mathrm{T}
\end{align}|{{EquationRef|Eq.2}}}}
|cellpadding = 6
|border
|border colour = #0073CF
|background colour = #F5FFFA}}
where is the transpose of the vector (or matrix) .
The -th element of this matrix is equal to the covariance between the {{math|i}}-th scalar component of and the {{math|j}}-th scalar component of . In particular, is the transpose of .
= Cross-covariance sesquilinear form of random vectors in a real or complex Hilbert space=
More generally let and , be Hilbert spaces over or with anti linear in the first variable, and let be resp. valued random variables.
Then the covariance of and is the sesquilinear form on
(anti linear in the first variable) given by
\operatorname{K}_{X,Y}(h_1,h_2) = \operatorname{cov}(\mathbf{X},\mathbf{Y})(h_1,h_2) &=
\operatorname{E}\left[\langle h_1,(\mathbf{X} - \operatorname{E}[\mathbf{X}])\rangle_1\langle(\mathbf{Y} - \operatorname{E}[\mathbf{Y}]), h_2 \rangle_2\right] \\
&= \operatorname{E}[\langle h_1,\mathbf{X}\rangle_1\langle\mathbf{Y}, h_2 \rangle_2] - \operatorname{E}[\langle h,\mathbf{X} \rangle_1] \operatorname{E}[\langle \mathbf{Y},h_2 \rangle_2] \\
&= \langle h_1, \operatorname{E}\left[(\mathbf{X} - \operatorname{E}[\mathbf{X}])(\mathbf{Y} - \operatorname{E}[\mathbf{Y}])^\dagger \right]h_2 \rangle_1\\
&= \langle h_1, \left( \operatorname{E}[\mathbf{X}\mathbf{Y}^\dagger] - \operatorname{E}[\mathbf{X}]\operatorname{E}[\mathbf{Y}]^\dagger \right) h_2 \rangle_1\\
\end{align}
Numerical computation
{{main|Algorithms for calculating variance#Covariance}}
When , the equation is prone to catastrophic cancellation if and are not computed exactly and thus should be avoided in computer programs when the data has not been centered before.Donald E. Knuth (1998). The Art of Computer Programming, volume 2: Seminumerical Algorithms, 3rd edn., p. 232. Boston: Addison-Wesley. Numerically stable algorithms should be preferred in this case.{{Cite book|last1=Schubert|first1=Erich|last2=Gertz|first2=Michael|title=Proceedings of the 30th International Conference on Scientific and Statistical Database Management |chapter=Numerically stable parallel computation of (Co-)variance |date=2018|chapter-url=http://dl.acm.org/citation.cfm?doid=3221269.3223036|language=en|location=Bozen-Bolzano, Italy|publisher=ACM Press|pages=1–12|doi=10.1145/3221269.3223036|isbn=978-1-4503-6505-5|s2cid=49665540}}
Comments
The covariance is sometimes called a measure of "linear dependence" between the two random variables. That does not mean the same thing as in the context of linear algebra (see linear dependence). When the covariance is normalized, one obtains the Pearson correlation coefficient, which gives the goodness of the fit for the best possible linear function describing the relation between the variables. In this sense covariance is a linear gauge of dependence.
Applications
= In genetics and molecular biology =
Covariance is an important measure in biology. Certain sequences of DNA are conserved more than others among species, and thus to study secondary and tertiary structures of proteins, or of RNA structures, sequences are compared in closely related species. If sequence changes are found or no changes at all are found in noncoding RNA (such as microRNA), sequences are found to be necessary for common structural motifs, such as an RNA loop. In genetics, covariance serves a basis for computation of Genetic Relationship Matrix (GRM) (aka kinship matrix), enabling inference on population structure from sample with no known close relatives as well as inference on estimation of heritability of complex traits.
In the theory of evolution and natural selection, the price equation describes how a genetic trait changes in frequency over time. The equation uses a covariance between a trait and fitness, to give a mathematical description of evolution and natural selection. It provides a way to understand the effects that gene transmission and natural selection have on the proportion of genes within each new generation of a population.{{cite journal |last1= Price | first1=George |year=1970 |title=Selection and covariance |journal=Nature |volume=227 |issue=5257 |pages=520–521 | doi=10.1038/227520a0 |pmid=5428476| bibcode=1970Natur.227..520P | s2cid=4264723 }}{{cite journal |last1= Harman |first1=Oren |year=2020 | title=When science mirrors life: on the origins of the Price equation |publisher=royalsocietypublishing.org |journal= Philosophical Transactions of the Royal Society B: Biological Sciences|volume=375 |issue=1797 |pages=1–7 | doi=10.1098/rstb.2019.0352 |pmid=32146891 |pmc=7133509 |doi-access=free }}
=In financial economics=
Covariances play a key role in financial economics, especially in modern portfolio theory and in the capital asset pricing model. Covariances among various assets' returns are used to determine, under certain assumptions, the relative amounts of different assets that investors should (in a normative analysis) or are predicted to (in a positive analysis) choose to hold in a context of diversification.
=In meteorological and oceanographic data assimilation=
{{unsourced section|date=May 2025}}
The covariance matrix is important in estimating the initial conditions required for running weather forecast models, a procedure known as data assimilation. The "forecast error covariance matrix" is typically constructed between perturbations around a mean state (either a climatological or ensemble mean). The "observation error covariance matrix" is constructed to represent the magnitude of combined observational errors (on the diagonal) and the correlated errors between measurements (off the diagonal). This is an example of its widespread application to Kalman filtering and more general state estimation for time-varying systems.
=In micrometeorology =
The eddy covariance technique is a key atmospherics measurement technique where the covariance between instantaneous deviation in vertical wind speed from the mean value and instantaneous deviation in gas concentration is the basis for calculating the vertical turbulent fluxes.
=In signal processing=
The covariance matrix is used to capture the spectral variability of a signal.{{cite journal|last=Sahidullah|first=Md.|author2=Kinnunen, Tomi|title=Local spectral variability features for speaker verification|journal=Digital Signal Processing|date=March 2016|volume=50|pages=1–11|doi=10.1016/j.dsp.2015.10.011|bibcode=2016DSP....50....1S |url=https://erepo.uef.fi/handle/123456789/4375}}
=In statistics and image processing =
The covariance matrix is used in principal component analysis to reduce feature dimensionality in data preprocessing.
See also
{{Div col|colwidth=30em}}
- Algorithms for calculating covariance
- Analysis of covariance
- Autocovariance
- Covariance function
- Covariance matrix
- Covariance operator
- Distance covariance, or Brownian covariance.
- Law of total covariance
- Propagation of uncertainty
{{Div col end}}