# Estimation of covariance matrices

In statistics, sometimes the covariance matrix of a multivariate random variable is not known but has to be estimated. Estimation of covariance matrices then deals with the question of how to approximate the actual covariance matrix on the basis of a sample from the multivariate distribution. Simple cases, where observations are complete, can be dealt with by using the sample covariance matrix. The sample covariance matrix (SCM) is an unbiased and efficient estimator of the covariance matrix if the space of covariance matrices is viewed as an extrinsic convex cone in Rp×p; however, measured using the intrinsic geometry of positive-definite matrices, the SCM is a biased and inefficient estimator.[1] In addition, if the random variable has normal distribution, the sample covariance matrix has Wishart distribution and a slightly differently scaled version of it is the maximum likelihood estimate. Cases involving missing data require deeper considerations. Another issue is the robustness to outliers, to which sample covariance matrices are highly sensitive.[2][3][4]

Statistical analyses of multivariate data often involve exploratory studies of the way in which the variables change in relation to one another and this may be followed up by explicit statistical models involving the covariance matrix of the variables. Thus the estimation of covariance matrices directly from observational data plays two roles:

• to provide initial estimates that can be used to study the inter-relationships;
• to provide sample estimates that can be used for model checking.

Estimates of covariance matrices are required at the initial stages of principal component analysis and factor analysis, and are also involved in versions of regression analysis that treat the dependent variables in a data-set, jointly with the independent variable as the outcome of a random sample.

## Estimation in a general context

Given a sample consisting of n independent observations x1,..., xn of a p-dimensional random vector xRp×1 (a p×1 column-vector), an unbiased estimator of the (p×p) covariance matrix

${\displaystyle \operatorname {\Sigma } =\operatorname {E} \left[\left(X-\operatorname {E} [X]\right)\left(X-\operatorname {E} [X]\right)^{\mathrm {T} }\right]}$

is the sample covariance matrix

${\displaystyle \mathbf {Q} ={1 \over {n-1}}\sum _{i=1}^{n}(x_{i}-{\overline {x}})(x_{i}-{\overline {x}})^{\mathrm {T} },}$

where ${\displaystyle x_{i}}$ is the i-th observation of the p-dimensional random vector, and the vector

${\displaystyle {\overline {x}}={1 \over {n}}\sum _{i=1}^{n}x_{i}}$

is the sample mean. This is true regardless of the distribution of the random variable X, provided of course that the theoretical means and covariances exist. The reason for the factor n − 1 rather than n is essentially the same as the reason for the same factor appearing in unbiased estimates of sample variances and sample covariances, which relates to the fact that the mean is not known and is replaced by the sample mean (see Bessel's correction).

In cases where the distribution of the random variable X is known to be within a certain family of distributions, other estimates may be derived on the basis of that assumption. A well-known instance is when the random variable X is normally distributed: in this case the maximum likelihood estimator of the covariance matrix is slightly different from the unbiased estimate, and is given by

${\displaystyle \mathbf {Q_{n}} ={1 \over n}\sum _{i=1}^{n}(x_{i}-{\overline {x}})(x_{i}-{\overline {x}})^{\mathrm {T} }.}$

A derivation of this result is given below. Clearly, the difference between the unbiased estimator and the maximum likelihood estimator diminishes for large n.

In the general case, the unbiased estimate of the covariance matrix provides an acceptable estimate when the data vectors in the observed data set are all complete: that is they contain no missing elements. One approach to estimating the covariance matrix is to treat the estimation of each variance or pairwise covariance separately, and to use all the observations for which both variables have valid values. Assuming the missing data are missing at random this results in an estimate for the covariance matrix which is unbiased. However, for many applications this estimate may not be acceptable because the estimated covariance matrix is not guaranteed to be positive semi-definite. This could lead to estimated correlations having absolute values which are greater than one, and/or a non-invertible covariance matrix.

When estimating the cross-covariance of a pair of signals that are wide-sense stationary, missing samples do not need be random (e.g., sub-sampling by an arbitrary factor is valid).[citation needed]

## Maximum-likelihood estimation for the multivariate normal distribution

A random vector XRp (a p×1 "column vector") has a multivariate normal distribution with a nonsingular covariance matrix Σ precisely if Σ ∈ Rp × p is a positive-definite matrix and the probability density function of X is

${\displaystyle f(x)=(2\pi )^{-{\frac {p}{2}}}\,\det(\Sigma )^{-{\frac {1}{2}}}\exp \left(-{1 \over 2}(x-\mu )^{\mathrm {T} }\Sigma ^{-1}(x-\mu )\right)}$

where μRp×1 is the expected value of X. The covariance matrix Σ is the multidimensional analog of what in one dimension would be the variance, and

${\displaystyle (2\pi )^{-{\frac {p}{2}}}\det(\Sigma )^{-{\frac {1}{2}}}}$

normalizes the density ${\displaystyle f(x)}$ so that it integrates to 1.

Suppose now that X1, ..., Xn are independent and identically distributed samples from the distribution above. Based on the observed values x1, ..., xn of this sample, we wish to estimate Σ.

### First steps

The likelihood function is:

${\displaystyle {\mathcal {L}}(\mu ,\Sigma )=(2\pi )^{-{\frac {np}{2}}}\,\prod _{i=1}^{n}\det(\Sigma )^{-{\frac {1}{2}}}\exp \left(-{\frac {1}{2}}(x_{i}-\mu )^{\mathrm {T} }\Sigma ^{-1}(x_{i}-\mu )\right)}$

It is fairly readily shown that the maximum-likelihood estimate of the mean vector μ is the "sample mean" vector:

${\displaystyle {\overline {x}}={\frac {x_{1}+\cdots +x_{n}}{n}}.}$

See the section on estimation in the article on the normal distribution for details; the process here is similar.

Since the estimate ${\displaystyle {\bar {x}}}$ does not depend on Σ, we can just substitute it for μ in the likelihood function, getting

${\displaystyle {\mathcal {L}}({\overline {x}},\Sigma )\propto \det(\Sigma )^{-{\frac {n}{2}}}\exp \left(-{1 \over 2}\sum _{i=1}^{n}(x_{i}-{\overline {x}})^{\mathrm {T} }\Sigma ^{-1}(x_{i}-{\overline {x}})\right),}$

and then seek the value of Σ that maximizes the likelihood of the data (in practice it is easier to work with log ${\displaystyle {\mathcal {L}}}$).

### The trace of a 1 × 1 matrix

Now we come to the first surprising step: regard the scalar ${\displaystyle (x_{i}-{\overline {x}})^{\mathrm {T} }\Sigma ^{-1}(x_{i}-{\overline {x}})}$ as the trace of a 1×1 matrix. This makes it possible to use the identity tr(AB) = tr(BA) whenever A and B are matrices so shaped that both products exist. We get

{\displaystyle {\begin{aligned}{\mathcal {L}}({\overline {x}},\Sigma )&\propto \det(\Sigma )^{-{\frac {n}{2}}}\exp \left(-{1 \over 2}\sum _{i=1}^{n}\operatorname {tr} \left(\left(x_{i}-{\overline {x}}\right)^{\mathrm {T} }\Sigma ^{-1}\left(x_{i}-{\overline {x}}\right)\right)\right)\\&=\det(\Sigma )^{-{\frac {n}{2}}}\exp \left(-{1 \over 2}\sum _{i=1}^{n}\operatorname {tr} \left(\left(x_{i}-{\overline {x}}\right)\left(x_{i}-{\overline {x}}\right)^{\mathrm {T} }\Sigma ^{-1}\right)\right)\\&=\det(\Sigma )^{-{\frac {n}{2}}}\exp \left(-{1 \over 2}\operatorname {tr} \left(\sum _{i=1}^{n}\left(x_{i}-{\overline {x}}\right)\left(x_{i}-{\overline {x}}\right)^{\mathrm {T} }\Sigma ^{-1}\right)\right)\\&=\det(\Sigma )^{-{\frac {n}{2}}}\exp \left(-{1 \over 2}\operatorname {tr} \left(S\Sigma ^{-1}\right)\right)\end{aligned}}}

where

${\displaystyle S=\sum _{i=1}^{n}(x_{i}-{\overline {x}})(x_{i}-{\overline {x}})^{\mathrm {T} }\in \mathbf {R} ^{p\times p}.}$

${\displaystyle S}$ is sometimes called the scatter matrix, and is positive definite if there exists a subset of the data consisting of ${\displaystyle p}$ affinely independent observations (which we will assume).

### Using the spectral theorem

It follows from the spectral theorem of linear algebra that a positive-definite symmetric matrix S has a unique positive-definite symmetric square root S1/2. We can again use the "cyclic property" of the trace to write

${\displaystyle \det(\Sigma )^{-{\frac {n}{2}}}\exp \left(-{1 \over 2}\operatorname {tr} \left(S^{\frac {1}{2}}\Sigma ^{-1}S^{\frac {1}{2}}\right)\right).}$

Let B = S1/2 Σ −1 S1/2. Then the expression above becomes

${\displaystyle \det(S)^{-{\frac {n}{2}}}\det(B)^{\frac {n}{2}}\exp \left(-{1 \over 2}\operatorname {tr} (B)\right).}$

The positive-definite matrix B can be diagonalized, and then the problem of finding the value of B that maximizes

${\displaystyle \det(B)^{\frac {n}{2}}\exp \left(-{1 \over 2}\operatorname {tr} (B)\right)}$

Since the trace of a square matrix equals the sum of eigen-values ("trace and eigenvalues"), the equation reduces to the problem of finding the eigen values λ1, ..., λp that maximize

${\displaystyle \lambda _{i}^{\frac {n}{2}}\exp \left(-{\frac {\lambda _{i}}{2}}\right).}$

This is just a calculus problem and we get λi = n for all i. Thus, assume Q is the matrix of eigen vectors, then

${\displaystyle B=Q(nI_{p})Q^{-1}=nI_{p}}$

i.e., n times the p×p identity matrix.

### Concluding steps

Finally we get

${\displaystyle \Sigma =S^{\frac {1}{2}}B^{-1}S^{\frac {1}{2}}=S^{\frac {1}{2}}\left({\frac {1}{n}}I_{p}\right)S^{\frac {1}{2}}={\frac {S}{n}},}$

i.e., the p×p "sample covariance matrix"

${\displaystyle {S \over n}={1 \over n}\sum _{i=1}^{n}(X_{i}-{\overline {X}})(X_{i}-{\overline {X}})^{\mathrm {T} }}$

is the maximum-likelihood estimator of the "population covariance matrix" Σ. At this point we are using a capital X rather than a lower-case x because we are thinking of it "as an estimator rather than as an estimate", i.e., as something random whose probability distribution we could profit by knowing. The random matrix S can be shown to have a Wishart distribution with n − 1 degrees of freedom.[5] That is:

${\displaystyle \sum _{i=1}^{n}(X_{i}-{\overline {X}})(X_{i}-{\overline {X}})^{\mathrm {T} }\sim W_{p}(\Sigma ,n-1).}$

### Alternative derivation

An alternative derivation of the maximum likelihood estimator can be performed via matrix calculus formulae (see also differential of a determinant and differential of the inverse matrix). It also verifies the aforementioned fact about the maximum likelihood estimate of the mean. Re-write the likelihood in the log form using the trace trick:

${\displaystyle \ln {\mathcal {L}}(\mu ,\Sigma )=\operatorname {const} -{n \over 2}\ln \det(\Sigma )-{1 \over 2}\operatorname {tr} \left[\Sigma ^{-1}\sum _{i=1}^{n}(x_{i}-\mu )(x_{i}-\mu )^{\mathrm {T} }\right].}$

The differential of this log-likelihood is

${\displaystyle d\ln {\mathcal {L}}(\mu ,\Sigma )=-{\frac {n}{2}}\operatorname {tr} \left[\Sigma ^{-1}\left\{d\Sigma \right\}\right]-{1 \over 2}\operatorname {tr} \left[-\Sigma ^{-1}\{d\Sigma \}\Sigma ^{-1}\sum _{i=1}^{n}(x_{i}-\mu )(x_{i}-\mu )^{\mathrm {T} }-2\Sigma ^{-1}\sum _{i=1}^{n}(x_{i}-\mu )\{d\mu \}^{\mathrm {T} }\right].}$

It naturally breaks down into the part related to the estimation of the mean, and to the part related to the estimation of the variance. The first order condition for maximum, ${\displaystyle d\ln {\mathcal {L}}(\mu ,\Sigma )=0}$, is satisfied when the terms multiplying ${\displaystyle d\mu }$ and ${\displaystyle d\Sigma }$ are identically zero. Assuming (the maximum likelihood estimate of) ${\displaystyle \Sigma }$ is non-singular, the first order condition for the estimate of the mean vector is

${\displaystyle \sum _{i=1}^{n}(x_{i}-\mu )=0,}$

which leads to the maximum likelihood estimator

${\displaystyle {\widehat {\mu }}={\bar {X}}={1 \over n}\sum _{i=1}^{n}X_{i}.}$

This lets us simplify

${\displaystyle \sum _{i=1}^{n}(x_{i}-\mu )(x_{i}-\mu )^{\mathrm {T} }=\sum _{i=1}^{n}(x_{i}-{\bar {x}})(x_{i}-{\bar {x}})^{\mathrm {T} }=S}$

as defined above. Then the terms involving ${\displaystyle d\Sigma }$ in ${\displaystyle d\ln L}$ can be combined as

${\displaystyle -{1 \over 2}\operatorname {tr} \left(\Sigma ^{-1}\left\{d\Sigma \right\}\left[nI_{p}-\Sigma ^{-1}S\right]\right).}$

The first order condition ${\displaystyle d\ln {\mathcal {L}}(\mu ,\Sigma )=0}$ will hold when the term in the square bracket is (matrix-valued) zero. Pre-multiplying the latter by ${\displaystyle \Sigma }$ and dividing by ${\displaystyle n}$ gives

${\displaystyle {\widehat {\Sigma }}={1 \over n}S,}$

which of course coincides with the canonical derivation given earlier.

Dwyer [6] points out that decomposition into two terms such as appears above is "unnecessary" and derives the estimator in two lines of working. Note that it may be not trivial to show that such derived estimator is the unique global maximizer for likelihood function.

## Intrinsic covariance matrix estimation

### Intrinsic expectation

Given a sample of n independent observations x1,..., xn of a p-dimensional zero-mean Gaussian random variable X with covariance R, the maximum likelihood estimator of R is given by

${\displaystyle {\hat {\mathbf {R} }}={1 \over n}\sum _{i=1}^{n}x_{i}x_{i}^{\mathrm {T} }.}$

The parameter R belongs to the set of positive-definite matrices, which is a Riemannian manifold, not a vector space, hence the usual vector-space notions of expectation, i.e. "E[R^]", and estimator bias must be generalized to manifolds to make sense of the problem of covariance matrix estimation. This can be done by defining the expectation of an manifold-valued estimator R^ with respect to the manifold-valued point R as

${\displaystyle \mathrm {E} _{\mathbf {R} }[{\hat {\mathbf {R} }}]\ {\stackrel {\mathrm {def} }{=}}\ \exp _{\mathbf {R} }\mathrm {E} \left[\exp _{\mathbf {R} }^{-1}{\hat {\mathbf {R} }}\right]}$

where

${\displaystyle \exp _{\mathbf {R} }({\hat {\mathbf {R} }})=\mathbf {R} ^{\frac {1}{2}}\exp \left(\mathbf {R} ^{-{\frac {1}{2}}}{\hat {\mathbf {R} }}\mathbf {R} ^{-{\frac {1}{2}}}\right)\mathbf {R} ^{\frac {1}{2}}}$
${\displaystyle \exp _{\mathbf {R} }^{-1}({\hat {\mathbf {R} }})=\mathbf {R} ^{\frac {1}{2}}\left(\log \mathbf {R} ^{-{\frac {1}{2}}}{\hat {\mathbf {R} }}\mathbf {R} ^{-{\frac {1}{2}}}\right)\mathbf {R} ^{\frac {1}{2}}}$

are the exponential map and inverse exponential map, respectively, "exp" and "log" denote the ordinary matrix exponential and matrix logarithm, and E[·] is the ordinary expectation operator defined on a vector space, in this case the tangent space of the manifold.[1]

### Bias of the sample covariance matrix

The intrinsic bias vector field of the SCM estimator ${\displaystyle {\hat {\mathbf {R} }}}$ is defined to be

${\displaystyle \mathbf {B} ({\hat {\mathbf {R} }})=\exp _{\mathbf {R} }^{-1}\mathrm {E} _{\mathbf {R} }\left[{\hat {\mathbf {R} }}\right]=\mathrm {E} \left[\exp _{\mathbf {R} }^{-1}{\hat {\mathbf {R} }}\right]}$

The intrinsic estimator bias is then given by ${\displaystyle \exp _{\mathbf {R} }\mathbf {B} ({\hat {\mathbf {R} }})}$.

For complex Gaussian random variables, this bias vector field can be shown[1] to equal

${\displaystyle \mathbf {B} ({\hat {\mathbf {R} }})=-\beta (p,n)\mathbf {R} }$

where

${\displaystyle \beta (p,n)={\frac {1}{p}}\left(p\log n+p-\psi (n-p+1)+(n-p+1)\psi (n-p+2)+\psi (n+1)-(n+1)\psi (n+2)\right)}$

and ψ(·) is the digamma function. The intrinsic bias of the sample covariance matrix equals

${\displaystyle \exp _{\mathbf {R} }\mathbf {B} ({\hat {\mathbf {R} }})=e^{-\beta (p,n)}\mathbf {R} }$

and the SCM is asymptotically unbiased as n → ∞.

Similarly, the intrinsic inefficiency of the sample covariance matrix depends upon the Riemannian curvature of the space of positive-definite matrices.

## Shrinkage estimation

If the sample size n is small and the number of considered variables p is large, the above empirical estimators of covariance and correlation are very unstable. Specifically, it is possible to furnish estimators that improve considerably upon the maximum likelihood estimate in terms of mean squared error. Moreover, for n < p (the number of observations is less than the number of random variables) the empirical estimate of the covariance matrix becomes singular, i.e. it cannot be inverted to compute the precision matrix.

As an alternative, many methods have been suggested to improve the estimation of the covariance matrix. All of these approaches rely on the concept of shrinkage. This is implicit in Bayesian methods and in penalized maximum likelihood methods and explicit in the Stein-type shrinkage approach.

A simple version of a shrinkage estimator of the covariance matrix is represented by the Ledoit-Wolf shrinkage estimator.[7][8][9][10] One considers a convex combination of the empirical estimator (${\displaystyle A}$) with some suitable chosen target (${\displaystyle B}$), e.g., the diagonal matrix. Subsequently, the mixing parameter (${\displaystyle \delta }$) is selected to maximize the expected accuracy of the shrunken estimator. This can be done by cross-validation, or by using an analytic estimate of the shrinkage intensity. The resulting regularized estimator (${\displaystyle \delta A+(1-\delta )B}$) can be shown to outperform the maximum likelihood estimator for small samples. For large samples, the shrinkage intensity will reduce to zero, hence in this case the shrinkage estimator will be identical to the empirical estimator. Apart from increased efficiency the shrinkage estimate has the additional advantage that it is always positive definite and well conditioned.

Various shrinkage targets have been proposed:

1. the identity matrix, scaled by the average sample variance;
2. the single-index model;
3. the constant-correlation model, where the sample variances are preserved, but all pairwise correlation coefficients are assumed to be equal to one another;
4. the two-parameter matrix, where all variances are identical, and all covariances are identical to one another (although not identical to the variances);
5. the diagonal matrix containing sample variances on the diagonal and zeros everywhere else;
6. the identity matrix.[8]

The shrinkage estimator can be generalized to a multi-target shrinkage estimator that utilizes several targets simultaneously.[11] Software for computing a covariance shrinkage estimator is available in R (packages corpcor[12] and ShrinkCovMat[13]), in Python (library scikit-learn), and in MATLAB.[14]

## Nearest valid matrix

In some applications (e.g., building data models from only partially observed data) one wants to find the "nearest" covariance matrix or correlation matrix to a given symmetric matrix (e.g., of observed covariances). In 2002, Higham[15] formalized the notion of nearness using a weighted Frobenius norm and provided a method for computing the nearest correlation matrix.

## References

1. ^ a b c Smith, Steven Thomas (May 2005). "Covariance, Subspace, and Intrinsic Cramér–Rao Bounds". IEEE Trans. Signal Process. 53 (5): 1610–1630. doi:10.1109/TSP.2005.845428. S2CID 2751194.
2. ^ Robust Statistics, Peter J. Huber, Wiley, 1981 (republished in paperback, 2004)
3. ^ "Modern applied statistics with S", William N. Venables, Brian D. Ripley, Springer, 2002, ISBN 0-387-95457-0, ISBN 978-0-387-95457-8, page 336
4. ^ Devlin, Susan J.; Gnanadesikan, R.; Kettenring, J. R. (1975). "Robust Estimation and Outlier Detection with Correlation Coefficients". Biometrika. 62 (3): 531–545. doi:10.1093/biomet/62.3.531.
5. ^
6. ^ Dwyer, Paul S. (June 1967). "Some applications of matrix derivatives in multivariate analysis". Journal of the American Statistical Association. 62 (318): 607–625. doi:10.2307/2283988. JSTOR 2283988.
7. ^ O. Ledoit and M. Wolf (2004a) "A well-conditioned estimator for large-dimensional covariance matrices Archived 2014-12-05 at the Wayback Machine" Journal of Multivariate Analysis 88 (2): 365—411.
8. ^ a b A. Touloumis (2015) "Nonparametric Stein-type shrinkage covariance matrix estimators in high-dimensional settings" Computational Statistics & Data Analysis 83: 251—261.
9. ^ O. Ledoit and M. Wolf (2003) "Improved estimation of the covariance matrix of stock returns with an application to portofolio selection Archived 2014-12-05 at the Wayback Machine" Journal of Empirical Finance 10 (5): 603—621.
10. ^ O. Ledoit and M. Wolf (2004b) "Honey, I shrunk the sample covariance matrix Archived 2014-12-05 at the Wayback Machine" The Journal of Portfolio Management 30 (4): 110—119.
11. ^ T. Lancewicki and M. Aladjem (2014) "Multi-Target Shrinkage Estimation for Covariance Matrices", IEEE Transactions on Signal Processing, Volume: 62, Issue 24, pages: 6380-6390.
12. ^
13. ^
14. ^ MATLAB code for shrinkage targets: scaled identity, single-index model, constant-correlation model, two-parameter matrix, and diagonal matrix.
15. ^ Higham, Nicholas J. (2002). "Computing the nearest correlation matrix—a problem from finance". IMA Journal of Numerical Analysis. 22 (3): 329–343. CiteSeerX 10.1.1.661.2180. doi:10.1093/imanum/22.3.329.