Skip to content

Add pain index and pain ratio to the returns accessor - #916

Open
Mattbusel wants to merge 1 commit into
polakowo:masterfrom
tensorust-ai:returns-pain-index
Open

Mattbusel wants to merge 1 commit into
polakowo:masterfrom
tensorust-ai:returns-pain-index

Conversation

@Mattbusel

Copy link
Copy Markdown

Adds the pain index and pain ratio to ReturnsAccessor, next to the ulcer index and Martin ratio from #881:

  • pain_index() / rolling_pain_index(): the mean absolute drawdown over all observations (Thomas Becker, Zephyr Associates; Bacon, Practical Portfolio Performance Measurement and Attribution). The ulcer index squares the drawdowns before averaging; the pain index averages them as they are, so every period under water counts linearly. Returned as a positive fraction.
  • pain_ratio() / rolling_pain_ratio(): annualized return minus annualized risk-free return, divided by the pain index.

Layout and conventions follow #881 one to one:

  • 1d / 2d / rolling numba kernels in returns/nb.py, built on drawdown_1d_nb and annualized_return_1d_nb.
  • Engine-neutral wrappers in returns/dispatch.py that reject engine="rust" through _no_rust(...), since vectorbt-rust has no counterpart.
  • risk_free is a constant per-period rate compounded over ann_factor, the same as martin_ratio.
  • NaN returns count as zero-return observations; minp counts non-NaN returns; empty input gives NaN; a series with no drawdown gives a pain index of 0 and a pain ratio of NaN.

Not added to stats(), so existing stats output is unchanged. Happy to add them there if you want.

Tests:

  • A hand-worked path: returns [0.10, -0.20, 0.25, -0.10, 0.0] give equity 1.10, 0.88, 1.10, 0.99, 0.99 and drawdowns 0, -20%, 0, -10%, -10%, so the pain index is 0.4 / 5 = 0.08. With 5 periods per year the annualized return is -1%, so the pain ratio is -0.125; with a 0.2% per-period risk-free rate it is (-0.01 - (1.002**5 - 1)) / 0.08.
  • An independent numpy reference for the full and rolling versions across three freq / year_freq pairs and three risk-free rates, interior NaN windows, numba vs auto engine equality, accessor defaults, and the explicit-rust rejection in tests/test_engine.py.

tests/test_returns.py and tests/test_engine.py pass locally (99 passed, the rest are vectorbt-rust skips); black is clean on the changed files.

If this is welcome, a follow-up would add Kappa-n on lower partial moments (Kaplan and Knowles 2004), which generalizes the Sortino ratio (n=2) and Omega (n=1, as Omega - 1).

pain_index() is the mean absolute drawdown over all observations (Becker / Zephyr;
Bacon, Practical Portfolio Performance Measurement and Attribution). pain_ratio()
divides the annualized return minus the annualized risk-free return by it, using
the same risk-free convention as martin_ratio(). Both have rolling variants and
1d/2d/rolling numba kernels, following the ulcer index / Martin ratio layout.

This branch has not been deployed

No deployments
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