Skip to content

fix(functions): count PRICEMAT/YIELDMAT US 30/360 maturity days from issue (#4093) - #4154

Open
jayzed80 wants to merge 1 commit into
storytold:mainfrom
jayzed80:fix/pricemat-30360-4093
Open

jayzed80 wants to merge 1 commit into
storytold:mainfrom
jayzed80:fix/pricemat-30360-4093

Conversation

@jayzed80

@jayzed80 jayzed80 commented Nov 14, 2026 •

Copy link
Copy Markdown

Fixes #4093

Root cause

mat_terms in crates/functions/src/financial/securities.rs:168 (the shared helper behind PRICEMAT and YIELDMAT) computed the settlement-to-maturity fraction (DSM) directly as year_frac(settlement, maturity, basis). US 30/360 (basis 0) is not additive: once the start day is the 30th, a 31st end day becomes the 30th. So Jan 30 → Jan 31 counts 0 days, even though issue → maturity (Jan 29 → Jan 31) is 2 days and issue → settlement is 1. Excel's results match DSM = DIM − A: 1 day. With DSM = 0, PRICEMAT's discount term disappears (price 100), and YIELDMAT returns #NUM! because of its dsm == 0 guard.

Fix

For basis 0 only, mat_terms now computes DSM as (days360(issue, maturity) − days360(issue, settlement)) / 360. It works in whole days, so the result has no extra floating-point error. Other bases are unchanged. Actual-day bases and European 30/360 (basis 4) are already additive, so DIM − A gives the same result there anyway. Basis 1 is tracked separately in #4054.

Tests

New test financial::tests::maturity_securities_count_us_30_360_from_issue in crates/functions/src/financial/tests.rs. It checks the three Excel values from the issue:

  • PRICEMAT 99.00990099009901 (zero coupon)
  • PRICEMAT 100.00277592613166
  • YIELDMAT 0.0499930565199356

It also checks the December repro and the two controls that already matched (basis 2, and a February 1 maturity).

Without the fix (run on the branch before the change to securities.rs was applied):

---- financial::tests::maturity_securities_count_us_30_360_from_issue stdout ----
thread 'financial::tests::maturity_securities_count_us_30_360_from_issue' panicked at crates/functions/src/financial/tests.rs:198:5:
got 100, expected 99.00990099009901
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 181 filtered out

With the fix:

test financial::tests::maturity_securities_count_us_30_360_from_issue ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 181 filtered out

The existing PRICEMAT/YIELDMAT basis-0 checks (Microsoft documentation examples) still pass.

Checks run

  • cargo fmt -p gridcraft-functions: only the changed lines were affected
  • cargo clippy -p gridcraft-functions --all-targets -- -D warnings: clean
  • cargo test -p gridcraft-functions: 182 passed, 0 failed

Not run

  • The full cargo xtask ci (wasm, layers, assets, other crates) was not run locally; I'm relying on CI for those.
  • I could not check against Excel. The issue mentions a mismatch when settlement is the last day of February and maturity is March 1, but gives no exact values. This change alters those cases too (DSM becomes DIM − A), and I have not confirmed them against Excel.

Summary by CodeRabbit

  • Bug Fixes
    • Corrected maturity-period calculations for securities using the US 30/360 basis, including date intervals spanning month ends.
  • Tests
    • Added regression coverage for maturity price and yield calculations across month-end dates, with checks for other basis and maturity-date scenarios.

…issue (storytold#4093)

Under basis 0, settlement-to-maturity is now issue-to-maturity less
issue-to-settlement, so a 30th-to-31st span counts one day as in Excel.
@coderabbitai

coderabbitai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

For basis 0, mat_terms now calculates the settlement-to-maturity fraction from issue-based day counts. Regression tests cover PRICEMAT and YIELDMAT with month-end dates and control cases.

Changes: Maturity interval calculation

Layer / File(s) Summary
Basis-0 calculation and regression coverage
crates/functions/src/financial/securities.rs, crates/functions/src/financial/tests.rs
For basis 0, mat_terms derives the settlement-to-maturity fraction from the difference between issue-to-maturity and issue-to-settlement day counts. Tests cover January and December 30th-to-31st intervals, plus basis 2 and a February 1 maturity. Other bases retain the direct year-fraction calculation.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: aiongg

Merge Risk: Minimal · up to 686f3

This change corrects PRICEMAT and YIELDMAT results for basis-0 month-end dates such as the 30th-to-31st intervals, and the new regression test covers those cases. The February-end to March 1 results have not been checked against Excel. That is a small residual uncertainty and does not block merging.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the actual change: PRICEMAT/YIELDMAT maturity day counting for US 30/360 is changed to count from issue. The raw summary and objectives both describe a basis-0 change to the shared mat…
Linked Issues check Passed Issue #4093 requests correct basis-0 PRICEMAT and YIELDMAT results for the January 30/31 case, the December repro, and the Excel values in the issue. The diff at `crates/functions/src/financial/securi…
Out of Scope Changes check Passed The PR changes one expression in mat_terms, limited to basis 0 through the if b == 0 branch. Other bases keep the original year_frac(sys, s, m, b) path. The mat_terms helper is shared by PRICE…
Full details: Title check

Explanation

The title names the actual change: PRICEMAT/YIELDMAT maturity day counting for US 30/360 is changed to count from issue. The raw summary and objectives both describe a basis-0 change to the shared maturity helper that does exactly this. The title is specific and concise. The '(#4093)' suffix references the linked issue and does not add noise to the meaning.

Full details: Linked Issues check

Explanation

Issue #4093 requests correct basis-0 PRICEMAT and YIELDMAT results for the January 30/31 case, the December repro, and the Excel values in the issue. The diff at crates/functions/src/financial/securities.rs (mat_terms) changes DSM for basis 0 to (day_count(issue, maturity) - day_count(issue, settlement)) / 360. For the January case this gives DSM = 1 day, which matches Excel. This restores the non-zero discount term in PRICEMAT and removes the dsm == 0 path that caused YIELDMAT #NUM!. The new test maturity_securities_count_us_30_360_from_issue in crates/functions/src/financial/tests.rs asserts the three Excel values from the issue, the December case, and the basis-2 and February 1 controls. Basis 1 is excluded, as the issue states (#4054). The February-end-to-March-1 discrepancy in #4093 has no exact expected values. The change alters that path, but the author states it is not verified against Excel. This residual uncertainty does not block the stated acceptance examples. I did not run the test suite; the verdict relies on the diff and the test assertions.

Full details: Out of Scope Changes check

Explanation

The PR changes one expression in mat_terms, limited to basis 0 through the if b == 0 branch. Other bases keep the original year_frac(sys, s, m, b) path. The mat_terms helper is shared by PRICEMAT and YIELDMAT, which the issue covers. The added test covers the same functions and the issue's controls. The code comment and the test doc comment explain the connection to #4093. No unrelated files or behavior changes appear in the reviewed diff.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between c0ee120 and 686f321.

Files selected for processing: 2

  • crates/functions/src/financial/securities.rs
  • crates/functions/src/financial/tests.rs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

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.

PRICEMAT and YIELDMAT miscount maturity intervals under US 30/360

1 participant