Statistics tab: fix benchmark alignment + rf-adjusted Sharpe/Sortino/alpha

- Pass the benchmark's DAILY price series to metrics.summary() instead of
  a month-end-resampled one; summary() derives daily returns and
  annualizes with 252d, so the old resample made beta/alpha regress the
  fund's month-end daily returns against whole-month benchmark returns
  (and skewed ann_return_bench).
- New sidebar setting 'Risk-free rate %' (default 4%, persisted) passed
  through to summary(), so Sharpe, Sortino and CAPM alpha are computed
  in excess of rf; noted in the page caption and stats help.
This commit is contained in:
gmp
2026-09-02 06:53:36 -04:00
parent d7302b1646
commit d5a7bb70b0
2 changed files with 44 additions and 10 deletions
+27
View File
@@ -166,6 +166,33 @@ def _run() -> None:
check("old statistic names migrated", cols == ["return", "vol", "alpha"],
str(cols))
print("risk-free rate", flush=True)
run_app("MSFT", "V", stats_order="sharpe,sortino,beta,alpha,ann_return_bench")
t0 = app().main.tabs[0].dataframe[0].value
alpha0 = t0.loc["Current", "alpha"]
sharpe0 = t0.loc["Current", "sharpe"]
beta_bench = float(t0.loc["benchmark: v", "beta"])
alpha_bench = float(t0.loc["benchmark: v", "alpha"][:-1]) / 100
# the benchmark vs ITSELF must be beta 1 / alpha 0 — catches the
# monthly-resampled benchmark (daily-vs-monthly returns mismatch)
check("benchmark row is beta 1 / alpha 0 (daily-aligned regression)",
not app().exception and abs(beta_bench - 1.0) < 1e-9
and abs(alpha_bench) < 1e-9,
f"beta={beta_bench} alpha={alpha_bench}")
rf_widget = next(n for n in app().sidebar.number_input
if "Risk-free" in n.label)
check("default risk-free rate is 4%", rf_widget.value == 4.0,
str(rf_widget.value))
rf_widget.set_value(10.0).run()
t1 = app().main.tabs[0].dataframe[0].value
check("alpha adjusts for the risk-free rate",
not app().exception and t1.loc["Current", "alpha"] != alpha0,
f"{t1.loc['Current', 'alpha']} vs {alpha0}")
check("sharpe adjusts for the risk-free rate",
t1.loc["Current", "sharpe"] != sharpe0,
f"{t1.loc['Current', 'sharpe']} vs {sharpe0}")
rf_widget.set_value(4.0).run()
print("date range", flush=True)
# total_return (cumulative) is range-sensitive; the annualized 'return'
# can coincidentally match at 1-decimal precision