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]:
In [6]:
GOOG_var_a = sec_returns['GOOG'].var() * 250
GOOG_var_a
Out[6]:
In [7]:
cov_matrix = sec_returns.cov() * 250
cov_matrix
Out[7]:
In [8]:
corr_matrix = sec_returns.corr()
corr_matrix
Out[8]:
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]:
Portfolio Volatillity
In [17]:
pfolio_vol = pfolio_var ** 0.5
print('{}%'.format(round(pfolio_vol, 4) * 100))
In [ ]: