CAPM_Single stock

import statsmodels.api as sm
import pandas as pd
import pandas_datareader as web
import matplotlib.pyplot as plt
%matplotlib inline
start = pd.to_datetime('2010-01-04')
end = pd.to_datetime('2020-04-16')

spy_etf = web.DataReader('SPY', 'yahoo', start, end)
spy_etf.info()

在這裏插入圖片描述

spy_etf.head()

在這裏插入圖片描述

aapl = web.DataReader('AAPL', 'yahoo', start, end)
aapl.head()

在這裏插入圖片描述

aapl['Close'].plot(label = 'AAPL',
                   figsize = (12, 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)
fig = plt.figure(figsize = (12, 8))
plt.scatter(aapl['Daily Return'], spy_etf['Daily Return'],alpha = 0.3)

在這裏插入圖片描述

aapl['Daily Return'].hist(bins = 100, figsize = (12, 8))

在這裏插入圖片描述

spy_etf['Daily Return'].hist(bins = 100, figsize = (12, 8))

在這裏插入圖片描述

X = sm.add_constant(spy_etf['Daily Return'].iloc[1:])
reg1 = sm.OLS(aapl['Daily Return'].iloc[1:], X, missing='drop')
results = reg1.fit()
print(results.summary())

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章