Skip to content

feat(backtest): expand benchmark-relative metrics with Beta, Jensen's Alpha, Treynor, and Capture Ratios - #303

Open
huuan26 wants to merge 3 commits into
OpenByteInc:mainfrom
huuan26:feat/backtest-benchmark-relative-metrics
Open

huuan26 wants to merge 3 commits into
OpenByteInc:mainfrom
huuan26:feat/backtest-benchmark-relative-metrics

Conversation

@huuan26

@huuan26 huuan26 commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Expands the benchmark-relative performance metrics in app/services/backtest/metrics.py and Strategy API V2:

  1. Shared Curve Alignment: Extracts _align_curves_and_returns to unify timestamp inner joining, percentage return computation, non-continuous gap filtering, and exchange-calendar session alignment across all benchmark metrics.
  2. Beta ($\beta$) & Systematic Risk: Implements sample covariance over benchmark variance ($\beta = \frac{\text{Cov}(R_p, R_b)}{\text{Var}(R_b)}$). Handles flat/zero benchmark variance safely via status "zero_benchmark_variance".
  3. Jensen's Alpha ($\alpha_{\text{ann}}$): Computes annualized risk-adjusted excess return over CAPM expected return:
    $$\alpha_{\text{ann}} = (R_{p, \text{ann}} - R_f) - \beta (R_{b, \text{ann}} - R_f)$$
  4. Treynor Ratio: Evaluates excess return per unit of systematic risk:
    $$\text{Treynor} = \frac{R_{p, \text{ann}} - R_f}{\beta}$$
    Safely returns None when $|\beta| \le 10^{-15}$ to prevent division by zero or infinite values.
  5. Correlation & $R^2$: Computes Pearson correlation coefficient ($r \in [-1, 1]$) and coefficient of determination ($R^2 \in [0, 1]$), with edge-case clamping and handling of flat portfolio variance.
  6. Capture Ratios:
    • upCapture: Compounded portfolio return divided by compounded benchmark return across periods where $R_b > 0$.
    • downCapture: Compounded portfolio return divided by compounded benchmark return across periods where $R_b < 0$.
    • captureRatio: Ratio of upside capture to downside capture ($\frac{\text{Up Capture}}{\text{Down Capture}}$).
    • Also tracks upMarketPeriods and downMarketPeriods for market regime diagnostics.
  7. Comprehensive Integration & Full Backward Compatibility:
    • calculate_benchmark_relative_metrics integrates Information Ratio, Beta/Alpha, and Capture metrics in a single structured result.
    • Preserves 100% backward compatibility for all existing callers of calculate_information_ratio and existing fields of result["benchmarkRelativeMetrics"].
    • Wired into StrategyV2BacktestService (app/services/strategy_v2/service.py).
  8. Documentation:
    • Updated docs/product/BACKTEST_CENTER.md detailing the new metrics, formulas, and edge cases.

Changes

  • backend_api_python/app/services/backtest/metrics.py:
    • Add _align_curves_and_returns helper.
    • Implement calculate_beta_and_alpha.
    • Implement calculate_capture_ratios.
    • Implement calculate_benchmark_relative_metrics.
    • Export new functions in __all__.
  • backend_api_python/app/services/strategy_v2/service.py:
    • Import and call calculate_benchmark_relative_metrics for result["benchmarkRelativeMetrics"].
  • backend_api_python/tests/test_backtest_metrics.py:
    • Add unit tests for Beta, Jensen's Alpha (with zero and positive risk-free rate), Treynor Ratio, Correlation, R-squared, Capture Ratios (both markets, up-only, down-only), zero benchmark variance, zero beta handling, insufficient history, and input validation.
    • Total 22/22 unit tests passing.
  • docs/product/BACKTEST_CENTER.md:
    • Document Beta, Jensen's Alpha, Treynor Ratio, Correlation, $R^2$, and Capture Ratios.

Test plan

  • pytest backend_api_python/tests/test_backtest_metrics.py passes (22/22 tests in 0.91s)
  • ruff check app/services/backtest/metrics.py tests/test_backtest_metrics.py app/services/strategy_v2/service.py passes with zero violations
  • git diff --check passes with zero whitespace/formatting issues
  • Verified full backward compatibility with existing calculate_information_ratio contract

API documentation (if routes/schemas changed)

  • No routes or OpenAPI schemas changed; expands benchmarkRelativeMetrics dictionary payload in backtest results

… Alpha, Treynor, and Capture Ratios

- Add _align_curves_and_returns helper in app/services/backtest/metrics.py to share common benchmark curve alignment and interval validation.
- Implement calculate_beta_and_alpha for systematic risk (Beta), CAPM excess return (Jensen's Alpha), Treynor Ratio, Pearson Correlation, and R-squared.
- Implement calculate_capture_ratios for compounded Up-Market Capture, Down-Market Capture, and Overall Capture Ratio.
- Implement calculate_benchmark_relative_metrics combining Information Ratio, Beta/Alpha, and Capture metrics while maintaining backward compatibility with calculate_information_ratio.
- Update Strategy API V2 backtest service to compute comprehensive benchmark-relative metrics.
- Document metrics and formulas in docs/product/BACKTEST_CENTER.md.
- Add comprehensive unit test coverage in tests/test_backtest_metrics.py.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant