Main Content

corrcov

Convert covariance matrix to correlation matrix

Description

R = corrcov(C) returns the correlation matrix R corresponding to the covariance matrix C.

example

[R,sigma] = corrcov(C) also returns sigma, a vector of standard deviations.

example

Examples

collapse all

Compare the correlation matrix obtained by applying corrcov on a covariance matrix with the correlation matrix obtained by direct computation using corrcoef on an input matrix.

Load the hospital data set and create a matrix containing the Weight and BloodPressure measurements. Note that hospital.BloodPressure has two columns of data.

load hospital
X = [hospital.Weight hospital.BloodPressure];

Compute the covariance matrix.

C = cov(X)
C = 3×3

  706.0404   27.7879   41.0202
   27.7879   45.0622   23.8194
   41.0202   23.8194   48.0590

Compute the correlation matrix from the covariance matrix by using corrcov.

R1 = corrcov(C)
R1 = 3×3

    1.0000    0.1558    0.2227
    0.1558    1.0000    0.5118
    0.2227    0.5118    1.0000

Compute the correlation matrix directly by using corrcoef, and then compare R1 with R2.

R2 = corrcoef(X)
R2 = 3×3

    1.0000    0.1558    0.2227
    0.1558    1.0000    0.5118
    0.2227    0.5118    1.0000

The correlation matrices R1 and R2 are the same.

Find the vector of standard deviations from the covariance matrix, and show the relationship between the standard deviations and the covariance matrix.

Load the hospital data set and create a matrix containing the Weight, BloodPressure, and Age measurements. Note that hospital.BloodPressure has two columns of data.

load hospital
X = [hospital.Weight hospital.BloodPressure hospital.Age];

Compute the covariance matrix of X.

C = cov(X)
C = 4×4

  706.0404   27.7879   41.0202   17.5152
   27.7879   45.0622   23.8194    6.4966
   41.0202   23.8194   48.0590    4.0315
   17.5152    6.4966    4.0315   52.0622

C is square, symmetric, and positive semidefinite. The diagonal elements of C are the variances of the four variables in X.

Compute the correlation matrix and standard deviations of X from the covariance matrix C.

[R,s1] = corrcov(C)
R = 4×4

    1.0000    0.1558    0.2227    0.0914
    0.1558    1.0000    0.5118    0.1341
    0.2227    0.5118    1.0000    0.0806
    0.0914    0.1341    0.0806    1.0000

s1 = 4×1

   26.5714
    6.7128
    6.9325
    7.2154

Compute the square root of the diagonal elements in C, and then compare s1 with s2.

s2 = sqrt(diag(C))
s2 = 4×1

   26.5714
    6.7128
    6.9325
    7.2154

s1 and s2 are equal and correspond to the standard deviation of the variables in X.

Input Arguments

collapse all

Covariance matrix, specified as a square, symmetric, and positive semidefinite matrix.

For a matrix X that has N observations (rows) and n random variables (columns), C is an n-by-n matrix. The n diagonal elements of C are the variances of the n random variables in X, and a zero diagonal element in C indicates a constant variable in X.

Data Types: single | double

Output Arguments

collapse all

Correlation matrix, returned as a matrix that corresponds to the covariance matrix C.

Data Types: single | double

Standard deviations, returned as an n-by-1 vector.

The elements of sigma are the standard deviations of the variables in X, the N-by-n matrix that produces C. Row i in sigma corresponds to the standard deviation of column i in X.

Data Types: single | double

More About

collapse all

Covariance

For two random variable vectors A and B, the covariance is defined as

cov(A,B)=1N1i=1N(AiμA)*(BiμB)

where N is the length of each column, μA and μB are the mean values of A and B, respectively, and * denotes the complex conjugate.

The covariance matrix of two random variables is the matrix of pairwise covariance calculations between each variable,

C=(cov(A,A)cov(A,B)cov(B,A)cov(B,B)).

For a matrix X, in which each column is a random variable composed of observations, the covariance matrix is the pairwise covariance calculation between each column combination. In other words, C(i,j)=cov(X(:,i),X(:,j)).

Variance

For a random variable vector A composed of N scalar observations, the variance is defined as

V=1N1i=1N|Aiμ|2

where μ is the mean of A,

μ=1Ni=1NAi.

Some definitions of variance use a normalization factor of N instead of N–1, but the mean always has the normalization factor N.

Extended Capabilities

Version History

Introduced in R2007b

See Also

| | |