Robert Castagna commited on
Commit
6f3b36e
1 Parent(s): f2b8e49

add stats to HRP

Browse files
Files changed (1) hide show
  1. pages/2_Portfolio_Builder.py +42 -0
pages/2_Portfolio_Builder.py CHANGED
@@ -250,3 +250,45 @@ with st.form(key="selecting columns"):
250
  )
251
  st.pyplot(fig3)
252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250
  )
251
  st.pyplot(fig3)
252
 
253
+ # ---------------------------- (Portfolio Statistics) -------------------------------- #
254
+
255
+ spy_prices = obb.equity.price.historical(symbol = "spy", provider="yfinance", start_date=start_date, end_date=end_date).to_df()
256
+
257
+ # Calculate daily returns
258
+ # Ensure you're using the adjusted close prices for accurate return calculation
259
+ benchmark_returns = spy_prices['close'].pct_change().dropna()
260
+
261
+ port.rf = 0.0406 # Risk-free rate
262
+ portfolio_return = np.dot(w, mu)
263
+
264
+ # market return
265
+ spy_daily_return = benchmark_returns
266
+ spy_expected_return = spy_daily_return.mean()
267
+
268
+ # portfolio's beta
269
+ covariance = returns.apply(lambda x: x.cov(spy_daily_return))
270
+ spy_variance = spy_daily_return.var()
271
+ beta_values = covariance / spy_variance
272
+ portfolio_beta = np.dot(w['weights'], beta_values)
273
+ st.write('Portfolio Beta: ', np.round(portfolio_beta,3))
274
+
275
+ # jensens alpha
276
+ expected_return = port.rf + portfolio_beta * (spy_daily_return - port.rf)
277
+ st.write('Jensen\'s Alpha: ', np.round(expected_return.iloc[-1],3))
278
+
279
+ # treynor ratio
280
+ treynor_ratio = (expected_return - port.rf) / portfolio_beta
281
+ st.write('Treynor Ratio: ', np.round(treynor_ratio.iloc[-1],3))
282
+
283
+ # Portfolio volatility
284
+ portfolio_stddev = np.sqrt(np.dot(pd.Series(w['weights']).T, np.dot(covariance, w['weights'])))
285
+
286
+ # Sharpe Ratio, adjusted for the risk-free rate
287
+ sharpe_ratio = rp.RiskFunctions.Sharpe(
288
+ mu=mu,
289
+ cov=cov,
290
+ returns=returns,
291
+ rf=port.rf,
292
+ w=w,
293
+ )
294
+ st.write('Sharpe Ratio: ', np.round(sharpe_ratio, 3))