Skip to content

test(backtesting): correct wrong oracles in performance metric tests (#14)#38

Open
bradsmithmba wants to merge 1 commit into
cloudtrainerwork:masterfrom
bradsmithmba:fix/test-assertion-oracles
Open

test(backtesting): correct wrong oracles in performance metric tests (#14)#38
bradsmithmba wants to merge 1 commit into
cloudtrainerwork:masterfrom
bradsmithmba:fix/test-assertion-oracles

Conversation

@bradsmithmba

Copy link
Copy Markdown

Summary

Three tests in tests/backtesting/test_performance.py asserted incorrect expected values — they were green/red for the wrong reasons and didn't actually validate the metrics. Fixing the oracles so the tests assert what's correct.

Closes #14.

The three fixes

test_win_rate_calculation — the pnl series [100, -50, 200, 150, -75, 300, -25, 180, -100, 250] has 6 positive entries, not 7. Win rate is 0.6.

- # 7 wins, 3 losses ... expected_win_rate = 0.7
+ # 6 wins, 4 losses ... expected_win_rate = 0.6

test_sharpe_ratio_calculation — the test used a constant return series (np.full(252, 0.001)). A constant series has zero standard deviation, which _calculate_sharpe correctly maps to 0.0 (undefined Sharpe), so assert sharpe_ratio > 10 could never pass. The "no volatility → high Sharpe" premise is backwards. Give the series small deterministic volatility so it genuinely exercises the high-Sharpe case, and replace assert annualized_return > 10 (which asserted >1000%) with > 0.10.

test_annualization — 252 freq='D' rows span only 251 calendar days (~0.69 yr). Since the calculator annualizes by elapsed calendar time, a 20% total return over 251 days correctly annualizes to ~30%, so asserting ~20% was wrong. Use a full calendar year of dates so a 20% total return annualizes to ~20% (computed: 0.2008).

Result

$ pytest tests/backtesting/test_performance.py -q
7 passed

(Previously: 3 failed — the three above — and 4 passed.)

Note

This file is also touched by the PR for #12 (#37), which adds a separate bar-count annualization test. The two were verified to merge cleanly (no conflict) in either order.

🤖 Generated with Claude Code

Three tests asserted incorrect expected values and passed/failed for the
wrong reasons:

- test_win_rate_calculation: pnl [100,-50,200,150,-75,300,-25,180,-100,250]
  is 6 wins / 4 losses = 0.6, but the comment said "7 wins" and it asserted
  0.7. Corrected to 0.6.
- test_sharpe_ratio_calculation: used a constant return series (zero std),
  which the calculator maps to Sharpe 0 — so `sharpe_ratio > 10` could never
  hold. Give the series small, deterministic volatility so it genuinely
  exercises the high-Sharpe case, and replace the nonsensical
  `annualized_return > 10` (>1000%) with `> 0.10`.
- test_annualization: 252 freq='D' rows span only 251 calendar days, so a
  20% total return correctly annualizes to ~30%, not the asserted 20%. Use a
  full calendar year of dates so 20% total annualizes to ~20%.

All seven tests in the file now pass and assert correct values.

Closes cloudtrainerwork#14

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.

Test suite has incorrect assertions — passing tests are asserting wrong expected values

1 participant