18
$\begingroup$

This is for a project which I've been trying to find some information for Covariance matrix and correlation matrix.

I understand that for a $n \times n$ matrix $A, AA^T$ will give me the covariance matrix.

Is there any relationship between the covariance and correlation matrix?

Sorry maybe I wasn't clear.

I wanted to use Cholesky decomposition to generate correlated variables from random variables. I do know how to do it using matlab. And I understand how it works for 2 variables. But when I scale up the matrix to $n \times n$ instead of $2 \times 2$, I am not sure how it will work out.

would appreciate if someone could provide more hint on the mathematics.

$\endgroup$

4 Answers 4

24
$\begingroup$

From a matrix algebra point of view the answer is fairly simple. Assume your covariance matrix is $\Sigma$ and let

$$ D =\sqrt{ \text{diag}\left( {\Sigma} \right)} $$

then the correlation matrix is given by $$ \varrho = D^{-1}\Sigma D^{-1} $$

Edit: fixed to include square root

$\endgroup$
3
  • 1
    $\begingroup$ I think one of your $D^{-1}$ needs to be transposed. $\endgroup$
    – SRKX
    Jun 26, 2017 at 5:32
  • 22
    $\begingroup$ I did transpose it, but you can't tell since it is diagonal ;-) $\endgroup$
    – Brian B
    Jun 27, 2017 at 17:34
  • 1
    $\begingroup$ numpy two-liner: Dinv = np.diag(1 / np.sqrt(np.diag(cov))) corr = Dinv @ cov @ Dinv $\endgroup$
    – MaxNoe
    Feb 26, 2021 at 16:22
7
$\begingroup$

Suppose you have a random vector $\mathbf{g}$, then the covariance matrix of $\mathbf{g}$ is defined as $$\mathbf{K}=\mathbf{E}\{(\mathbf{g}-\bar{\mathbf{g}})(\mathbf{g}-\bar{\mathbf{g}})^{\dagger}\}$$ where $\mathbf{E}$ denotes expectation, $\bar{\mathbf{g}}$ denotes the mean of $\mathbf{g}$, $\dagger$ means transpose for real random vector, and conjugate transpose for complex random vector.

The correlation matrix is $$\mathbf{R}=\mathbf{E}\{\mathbf{g}\mathbf{g}^{\dagger}\}$$

So we have $$\mathbf{K}=\mathbf{R}-\bar{\mathbf{g}}\bar{\mathbf{g}}^{\dagger}$$

For zero-mean random vectors $\mathbf{K}=\mathbf{R}$.

EDIT: for another definition where the correlation matrix is the normalized covariance matrix, the relation is $$\mathbf{R}_{ij}=\frac{\mathbf{K}_{ij}}{\sigma_i \sigma_j}$$ where $\sigma_i, \sigma_j$ are the standard deviation of $\mathbf{g}_i$ and $\mathbf{g}_j$, respectively.

$\endgroup$
2
  • 3
    $\begingroup$ "correlation matrix" can mean that, but a more usual meaning is the normalized covariance matrix (i.e. correlation factors) $\endgroup$
    – leonbloy
    Aug 26, 2012 at 4:02
  • $\begingroup$ And is $\sigma_i$ simply $\sqrt{\mathbf{K}_{ii}}$? $\endgroup$ Apr 2, 2017 at 15:44
2
$\begingroup$

Cribbing from the answer by Brian B., assume your covariance matrix is Σ and let

D = sqrt(diag(Σ)), a vector of square roots of the diagonal of Σ.

then the correlation matrix is given by ϱ = D-inverse Σ D-inverse-prime

D, here, is a p x 1 vector (from the diagonal of Σ) and its inverse is the item by item inverse of D -- i.e., vector of {one over element} for each element.

$\endgroup$
3
  • $\begingroup$ This clarification is useful but I'd have kept the math format. $\endgroup$
    – SRKX
    Jun 26, 2017 at 5:20
  • 1
    $\begingroup$ Sorry, I am not following. If $D^{-1}$ is a $p \times 1$ vector, then $(D^{-1})^T$ is a $1 \times p$ vector and the product $D^{-1} \Sigma$ is a also a $p \times 1$ which means that and $D^{-1} \Sigma (D^{-1})^T$ is a scalar, not a matrix. $\endgroup$
    – Confounded
    Dec 20, 2017 at 12:38
  • $\begingroup$ $D$ needs to be a diagonal matrix, not a vector. Let $D$ be the diagonal matrix with the reciprocals of the square roots of the diagonal entries of $\Sigma$ on its diagonal. $\endgroup$
    – Vladhagen
    Feb 24, 2018 at 22:31
1
$\begingroup$

Matlab has a function cov2corr to extract the correlation matrix from covariance matrix. If you're already using Matlab, no need to reinvent the wheel. The implementation of the function is similar to chaohuang's answer above (with some error checking).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.