Skip to content

Fix daily risk-free price interval alignment - #386

Merged
timkpaine merged 2 commits into
pmorissette:masterfrom
fallenmi:codex/ffn-align-daily-riskfree-intervals
Sep 30, 2026
Merged

timkpaine merged 2 commits into
pmorissette:masterfrom
fallenmi:codex/ffn-align-daily-riskfree-intervals

Conversation

@fallenmi

Copy link
Copy Markdown
Contributor

Problem

When PerformanceStats receives a risk-free price series on a denser calendar than the asset, daily excess returns currently compare different holding intervals. For example, a Friday-to-Monday asset return is reduced by the benchmark's Sunday-to-Monday return.

This produces nonzero excess performance even when the asset is the risk-free holding itself:

import ffn
import pandas as pd

rf = pd.Series([100., 101., 102., 103., 104.],
               index=pd.date_range("2024-01-05", periods=5))
asset = rf.iloc[[0, 3, 4]].rename("same_asset")
stats = ffn.PerformanceStats(asset, rf=rf)
print(stats.daily_sharpe, stats.daily_sortino)

On the base commit this prints 11.224972160321826 inf. Both holdings have identical endpoint prices, so interval excess returns are zero; both ratios should be NaN, consistent with the existing zero-excess convention.

Change

  • Sample risk-free prices on the asset's observed daily endpoints before calculating risk-free returns.
  • Use the asset's timezone for daily sampling; reject mixing naive and aware timestamps.
  • Leave intervals with missing endpoint prices unavailable rather than forward/backward filling them.
  • Keep scalar risk-free rates, standalone ratio functions, frequency inference, and monthly/yearly branches unchanged.

The existing daily price-series expectation in test_performance_stats_reuses_excess_returns intentionally changes: it previously preserved returns calculated on the benchmark's own calendar. Its scalar/dtype/call-count checks and monthly/yearly expectations remain. New regression tests cover equal holdings on daily/business/gapped/intraday calendars, nonzero excess returns with a missing endpoint, GroupStats date-range rebuilding, and mixed timezone rejection.

Validation

  • Full suite: 841 passed, no skips, on Python 3.11.11 with both NumPy 2.4.6 / pandas 3.0.6 and NumPy 1.26.4 / pandas 2.2.3.
  • The same candidate test tree against unchanged base implementation: 826 passed, 15 failed (8 corrected existing price-series expectations and 7 new regression cases). Unchanged base's original suite: 831 passed.
  • Separate 216-case scalar interval oracle: 216 passed in each dependency configuration; unchanged base: 48 passed, 168 failed. Covers calendar gaps, missing endpoints, timezone/DST, benchmark direction, and annualization factors 252/365.
  • Independent additional timezone/GroupStats/dtype checks found no introduced failure in their bounded sample; two sparse-series reduction errors reproduced identically on base and candidate.
  • Ruff check and format check passed for both changed files.

All test inputs were synthetic or bundled fixtures; runtime networking was disabled. These checks do not establish all dependency versions, exact intraday fixing-time equivalence, or correctness of the unchanged monthly/yearly alignment.

AI assistance disclosure: Claude Code (Opus 5.5) identified the caller issue and proposed this patch; Codex independently reproduced it, ran the tests/oracles, reviewed the change, and prepared this PR.

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.02%. Comparing base (3ef2b1b) to head (f739e4f).
⚠️ Report is 329 commits behind head on master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff             @@
##           master     #386       +/-   ##
===========================================
+ Coverage   57.32%   81.02%   +23.70%     
===========================================
  Files           4        4               
  Lines        1174     1481      +307     
  Branches      254      304       +50     
===========================================
+ Hits          673     1200      +527     
+ Misses        435      217      -218     
+ Partials       66       64        -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@timkpaine
timkpaine merged commit b1396bf into pmorissette:master Sep 30, 2026
11 checks passed

This branch was previously deployed

1 inactive deployment
dev — f739e4f1 Deployed Sep 29, 2026 by timkpaine via build (3.11, >=3,<4, >=2,<3) #226
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.

3 participants