-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapm_analysis.py
More file actions
executable file
·63 lines (49 loc) · 2.27 KB
/
Copy pathcapm_analysis.py
File metadata and controls
executable file
·63 lines (49 loc) · 2.27 KB
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
62
63
# Import necessary libraries
from pandas_datareader import DataReader
import numpy as np
import pandas as pd
import datetime
from socket import gaierror
from pandas_datareader._utils import RemoteDataError
from yahoo_fin import stock_info as si
# Define risk-free return rate
risk_free_return = 0.02
# Set pandas options for display
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
# Get all tickers in NASDAQ stock exchange
nasdaq_tickers = si.tickers_nasdaq()
# Define the ticker for index
index_ticker = '^GSPC'
# Define start and end date
start = datetime.datetime.now() - datetime.timedelta(days=365)
end = datetime.date.today()
# List to hold all expected returns
expected_returns = []
# Loop through each ticker in NASDAQ and calculate expected returns
for ticker in nasdaq_tickers:
try:
# Fetch stock and index data
stock = DataReader(ticker, 'yahoo', start, end)
index = DataReader(index_ticker, 'yahoo', start, end)
# Resample to monthly data
return_s1 = stock.resample('M').last()
return_s2 = index.resample('M').last()
# Create a dataframe to hold stock and index returns
dataframe = pd.DataFrame({'s_adjclose': return_s1['Adj Close'], 'm_adjclose': return_s2['Adj Close']}, index=return_s1.index)
dataframe[['s_returns', 'm_returns']] = np.log(dataframe[['s_adjclose', 'm_adjclose']] / dataframe[['s_adjclose', 'm_adjclose']].shift(1))
dataframe = dataframe.dropna()
# Calculate beta and alpha using linear regression
covmat = np.cov(dataframe["s_returns"], dataframe["m_returns"])
beta = covmat[0,1] / covmat[1,1]
beta, alpha = np.polyfit(dataframe["m_returns"], dataframe["s_returns"], deg=1)
# Calculate expected return using CAPM model
expected_return = risk_free_return + beta * (dataframe["m_returns"].mean() * 12 - risk_free_return)
# Print expected return for each ticker
print('{}:'.format(ticker))
print("Expected Return: ", expected_return)
# Append expected return to list
expected_returns.append(expected_return)
except (KeyError, RemoteDataError, TypeError, gaierror) as e:
# Handle exceptions
print(e)