Lirsen Myrtaj wborders commited on
Commit
fc101f7
1 Parent(s): 22f8bc3

Added benchmark component and risk assignment and color to bubble visualization (#29)

Browse files

- Added benchmark component and risk assignment and color to bubble visualization (b3daf186ec45d202d0982bc46938812f72735ab6)


Co-authored-by: Warren Borders <wborders@users.noreply.huggingface.co>

Files changed (1) hide show
  1. plots.py +21 -13
plots.py CHANGED
@@ -8,9 +8,9 @@ import plotly.express as px
8
 
9
 
10
  def beta(stock_df, choices):
11
- symbols, weights, investment, rf, A_coef = choices.values()
12
  tickers = symbols
13
- tickers.append('sp500')
14
  #print(tickers)
15
  quantity = weights
16
  selected_stocks = stock_df[tickers]
@@ -32,15 +32,15 @@ def beta(stock_df, choices):
32
  alpha_list = []
33
  stocks_daily_return = df_stocks
34
  for i in stocks_daily_return.columns:
35
- if i != 'Date' and i != 'sp500':
36
  # stocks_daily_return.plot(kind = 'scatter', x = 'A', y = i)
37
- b, a = np.polyfit(stocks_daily_return['sp500'], stocks_daily_return[i], 1)
38
  # plt.plot(stocks_daily_return['sp500'], b * stocks_daily_return['sp500'] + a, '-', color = 'r')
39
  beta_list.append(round(b, 2))
40
  alpha_list.append(round(a, 2))
41
  # plt.show()
42
  # Formats the results
43
- symbols.remove('sp500')
44
  beta = {'Assets': symbols, 'Beta': beta_list}
45
  alpha = {'Assets': symbols, 'Alpha': alpha_list}
46
  # Creates a header for streamlit
@@ -54,10 +54,10 @@ def beta(stock_df, choices):
54
 
55
 
56
  def ER(stock_df, choices):
57
- symbols, weights, investment, rf, A_coef = choices.values()
58
  symbols_ =symbols.copy()
59
  tickers = symbols
60
- tickers.append('sp500')
61
  #print(tickers)
62
  quantity = weights
63
  selected_stocks = stock_df[tickers]
@@ -78,9 +78,9 @@ def ER(stock_df, choices):
78
  # print(df_stocks)
79
 
80
  for i in stocks_daily_return.columns:
81
- if i != 'Date' and i != 'sp500':
82
  # stocks_daily_return.plot(kind = 'scatter', x = 'A', y = i)
83
- b, a = np.polyfit(stocks_daily_return['sp500'], stocks_daily_return[i], 1)
84
  # plt.plot(stocks_daily_return['sp500'], b * stocks_daily_return['sp500'] + a, '-', color = 'r')
85
  beta[i] = round(b, 2)
86
  alpha[i] = round(a, 2)
@@ -92,14 +92,14 @@ def ER(stock_df, choices):
92
  # rf = 0 assuming risk-free rate of 0
93
  rf = 0
94
  # rm - annualize retun
95
- rm = stocks_daily_return['sp500'].mean() * 252
96
  for i in keys:
97
  ER_.append( round(rf + (beta[i] * (rm - rf)), 2))
98
 
99
  #for i in keys:
100
  # print('Expected Return based on CAPM for {} is {}%'.format(i, ER_[i]))
101
  #print(ER)
102
- symbols.remove('sp500')
103
  #st.subheader('Expected Annual Return Based on CAPM Model')
104
 
105
  Expected_return = {'Assets': symbols_, 'Expected Annual Return': ER_}
@@ -187,24 +187,32 @@ def display_heat_map(stock_df,choices):
187
  # st.subheader('Portfolio Historical Cumulative Returns Based On Inputs!')
188
  # st.line_chart(cumulative_profit)
189
  def buble_interactive(stock_df,choices):
190
- symbols, weights, investment, rf, A_coef = choices.values()
191
  beta,cash_value_weights = ER(stock_df,choices)
192
  my_list = []
 
193
  for i in beta.values():
194
  my_list.append(i)
 
 
 
 
 
 
195
 
196
  df_final =pd.DataFrame()
197
  df_final['ticker'] = symbols
198
  df_final['quantities'] = weights
199
  df_final['cash_value'] =cash_value_weights
200
  df_final['Beta'] = my_list
 
201
 
202
  fig = px.scatter(
203
  df_final,
204
  x="quantities",
205
  y="Beta",
206
  size="cash_value",
207
- #color="continent",
208
  hover_name="ticker",
209
  log_x=True,
210
  size_max=60,
 
8
 
9
 
10
  def beta(stock_df, choices):
11
+ symbols, weights, benchmark, investing_style, rf, A_coef = choices.values()
12
  tickers = symbols
13
+ tickers.append(benchmark)
14
  #print(tickers)
15
  quantity = weights
16
  selected_stocks = stock_df[tickers]
 
32
  alpha_list = []
33
  stocks_daily_return = df_stocks
34
  for i in stocks_daily_return.columns:
35
+ if i != 'Date' and i != benchmark:
36
  # stocks_daily_return.plot(kind = 'scatter', x = 'A', y = i)
37
+ b, a = np.polyfit(stocks_daily_return[benchmark], stocks_daily_return[i], 1)
38
  # plt.plot(stocks_daily_return['sp500'], b * stocks_daily_return['sp500'] + a, '-', color = 'r')
39
  beta_list.append(round(b, 2))
40
  alpha_list.append(round(a, 2))
41
  # plt.show()
42
  # Formats the results
43
+ symbols.remove(benchmark)
44
  beta = {'Assets': symbols, 'Beta': beta_list}
45
  alpha = {'Assets': symbols, 'Alpha': alpha_list}
46
  # Creates a header for streamlit
 
54
 
55
 
56
  def ER(stock_df, choices):
57
+ symbols, weights, investing_style, benchmark, rf, A_coef = choices.values()
58
  symbols_ =symbols.copy()
59
  tickers = symbols
60
+ tickers.append(benchmark)
61
  #print(tickers)
62
  quantity = weights
63
  selected_stocks = stock_df[tickers]
 
78
  # print(df_stocks)
79
 
80
  for i in stocks_daily_return.columns:
81
+ if i != 'Date' and i != benchmark:
82
  # stocks_daily_return.plot(kind = 'scatter', x = 'A', y = i)
83
+ b, a = np.polyfit(stocks_daily_return[benchmark], stocks_daily_return[i], 1)
84
  # plt.plot(stocks_daily_return['sp500'], b * stocks_daily_return['sp500'] + a, '-', color = 'r')
85
  beta[i] = round(b, 2)
86
  alpha[i] = round(a, 2)
 
92
  # rf = 0 assuming risk-free rate of 0
93
  rf = 0
94
  # rm - annualize retun
95
+ rm = stocks_daily_return[benchmark].mean() * 252
96
  for i in keys:
97
  ER_.append( round(rf + (beta[i] * (rm - rf)), 2))
98
 
99
  #for i in keys:
100
  # print('Expected Return based on CAPM for {} is {}%'.format(i, ER_[i]))
101
  #print(ER)
102
+ symbols.remove(benchmark)
103
  #st.subheader('Expected Annual Return Based on CAPM Model')
104
 
105
  Expected_return = {'Assets': symbols_, 'Expected Annual Return': ER_}
 
187
  # st.subheader('Portfolio Historical Cumulative Returns Based On Inputs!')
188
  # st.line_chart(cumulative_profit)
189
  def buble_interactive(stock_df,choices):
190
+ symbols, weights, investing_style,benchmark, rf, A_coef = choices.values()
191
  beta,cash_value_weights = ER(stock_df,choices)
192
  my_list = []
193
+ my_colors = []
194
  for i in beta.values():
195
  my_list.append(i)
196
+ if i < 0.3:
197
+ my_colors.append("Conservative")
198
+ if i >= 0.3 and i <= 1.1:
199
+ my_colors.append("Moderate Risk")
200
+ if i > 1.1:
201
+ my_colors.append("Risky")
202
 
203
  df_final =pd.DataFrame()
204
  df_final['ticker'] = symbols
205
  df_final['quantities'] = weights
206
  df_final['cash_value'] =cash_value_weights
207
  df_final['Beta'] = my_list
208
+ df_final['Risk'] = my_colors
209
 
210
  fig = px.scatter(
211
  df_final,
212
  x="quantities",
213
  y="Beta",
214
  size="cash_value",
215
+ color="Risk",
216
  hover_name="ticker",
217
  log_x=True,
218
  size_max=60,