# cov

Sample covariance matrix

### Syntax

C = cov(x) C = cov(x, 0) C = cov(x, 1) C = cov(x, y) C = cov(x, y, 0) C = cov(x, y, 1)

### Parameters

- x
a nobs-by-1 or nobs-by-n matrix of doubles

- y
a nobs-by-1 or nobs-by-m matrix of doubles

- C
a square matrix of doubles, the empirical covariance or cross-covariance

### Description

If x is a nobs-by-1 matrix,
then `cov(x)`

returns the sample variance of x,
normalized by nobs-1.

If x is a nobs-by-n matrix,
then `cov(x)`

returns the n-by-n sample covariance matrix of the
columns of x, normalized by nobs-1.
Here, each column of x is a variable among (1 ... n) and
each row of x is an observation.

If x and y are two nobs-by-1 matrices,
then `cov(x, y)`

returns the 2-by-2 sample covariance matrix of x and
y, normalized by nobs-1, where nobs is the number of observations.

If x and y are respectively a nobs-by-n and a nobs-by-m matrix
then `cov(x, y)`

returns the n-by-m sample cross-covariance matrix of x and
y, normalized by nobs-1, where nobs is the number of observations.

`cov(x, 0)`

is the same as `cov(x)`

and
`cov(x, y, 0)`

is the same as `cov(x, y)`

.
In this case, if the population is from a normal distribution,
then C is the best unbiased estimate of the covariance matrix or cross-covariance matrix.

`cov(x, 1)`

and `cov(x, y, 1)`

normalize by nobs.
In this case, C is the second moment matrix of the
observations about their mean.

The covariance of two random vectors X and Y is defined by:

*Cov(X,Y) = E[ (X-E(X)).(Y-E(Y)) ^{t}]*

where E is the expectation.

### Examples

x = [1; 2]; y = [3; 4]; C = cov(x, y) expected = [0.5, 0.5; 0.5, 0.5]; C = cov([x, y])

x = [230; 181; 165; 150; 97; 192; 181; 189; 172; 170]; y = [125; 99; 97; 115; 120; 100; 80; 90; 95; 125]; expected = [ 1152.4556, -88.911111 -88.911111, 244.26667 ]; C = cov(x, y) C = cov([x, y])

// Source [3] A = [ 4.0 2.0 0.60 4.2 2.1 0.59 3.9 2.0 0.58 4.3 2.1 0.62 4.1 2.2 0.63 ]; S = [ 0.025 0.0075 0.00175 0.0075 0.007 0.00135 0.00175 0.00135 0.00043 ]; C = cov(A)

### Bibliography

Wikipedia: Covariance matrix Wikipedia: Cross-covariance matrix

[3] NIST/SEMATECH e-Handbook of Statistical Methods, 6.5.4.1. Mean Vector and Covariance Matrix

"Introduction to probability and statistics for engineers and scientists", Sheldon Ross

### History

Versão | Descrição |

5.5.0 | cov function added, to improve mvvacov (deprecated) |

6.1 | cross-covariance computation added |

## Comments

Add a comment:Please login to comment this page.