Constructing a Covariance Matrix from Correlations and Standard Deviations
Summary
The document explains how to convert a correlation matrix and a set of standard deviations into a covariance matrix. For each pair of variables, multiply their correlation by both standard deviations. On the diagonal, each variable’s covariance with itself is its variance, equal to the square of its standard deviation.
A small two-variable example illustrates the calculation and shows that common numerical libraries can compute a sample covariance matrix directly from observations. The example’s values are illustrative rather than market evidence. When building a matrix from estimated inputs, the resulting covariance estimates inherit the data and estimation choices behind the correlations and standard deviations; the document does not discuss estimation windows, missing data, or whether a matrix is suitable for portfolio optimization.
Key ideas
- Covariance between two variables equals their correlation multiplied by both standard deviations.
- The diagonal entries of a covariance matrix are variances, obtained by squaring each standard deviation.
- Applying the pairwise relationship to all entries converts a correlation matrix into a covariance matrix.
- Statistical libraries can also estimate covariance directly from observed data.
- The document does not address input estimation choices or matrix suitability for portfolio use.
Tags
Full text
# Creating a Covariance Matrix
# Creating a Covariance Matrix
Lets say that you have the correlation of x,y and you have the standard deviations of x and y , how would you then find the covariance of x,y using the correlation of x,y and and the standard deviation of x,y .
The reason I would like to know this is I would like to take a correlation matrix and the standard deviations of all of the variables and use it to create a covariance matrix .
Thank you for your time , your help will be greatly appreciated
## Answer by Valometrics.com (score 8, accepted)
https://quant.stackexchange.com/a/50889
here is how to get covariance matrix from correlations:
## Answer by David Duarte (score 2)
https://quant.stackexchange.com/a/50890
The relationship between covariance, standard deviation and correlation is:
$$ corr(x,y) = \frac{cov(x,y)}{\sigma_x \sigma_y}$$
So to construct your matrix you will have the variances in the diagonal:
$$ cov(x,x) = corr(x,x) \times \sigma_x \times \sigma_x = 1 \times \sigma_x^2 = \sigma_x^2 $$
And for the covariances:
$$ cov(x,y) = corr(x,y) \times \sigma_x \times \sigma_y $$
Here is an example with the calculations in python:
```
import numpy as np
import pandas as pd
x = np.random.randint(1,4,20)
y = np.random.randint(1,4,20)
pd.DataFrame({'x': x, 'y': y}).describe()
```
```
std_x, std_y = pd.DataFrame({'x': x, 'y': y}).describe().loc['std']
corr_xy = np.corrcoef(x,y)[0][1]
print(f"Correlation between x and y: {corr_xy}")
var_x = std_x**2
print(f"Variance of x: {var_x}")
var_y = std_y**2
print(f"Variance of y: {var_y}")
cov_xy = corr_xy * std_x * std_y
print(f"Covariance between x and y: {cov_xy}")
```
Which would output:
`Correlation between x and y: -0.20037977722310454` `Variance of x: 0.6421052631578946` `Variance of y: 0.7263157894736844` `Covariance between x and y: -0.13684210526315793`
Altought you could get these values directly with Numpy:
```
cov = np.cov(x,y)
print(cov)
```
[[ 0.64210526 -0.13684211] [-0.13684211 0.72631579]]
Or using pandas:
```
pd.DataFrame({'x': x, 'y': y}).cov()
```Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.