Report the money-weighted return over the period
Summary
Closes #156.
Portfolio::Xirr solves for an annual rate, and Performance#money_weighted published it whatever the period length. A gain of 3.57% over two days was reported as 603.36 — 60,336%. R4 already withholds the annualised time-weighted return below a year for exactly that reason, and GIPS requires that a return for a period under a year is not annualised: extrapolating a partial-year return states a figure the assets never earned.
The solver's time unit is now a parameter, defaulting to a year, so every existing caller is unchanged. money_weighted asks for the span the rows cover, so mwr is the return over the period; annualized_mwr is its annualised form under R4's existing 365-day guard and the existing total-loss guard in #annualize.
Solved in period units, not de-annualised afterwards
The two are the same root read in different units — verified to 1e-9 on a series with an intermediate flow. They are not interchangeable in practice: the annual form of a short period is often unreachable.
1d +100% annual=nil period=+100.0000%
1d +500% annual=nil period=+500.0000%
2d +300% annual=nil period=+300.0000%
A one-day +100% needs an annual rate of about 7.5e109 — beyond what Newton reaches and outside bisection's bracket — so a post-hoc conversion reports nothing exactly where the period figure is ordinary.
Two deliberate choices, both measurable
The closing value is dated at the end of the last row's day. A row's value_open is the previous day's close and its value_close is that day's, so N rows are N days of exposure and N daily returns. Dating the closing flow at the last row's own date — the start of that day — discounted over N−1 days money that was at work for N, and that single day was the entire gap between annualized_mwr and the annual XIRR of the same flows:
| span | mwr | annualize() | annual XIRR | gap | |
|---|---|---|---|---|---|
| Closing at the last row's date | 364 | 0.13475242 | 0.13475242 | 0.13514658 | 3.94 bp |
| Closing at the end of that day | 365 | 0.13462698 | 0.13462698 | 0.13462698 | 0.00 bp |
Both annualised figures therefore share one denominator. The span now equals the interval the time-weighted figure chains over and the one Period#days counts, so annualized_mwr is the annual XIRR exactly and no second convention is introduced. An earlier revision of this PR documented the 3.94 bp as an accepted limit; it is fixed instead.
The period figure is unchanged where the series has flows only at its ends — the rate is closing/opening - 1 whatever unit it is read in — so the two-day fixture still reports +3.571429%.
Scope and dependencies
- Issue: #156 — decision record, triage plan
- Parent PR/branch, if stacked: none — branched from
mainat6ef977d - Intentionally out of scope: the 2.4 label for the periodic figure; Modified Dietz or any second money-weighted method (R8 defines the figure as an IRR);
RATE_FLOOR = -0.999999, which withholds a near-total loss under either parameterisation; the inclusive-count convention in#annualize - Overlaps #164, which also edits
performance.rb(different methods). Whichever merges second rebases.
Evidence checklist
- Each issue in this PR has its own acceptance criteria and no unrelated issue work is included.
- Contract rows touched by this change name existing tests — R4, R8 and R9 amended in place, citations unchanged, R1–R17 intact so
EXPECTED_ROWSis untouched. Verified: 17 rows, 0 unresolved. - Any gate test has an observed failing mutation transcript attached —
Portfolio::Xirr(7/7) andPortfolio::Performance(5/5) both posted; see the mutation transcript comments. - Fixtures used as evidence assert internal consistency — built through
lay_balance, which refuses a day whose components do not reach the closing figure. - Financial calculations use
BigDecimaland documented rounding points — unchanged. R9'sFloatexception is unchanged and still recorded. - De-identification uses token-shape allowlists; no personal data is included.
- Files changed, decisions, tests/runtime, risks, and next unblocked issue are documented below.
Files changed
| File | Change |
|---|---|
app/models/portfolio/xirr.rb | days_per_unit: parameter, defaulting to DAYS_PER_YEAR; years_for → units_for |
app/models/portfolio/performance.rb | money_weighted solves over the row span; annualized_money_weighted_return added |
docs/portfolio/returns-contract.md | R4, R8, R9 amended |
test/models/portfolio/xirr_test.rb | Annual-default control, the unit identity, the unreachable-annual case, the unit guard |
test/models/portfolio/performance_test.rb | The 2-day fixture, and the annualisation boundary |
Validation
| Check | Result |
|---|---|
| Headline fixture, 1,000 → 1,035.714286 over a 2-day span | annual 603.36 (reproduces the issue's figure exactly) → period +3.571429% |
| Identity, series with an intermediate flow | PV at the annual rate and at the period rate are the same root, 1.705e-13 both |
| Long period, 400 days | period +0.30000000 → annualised over the span +0.27049602 = the annual XIRR exactly |
| Contract gate | 17 returns rows, 0 unresolved |
ruby -c on all five files | Syntax OK |
bin/rails test test/models/portfolio/ | 117 runs, 500 assertions, 0 failures |
bin/rubocop | 2041 files, no offenses |
bin/rails portfolio:verify_contract_coverage | 40 methodology and 17 returns rows verified |
The solver evidence above is stdlib-only and was run directly against the changed class. The Minitest suite now runs too, in the repository's own dev container (sure_devcontainer-app-1) on the pinned Ruby 3.4.9 against its Postgres. The earlier claim that it could not be run was true of a different environment.
Risks, blockers, and follow-up
- Three existing assertions read
mwrover a ~365-day period (0.1346, tolerance0.002). They now read the period figure, ≈0.13416— inside tolerance, so they should pass. Should, not verified; CI settles it, and if it is red that is this PR's to fix. - No mutation transcript. Done, both halves.
Portfolio::Xirr: 7 mutations, 7 caught.Portfolio::Performance: 5 mutations, 5 caught — solving in years, closing the flow a day early, dropping the total-loss guard, annualising under a year, and dropping the opening value. Only one test catches the day-early mutation, and it does so on a 1.25e-4 delta a looser tolerance would have waved through. metrics[:mwr]changes meaning from an annual rate to a period rate.git grepfinds no view or controller reading it — #134's hub does not consume the engine and 2.4 is not built — so the only readers are the engine's own tests. That is what makes now the cheap moment.- Branch name. This session was told to develop on
claude/review-open-prs-5cbhde, which is #164's head; committing there would have merged unrelated work into an open PR, so this went to its own branch. Say the word if you want it renamed. - Next unblocked issue: #154 is in flight as #164; #151 and #142 remain
needs:design.