corrcov

Convert covariance matrix to correlation matrix

Description

example

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.

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.

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.

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

collapse all

Covariance

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

$\mathrm{cov}\left(A,B\right)=\frac{1}{N-1}\sum _{i=1}^{N}{\left({A}_{i}-{\mu }_{A}\right)}^{*}\left({B}_{i}-{\mu }_{B}\right)$

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=\left(\begin{array}{cc}\mathrm{cov}\left(A,A\right)& \mathrm{cov}\left(A,B\right)\\ \mathrm{cov}\left(B,A\right)& \mathrm{cov}\left(B,B\right)\end{array}\right).$

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\left(i,j\right)=\mathrm{cov}\left(X\left(:,i\right),X\left(:,j\right)\right)$.

Variance

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

$V=\frac{1}{N-1}\sum _{i=1}^{N}|{A}_{i}-\mu {|}^{2}$

where μ is the mean of A,

$\mu =\frac{1}{N}\sum _{i=1}^{N}{A}_{i}.$

Some definitions of variance use a normalization factor of N instead of N–1, but the mean always has the normalization factor N.
{"ft_lang_label":"__label__en","ft_lang_prob":0.73121196,"math_prob":0.9993773,"size":5327,"snap":"2022-05-2022-21","text_gpt3_token_len":1479,"char_repetition_ratio":0.15818147,"word_repetition_ratio":0.09495897,"special_character_ratio":0.28515112,"punctuation_ratio":0.14217557,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99987686,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-01-23T05:14:22Z\",\"WARC-Record-ID\":\"<urn:uuid:8ce953a3-5725-4d9f-a882-f6ab3b8a45e1>\",\"Content-Length\":\"101642\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d48f52ec-8483-45ca-ad71-ca2d5dc92030>\",\"WARC-Concurrent-To\":\"<urn:uuid:29deec5d-2d9b-4a9c-8fc0-d0ebb23f3eb7>\",\"WARC-IP-Address\":\"104.68.243.15\",\"WARC-Target-URI\":\"https://se.mathworks.com/help/stats/corrcov.html\",\"WARC-Payload-Digest\":\"sha1:CXZP3IOUER2Z7RUIPPJBDTDYYIO6AGZE\",\"WARC-Block-Digest\":\"sha1:47J36XANGAHMKR3XGYPTOFSLA67NSQO3\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-05/CC-MAIN-2022-05_segments_1642320304134.13_warc_CC-MAIN-20220123045449-20220123075449-00555.warc.gz\"}"}