import numpy as np
import pandas as pd
from pandas_datareader import data as wb
import matplotlib.pyplot as plt

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']

sec_returns = np.log(sec_data / sec_data.shift(1))

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

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

cov_matrix = sec_returns.cov() * 250
cov_matrix

corr_matrix = sec_returns.corr()
corr_matrix

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

"""
Explanation: 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.
End of explanation
"""


pfolio_var = np.dot(weights.T, np.dot(sec_returns.cov() * 250, weights))
pfolio_var

"""
Explanation: Portfolio Variance
End of explanation
"""


pfolio_vol = pfolio_var ** 0.5
print('{}%'.format(round(pfolio_vol, 4) * 100))

"""
Explanation: Portfolio Volatillity
End of explanation
"""


