$r_p(t) = \sum\limits_{i}^{n}w_i r_i(t)$
$ w_i = \frac{MarketCap_i}{\sum_{j}^{n}{MarketCap_j}} $
$ r_p(t) = \beta_pr_m(t) + \sum\limits_{i}^{n}w_i \alpha_i(t)$
# Treat Model CAPM as a simple linear regression
from scipy import stats
import pandas as pd
import pandas_datareader as web
help(stats.linregress)
spy_etf = web.DataReader('SPY','google')
spy_etf.info()
spy_etf.head()
# Get dates from ,head() and .tail()
start = pd.to_datetime('2010-01-04')
end = pd.to_datetime('2017-07-18')
aapl = web.DataReader('AAPL','google',start,end)
aapl.head()
Re-assign spy_etf with 'start' and 'end' dates so that the number of period matches with aapl. Otherwise, you can't plot the scatter graph
spy_etf = web.DataReader('SPY','google', start, end)
import matplotlib.pyplot as plt
%matplotlib inline
aapl['Close'].plot(label='AAPL',figsize=(10,8))
spy_etf['Close'].plot(label='SPY Index')
plt.legend()
aapl['Cumulative'] = aapl['Close']/aapl['Close'].iloc[0]
spy_etf['Cumulative'] = spy_etf['Close']/spy_etf['Close'].iloc[0]
aapl['Cumulative'].plot(label='AAPL',figsize=(10,8))
spy_etf['Cumulative'].plot(label='SPY Index')
plt.legend()
plt.title('Cumulative Return')
aapl['Daily Return'] = aapl['Close'].pct_change(1)
spy_etf['Daily Return'] = spy_etf['Close'].pct_change(1)
Plot scatter graph to see if there is any correlation visually
plt.scatter(aapl['Daily Return'],spy_etf['Daily Return'],alpha=0.3)
aapl['Daily Return'].hist(bins=100)
spy_etf['Daily Return'].hist(bins=100)
Get Beta & Alpha values from unpacking function .linregress()
beta,alpha,r_value,p_value,std_err = stats.linregress(aapl['Daily Return'].iloc[1:],spy_etf['Daily Return'].iloc[1:])
beta
alpha
r_value
We simulate the scenario by creating a fake stock with return = spy_etf + noise with mean of 0 and std dev of 0.001
spy_etf['Daily Return'].head()
import numpy as np
noise = np.random.normal(0, 0.001, len(spy_etf['Daily Return'].iloc[1:]))
noise
fake_stock = spy_etf['Daily Return'].iloc[1:] + noise
plt.scatter(fake_stock, spy_etf['Daily Return'].iloc[1:], alpha=0.25)
beta,alpha,r_value,p_value,std_err = stats.linregress(spy_etf['Daily Return'].iloc[1:]+noise,spy_etf['Daily Return'].iloc[1:])
Beta value is almost 1. The fake stock is highly correlated with SPY
beta
alpha
Looks like our understanding is correct! If you have a stock that lines up with the index or the market itself, you'll have a really high beta value (close to 1) and a really low alpha value.