Skip to content

Rust phase 2: metric kernel parity (BacktestMetrics + BacktestSummaryMetrics) #523

@MDUYN

Description

@MDUYN

Goal

Reimplement the per-run BacktestMetrics and cross-window BacktestSummaryMetrics calculations in Rust, exposed as PyO3 functions. Achieve numerical parity with the existing Python implementation (within agreed float tolerance) and benchmark the speedup.

Tasks

  • Catalog every metric currently produced (sources of truth: investing_algorithm_framework/services/metrics/ and docusaurus/docs/Getting Started/metrics.md). Group by domain: returns, risk-adjusted, drawdown, per-trade, win/loss, exposure, multi-window robustness.
  • Define Rust-side input types: PortfolioSnapshot, Trade, RiskFreeRate. Use polars Series / ndarray for timeseries inputs to enable zero-copy conversion from numpy / pandas.
  • Implement per-run metrics: CAGR, Sharpe, Sortino, Calmar, max drawdown + duration, recovery factor, profit factor, win rate, exposure, equity curve / drawdown series, monthly + yearly returns, return distribution.
  • Implement summary aggregator: stability score, consistency score, number of profitable windows, cross-window roll-ups.
  • Optional metrics: List[str] filter parity with Python.
  • PyO3 functions: compute_backtest_metrics(snapshots, trades, risk_free_rate, metrics) -> dict and compute_backtest_summary(per_run_metrics) -> dict. Return plain dicts so the Python layer can populate the existing dataclasses unchanged.
  • Parity test suite: for every backtest fixture under tests/resources/, assert Rust output equals Python output within rtol=1e-9 on counts and rtol=1e-6, atol=1e-12 on float metrics. Document any unavoidable divergence (e.g. parallel reduction order) with a tighter tolerance.
  • Property-based tests with hypothesis on the Python wrapper, comparing pure-Python vs native paths.
  • pytest-benchmark comparison: pure-Python vs native on a 100-window summary. Target ≥ 20× speedup.

Acceptance criteria

  • All existing metric tests pass with IAF_NATIVE=1 and IAF_NATIVE=0.
  • New parity test suite green.
  • Benchmark report committed to the repo (markdown + JSON).
  • No public Python API change.

Dependencies

  • Phase 1 (crate scaffold) must be merged.

Part of the Epic: hybrid Python + Rust acceleration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance / memory optimizationrelease:backlogNot yet targetedrustRust port / native accelerationtype:featureNew capability

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions