Advertisement
Offew1995

Beta estimation

Jun 22nd, 2023
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | Cryptocurrency | 0 0
  1. # Initialize empty dictionaries to save results
  2. beta,alpha = dict(), dict()
  3.  
  4. # Make a nice subplot
  5. fig, axes = plt.subplots(1,3, dpi=150, figsize=(15,8))
  6. axes = axes.flatten()
  7.  
  8. # Loop on every stock daily return
  9. for idx, stock in enumerate(stocks_daily_return.columns):
  10.  
  11. # Ignoring the date and S&P500 Columns
  12.     if stock != "Date" and stock != "SP500":
  13.  
  14.         # scatter plot between stock and the S&P500
  15.         stocks_daily_return.plot(kind = "scatter", x = "SP500", y = stock, ax=axes[idx-1])
  16.  
  17.         # Fit a line (regression using polyfit of degree 1)
  18.         b_, a_ = np.polyfit(stocks_daily_return[stock], stocks_daily_return["SP500"], 1)
  19.  
  20.         regression_line = b_ * stocks_daily_return["SP500"] + a_
  21.         axes[idx-1].plot(stocks_daily_return["SP500"], regression_line, "-", color = "r")
  22.  
  23.         # save the regression coeeficient for the current stock
  24.         beta[stock] = b_
  25.         alpha[stock] = a_
  26.  
  27. plt.suptitle("Beta estimation: regression between SP500 and individual stock daily performance", size=20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement