Week 02-3 – CAPM
Capital Asset Pricing Model¶
Capital asset pricing model is a simple linear model that assumes in eqilibrium the relationship between a singal asset $i$ and the market (benchmark) can be written as:
$$R_i – r = \alpha + \beta R_m – r + \epsilon$$where $r$ is the risk-free rate.
The coefficient $\beta$ indicates the risk level of the asset comparing to the market. For $\beta = 2$, the asset is twice the riskier comparing to the market. The coefficient $\alpha$ indicates the abnormal return from this specific asset. For sigle name stock, $\alpha$ is typically very noisy; therefore, $\alpha$ is mostly used for fund analysis which is more stable through diversification.
CAPM is a linear regression model built on asset excess return (risk premium). So the CAPE represents higher returns to compensate for incurring risk. We will use 3-month Treasury bill rate as the risk-free rate $r$.
Import Packages¶
In [6]:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import quandl
from statsmodels import regression
%matplotlib inline
—————————————————————————
ImportError Traceback (most recent call last)
2 import numpy as np
3 import pandas as pd
—-> 4 import quandl
5
6 from statsmodels import regression
ImportError: No module named ‘quandl’
Getting Data¶
https://www.quandl.com/ has free data.
In [7]:
start_date = ‘2014-06-30’
end_date = ‘2015-06-30’
# We will look at the returns of an asset one-month into the future to model future returns.
offset_start_date = ‘2014-07-31’
offset_end_date = ‘2015-07-31’
df_msft = quandl.get(“GOOG/NASDAQ_MSFT”, authtoken=”8Xr8PVKcumcx-kaTYaWs”)
df_spy = quandl.get(“GOOG/NYSE_SPY”)
df_treasury = quandl.get(“USTREASURY/BILLRATES”)
—————————————————————————
NameError Traceback (most recent call last)
6 offset_end_date = ‘2015-07-31’
7
—-> 8 df_msft = quandl.get(“GOOG/NASDAQ_MSFT”, authtoken=”8Xr8PVKcumcx-kaTYaWs”)
9 df_spy = quandl.get(“GOOG/NYSE_SPY”)
10 df_treasury = quandl.get(“USTREASURY/BILLRATES”)
NameError: name ‘quandl’ is not defined
In [8]:
start_date = ‘2011-12-30’
end_date = ‘2015-12-31’
df_tbill = df_treasury[start_date:end_date][‘4 Wk Bank Discount Rate’] / 100
df_data = pd.DataFrame(index=df_tbill.index)
df_data[‘T Bill’] = df_tbill
df_data[‘MSFT’] = df_msft[start_date:end_date][‘Close’].pct_change().dropna() – df_data[‘T Bill’]
df_data[‘SPY’] = df_spy[start_date:end_date][‘Close’].pct_change().dropna() – df_data[‘T Bill’]
—————————————————————————
NameError Traceback (most recent call last)
2 end_date = ‘2015-12-31’
3
—-> 4 df_tbill = df_treasury[start_date:end_date][‘4 Wk Bank Discount Rate’] / 100
5 df_data = pd.DataFrame(index=df_tbill.index)
6 df_data[‘T Bill’] = df_tbill
NameError: name ‘df_treasury’ is not defined
In [9]:
df_data[‘Constant’] = pd.TimeSeries(np.ones(len(df_data.index)), index=df_data.index)
df_data = df_data.dropna()
df_data.tail()
—————————————————————————
NameError Traceback (most recent call last)
—-> 1 df_data[‘Constant’] = pd.TimeSeries(np.ones(len(df_data.index)), index=df_data.index)
2 df_data = df_data.dropna()
3 df_data.tail()
NameError: name ‘df_data’ is not defined
Computing CAPM Model.¶
In [5]:
OLS_model = regression.linear_model.OLS(df_data[‘MSFT’], df_data[[‘Constant’, ‘SPY’]])
fitted_model = OLS_model.fit()
print(‘p-value’, fitted_model.f_pvalue)
print(fitted_model.params)
R1_params = fitted_model.params
#OLS_model = regression.linear_model.OLS(df[‘R2’], df[[‘SPY’, ‘RF’, ‘Constant’]])
#fitted_model = OLS_model.fit()
#print ‘p-value’, fitted_model.f_pvalue
#print fitted_model.params
#R2_params = fitted_model.params
—————————————————————————
NameError Traceback (most recent call last)
—-> 1 OLS_model = regression.linear_model.OLS(df_data[‘MSFT’], df_data[[‘Constant’, ‘SPY’]])
2 fitted_model = OLS_model.fit()
3 print(‘p-value’, fitted_model.f_pvalue)
4 print(fitted_model.params)
5 R1_params = fitted_model.params
NameError: name ‘regression’ is not defined
Rolling Regression and CAPM¶
In [ ]:
model = pd.stats.ols.MovingOLS(y = df_data[‘MSFT’], x=df_data[[‘Constant’, ‘SPY’]],
window_type=’rolling’,
window=252)
rolling_parameter_estimates = model.beta
rolling_parameter_estimates[‘SPY’].plot();
plt.hlines(R1_params[‘SPY’], df_data.index[0], df_data.index[-1], linestyles=’dashed’, colors=’blue’)
plt.title(‘CAPM Beta for MSFT’)
plt.show()
#plt.legend([‘Market Beta’, ‘Risk Free Beta’, ‘Intercept’, ‘Market Beta Static’, ‘Risk Free Beta Static’, ‘Intercept Static’]);