Repository navigation
fix(evaluate): return scalar beta with consistent covariance normalization - #2364
Natsuki Komatsu (nkjapan) wants to merge 3 commits into
Conversation
|
@microsoft-github-policy-service agree |
|
The diagnosis checks out: Three input cases the new tests don't pin down, which I ran against the new expression
None of these block the PR; the first is probably worth one assertion, since the fix changes the return type from matrix to scalar and any caller that relied on a |
|
|
Thanks for reviewing this and for the suggestions! I’ve added regression tests for the constant-benchmark case and clarified the input assumptions in the docstring, including positional matching and NaN handling. The focused tests pass, and the calculation itself is unchanged. |
Description
Fix
get_betainqlib/contrib/evaluate_portfolio.pyto return the scalar ratiocov_r_b[0, 1] / cov_r_b[1, 1]. Taking both terms from the same covariance matrix also keeps their normalization consistent.Add return-value documentation and 10 regression cases covering beta and its use in
get_alpha.Motivation and Context
The current implementation divides the entire 2×2 result of
np.cov(r, b)bynp.var(b), so it returns a matrix rather than a scalar. It also mixes the sample covariance'sN - 1denominator with the population variance'sNdenominator.For five nonconstant observations with
r == b, this produces a matrix filled with 1.25 instead of beta 1. The incorrect shape and value propagate intoget_alpha; a benchmark compared with itself should have alpha 0.How Has This Been Tested?
tests/misc/test_evaluate_portfolio.py: self-beta at three sample sizes, four affine benchmark scales, comparison withscipy.stats.linregress, and self-alpha at two risk-free rates. All 10 fail on the unmodified base and pass with this fix.5dbc5086f9a14fe1c8d6d81fd40c9e1fe146148f: Windows, Ubuntu 22.04/24.04, and macOS 14/15, each with Python 3.8–3.12.Types of changes