Skip to content

Add hypothesis property-based tests for statistical invariants; fix sortino ZeroDivisionError#379

Merged
tschm merged 8 commits intomainfrom
copilot/add-property-based-tests
Mar 23, 2026
Merged

Add hypothesis property-based tests for statistical invariants; fix sortino ZeroDivisionError#379
tschm merged 8 commits intomainfrom
copilot/add-property-based-tests

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 23, 2026

Adds property-based tests using hypothesis to verify mathematical invariants that must hold for any valid returns series. In doing so, exposes and fixes a latent ZeroDivisionError in sortino() when no negative returns are present.

New: property-based tests (tests/test_jquantstats/test_properties.py)

Six invariants tested across arbitrary generated return sequences:

Test Invariant
test_sharpe_nonnegative_for_all_positive_returns Sharpe ≥ 0 when all returns > 0
test_max_drawdown_always_nonnegative Max drawdown ≥ 0 (any returns)
test_max_drawdown_zero_for_nonnegative_returns Max drawdown = 0 when all returns ≥ 0
test_win_rate_in_unit_interval Win rate ∈ [0, 1]
test_volatility_nonnegative Volatility ≥ 0
test_sortino_nonnegative_for_all_positive_returns Sortino ≥ 0 (or ±∞) when all returns > 0

Tests use @pytest.mark.property (already registered in pytest.ini) and @settings(max_examples=100). The module uses pytest.importorskip("hypothesis") so tests are automatically skipped when hypothesis is not installed, and run normally when it is available — no changes to pyproject.toml are required.

Bug fix: sortino() ZeroDivisionError (_stats.py)

When every return is non-negative, downside_deviation = 0. Previously this raised ZeroDivisionError; now returns the mathematically correct value:

# Before: raises ZeroDivisionError
data.stats.sortino()  # with all-positive returns

# After: returns inf (no downside risk)
# mean > 0  → +inf
# mean < 0  → -inf  (degenerate)
# mean == 0 → nan
Original prompt

This section details on the original issue you should resolve

<issue_title>Add property-based tests — Use hypothesis to test statistical invariants (e.g. Sharpe ≥ 0 when all returns positive, max drawdown ≤ 0)</issue_title>
<issue_description></issue_description>

Comments on the Issue (you are @copilot in this section)


⚡ Quickly spin up Copilot coding agent tasks from anywhere on your macOS or Windows machine with Raycast.

@tschm
Copy link
Copy Markdown
Member

tschm commented Mar 23, 2026

@copilot: no need to install hypothesis

@tschm tschm marked this pull request as ready for review March 23, 2026 10:17
Copilot AI changed the title [WIP] Add property-based tests using hypothesis for statistical invariants Add hypothesis property-based tests for statistical invariants; fix sortino ZeroDivisionError Mar 23, 2026
Copilot AI requested a review from tschm March 23, 2026 10:20
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 23, 2026

@copilot: no need to install hypothesis

Removed hypothesis from pyproject.toml. The test module now uses pytest.importorskip("hypothesis") so tests skip gracefully when hypothesis isn't installed and run automatically when it is available. Fixed in commit 034a946.

@tschm tschm merged commit 3f42d14 into main Mar 23, 2026
21 checks passed
@tschm tschm deleted the copilot/add-property-based-tests branch March 23, 2026 10:45
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.

Add property-based tests — Use hypothesis to test statistical invariants (e.g. Sharpe ≥ 0 when all returns positive, max drawdown ≤ 0)

2 participants