-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCAPM.py
61 lines (44 loc) · 1.29 KB
/
CAPM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 1 16:56:25 2021
@author: fav21
"""
# load modules
import numpy as np
import pandas as pd
# risk-free Treasury rate
R_f = 0.0175 / 252
# read in the market data
data = pd.read_csv('capm_market_data.csv')
data.head()
df = data.drop(columns=['date'])
daily = df.pct_change(axis=0)
daily.dropna(inplace=True)
daily.head(5)
spy = daily.spy_adj_close.values
print("SPY first five:", spy[0:5])
aapl = daily.aapl_adj_close.values
print("AAPL first five:", aapl[0:5])
ex_spy = spy-R_f
ex_aapl = aapl-R_f
print("SPY excess:",ex_spy[-5:])
print("AAPL excess:",ex_aapl[-5:])
import matplotlib.pyplot as plt
plt.scatter(x=ex_spy,y=ex_aapl)
x = ex_spy.reshape(-1,1)
y = ex_aapl.reshape(-1,1)
beta = np.matmul(np.matmul(np.linalg.inv(np.matmul(x.transpose(),x)),x.transpose()),y)
beta = beta[0][0]
print(beta)
def beta_sensitivity(x_val,y_val):
estimates = []
for i in range(0,len(x_val)):
x_temp = np.delete(x_val,i).reshape(-1,1)
y_temp = np.delete(y_val,i).reshape(-1,1)
beta = np.matmul(np.matmul(np.linalg.inv(np.matmul(x_temp.transpose(),x_temp)),x_temp.transpose()),y_temp)
beta = beta[0][0]
estimate = (i,beta)
estimates.append(estimate)
return(estimates)
betas = beta_sensitivity(ex_spy,ex_aapl)
betas[:5]