Covariance and Correlation

\begin{eqnarray*} Covariance Matrix: \ \ \Sigma = \begin{bmatrix} \sigma_{1}^2 \ \sigma_{12} \ \dots \ \sigma_{1I} \\ \sigma_{21} \ \sigma_{2}^2 \ \dots \ \sigma_{2I} \\ \vdots \ \vdots \ \ddots \ \vdots \\ \sigma_{I1} \ \sigma_{I2} \ \dots \ \sigma_{I}^2 \end{bmatrix} \end{eqnarray*}

The Covariance Matrix is a representation of how two or more variables relate to each other. The covariance between a variable to itself, is the variance of that same variables.


In [2]:
import numpy as np
import pandas as pd
from pandas_datareader import data as wb
import matplotlib.pyplot as plt

In [3]:
tickers = ['PG', 'GOOG']

sec_data = pd.DataFrame()

for t in tickers:
    sec_data[t] = wb.DataReader(t, data_source='google', start='2007-1-1')['Close']

In [4]:
sec_returns = np.log(sec_data / sec_data.shift(1))

In [5]:
PG_var_a = sec_returns['PG'].var() * 250
PG_var_a


Out[5]:
0.030992113443210789

In [6]:
GOOG_var_a = sec_returns['GOOG'].var() * 250
GOOG_var_a


Out[6]:
0.084736799096451182

In [7]:
cov_matrix = sec_returns.cov() * 250
cov_matrix


Out[7]:
PG GOOG
PG 0.030992 0.019813
GOOG 0.019813 0.084737

In [8]:
corr_matrix = sec_returns.corr()
corr_matrix


Out[8]:
PG GOOG
PG 1.000000 0.386506
GOOG 0.386506 1.000000

In [9]:
weights = np.array([0.5, 0.5]) # 50% Google, 50% P&G

Portfolio Variance


In [10]:
pfolio_var = np.dot(weights.T, np.dot(sec_returns.cov() * 250, weights))
pfolio_var


Out[10]:
0.038838886062011954

Portfolio Volatillity


In [17]:
pfolio_vol = pfolio_var ** 0.5
print('{}%'.format(round(pfolio_vol, 4) * 100))


19.71%

In [ ]: