-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCAPM_functions.py
30 lines (27 loc) · 1 KB
/
CAPM_functions.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
import plotly.express as px
import numpy as np
# function to plot interactive plotly chart
def interactive_plot(df):
fig=px.line()
for i in df.columns[1:]:
fig.add_scatter(x=df['Date'], y=df[i], name=i)
fig.update_layout(width= 450, margin=dict(l=20,r=20,t=50,b=20), legend=dict(orientation='h',yanchor='bottom', y=1.02, xanchor='right', x=1))
return fig
def normalize(df_2):
df=df_2.copy()
for i in df.columns[1:]:
df[i]=df[i]/df[i][0]
return df
# function to calculate daily return
def daily_return(df):
df_daily_return=df.copy()
for i in df.columns[1:]:
for j in range(1, len(df)):
df_daily_return[i][j]=(df[i][j]-df[i][j-1])/df[i][j-1]*100
df_daily_return[i][0]=0
return df_daily_return
# function to create beta
def calculate_beta(stocks_daily_return, stock):
rm=stocks_daily_return['sp500'].mean()*252
b,a=np.polyfit(stocks_daily_return['sp500'],stocks_daily_return[stock],1)
return b,a