In [1]:
import numpy as np
In [2]:
import pandas as pd
In [3]:
from numpy.linalg import inv
In [4]:
df1=pd.read_excel('OIH.xls')
In [5]:
df2=pd.read_excel('RKH.xls')
In [6]:
df=pd.merge(df1, df2, on='Date', suffixes=('_OIH', '_RKH'))
In [7]:
df.set_index('Date', inplace=True)
In [8]:
df3=pd.read_excel('RTH.xls')
In [9]:
df=pd.merge(df, df3, on='Date')
In [10]:
df.rename(columns={"Adj Close": "Adj Close_RTH"}, inplace=True)
In [11]:
df.set_index('Date', inplace=True)
In [12]:
df.sort_index(inplace=True)
In [13]:
dailyret=df.loc[:, ('Adj Close_OIH', 'Adj Close_RKH', 'Adj Close_RTH')].pct_change()
In [14]:
dailyret.rename(columns={"Adj Close_OIH": "OIH", "Adj Close_RKH": "RKH", "Adj Close_RTH": "RTH"}, inplace=True)
In [15]:
excessRet=dailyret-0.04/252
In [16]:
M=252*excessRet.mean()
In [17]:
M
Out[17]:
In [18]:
C=252*excessRet.cov()
In [19]:
C
Out[19]:
In [20]:
F=np.dot(inv(C), M)
In [21]:
F
Out[21]:
In [22]:
g=0.04+np.dot(F.T, np.dot(C, F))/2
In [23]:
g
Out[23]:
In [24]:
S=np.sqrt(np.dot(F.T, np.dot(C, F)))
In [25]:
S
Out[25]: