Skip to content

Report total loss in geometric mean returns - #1429

Open
galafis wants to merge 1 commit into
kernc:masterfrom
galafis:fix/geometric-mean-total-loss
Open

galafis wants to merge 1 commit into
kernc:masterfrom
galafis:fix/geometric-mean-total-loss

Conversation

@galafis

@galafis galafis commented Oct 24, 2026

Copy link
Copy Markdown

A zero gross return (a -100% return) currently makes geometric_mean return 0 instead of -1. Consequently, a backtest whose equity falls to zero reports Return (Ann.) as 0% even though Return is -100%.

Handle zero gross returns explicitly as a geometric mean return of -1. Preserve the existing behavior for negative gross returns, NaNs and empty inputs.

Tests cover total loss before and after positive returns, missing observations, ordinary positive growth factors, empty data, and an end-to-end leveraged strategy whose equity is exhausted. The independent product-root formula, and scipy.stats.gmean for zero gross returns, give -1. Reference: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.gmean.html .

Before: four total-loss helper subcases and the end-to-end annualized-return assertion fail. After: all 26 TestBacktest tests pass. flake8 on both changed files and git diff --check pass. Python 3.12.14, pandas 3.1.0, NumPy 2.5.4.

Full suite after installing test extras: 84 tests OK, 1 skipped. Existing pandas deprecation warnings remain. Mypy reports the same 10 errors in unchanged _plotting.py as the clean baseline (error lines compared identical).

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