Statistics tab: use the BIL daily total return as rf (Fund Lab convention)

- No fixed risk-free rate: the statistics page now nets Sharpe, Sortino
  and CAPM alpha against BIL (SPDR 1-3 Month T-Bill) daily total
  returns from the data bundle — the same reference the Fund Lab uses
  for all its alphas (pre-2007 dates fill 0, as in fundlab.decompose).
- metrics: sharpe/sortino/beta_alpha/summary accept a daily rf SERIES
  (or scalar annual rate as before) via a shared excess() helper.
- Also fixes a latent double-count: the old scalar-rf alpha subtracted
  rf twice (once in the returns, once in the intercept term); alpha is
  now mean(excess fund) - beta * mean(excess bench), the standard CAPM
  intercept on excess returns.
- Page caption states the T-bill reference (or warns if BIL is absent).
- Tests: series-rf identities (sharpe/sortino/beta/alpha), benchmark
  self-row beta 1 / alpha 0, caption check.
This commit is contained in:
gmp
2026-09-02 07:03:17 -04:00
parent d5a7bb70b0
commit bdb887dee8
3 changed files with 62 additions and 37 deletions
+23 -16
View File
@@ -166,11 +166,28 @@ def _run() -> None:
check("old statistic names migrated", cols == ["return", "vol", "alpha"],
str(cols))
print("risk-free rate", flush=True)
print("risk-free rate (BIL)", flush=True)
import metrics as _m
import numpy as _np
import pandas as _pd
idx = _pd.bdate_range("2020-01-01", periods=252)
_rng = _np.random.default_rng(7)
r = _pd.Series(_rng.normal(0.0005, 0.01, len(idx)), idx)
b = _pd.Series(_rng.normal(0.0002, 0.008, len(idx)), idx)
c = 0.0002
rf_s = _pd.Series(c, idx)
check("series rf: sharpe equals rf-shifted series",
abs(_m.sharpe(r, rf_s) - _m.sharpe(r - c)) < 1e-12)
check("series rf: sortino equals rf-shifted series",
abs(_m.sortino(r, rf_s) - _m.sortino(r - c)) < 1e-12)
check("series rf: beta invariant, alpha = shifted-series alpha",
abs(_m.beta_alpha(r, b, rf_s)[0] - _m.beta_alpha(r - c, b - c, 0.0)[0]) < 1e-12
and abs(_m.beta_alpha(r, b, rf_s)[1] - _m.beta_alpha(r - c, b - c, 0.0)[1]) < 1e-12)
check("scalar rf equals daily-constant series rf",
abs(_m.sharpe(r, c * _m.ANN) - _m.sharpe(r, rf_s)) < 1e-12)
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
@@ -179,19 +196,9 @@ def _run() -> None:
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()
caps = " ".join(c.value for c in app().main.caption)
check("caption states the T-bill reference",
"3-mo T-bill (BIL)" in caps, caps[:400])
print("date range", flush=True)
# total_return (cumulative) is range-sensitive; the annualized 'return'