Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Initialize empty dictionaries to save results
- beta,alpha = dict(), dict()
- # Make a nice subplot
- fig, axes = plt.subplots(1,3, dpi=150, figsize=(15,8))
- axes = axes.flatten()
- # Loop on every stock daily return
- for idx, stock in enumerate(stocks_daily_return.columns):
- # Ignoring the date and S&P500 Columns
- if stock != "Date" and stock != "SP500":
- # scatter plot between stock and the S&P500
- stocks_daily_return.plot(kind = "scatter", x = "SP500", y = stock, ax=axes[idx-1])
- # Fit a line (regression using polyfit of degree 1)
- b_, a_ = np.polyfit(stocks_daily_return[stock], stocks_daily_return["SP500"], 1)
- regression_line = b_ * stocks_daily_return["SP500"] + a_
- axes[idx-1].plot(stocks_daily_return["SP500"], regression_line, "-", color = "r")
- # save the regression coeeficient for the current stock
- beta[stock] = b_
- alpha[stock] = a_
- plt.suptitle("Beta estimation: regression between SP500 and individual stock daily performance", size=20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement