Repository navigation
Compute interval factors from cumulative log-discounts - #324
Merged
Merged
Conversation
discount(curve, from, to), accumulation and forward divided two discount factors, which is 0/0 = NaN once both underflow: an interval 20000 -> 20001 on a Nelson-Siegel curve, or ForwardStarting far into the tail. An internal cumulative log-discount L(t) = -log D(t) now carries interval quantities. Built-in curves compute it directly: z(t)·t for curves defined by a zero rate (exactly the exponent their discount already used), the spline tail limit at Inf, L1 ± L2 for CompositeYield, k·L for ScaledYield, and closed forms for Vasicek and RatePath. Their discount factor is exp(-L), so an interval from 0 reproduces discount(curve, t) bit for bit and every present_value of a contract is unchanged. Which formula applies depends only on the curve's type. A curve that defines only discount keeps the exact ratio D(to)/D(from), since its discount factor may be nonpositive or differ from 1 at 0. Smith-Wilson, whose fit to arbitrary prices can make the discount factor negative, gets an exact interval exp(-ufr·Δt)·(1+s(to))/(1+s(from)). ForwardStarting and HullWhite delegate to their curve's interval. Empty intervals are exactly 1, also at Inf; forward is the difference of L. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Nw2FiivhYrC16532c1mJQN
NEWS, the migration guide and the models page: built-in curves compute interval factors from their cumulative log-discount, intervals from 0 are unchanged, far-tail intervals are finite, and a curve that defines only discount keeps the ratio D(to)/D(from). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Nw2FiivhYrC16532c1mJQN
s(0) = 0, so an interval from an exact 0 (every present value of a contract) needs only s(to): the result is unchanged bit for bit and a Smith-Wilson bond present value no longer pays for a second sum. A time dual at 0 still takes the full ratio, so its derivative is kept. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Nw2FiivhYrC16532c1mJQN
… relative factor A composite took its components' cumulative log-discounts at t = Inf and then combined them, so flat forwards of 4% and -2% gave Inf + -Inf = NaN at infinity although the sum tends to +Inf (FinanceModels 6, which summed zero rates, gave 0). Each curve now describes the tail of L, L(t) = a2·t² + a1·t + a0 + o(1): the knot-curve tails (Spline, MonotoneConvex) and Constant in closed form, CompositeYield combining componentwise with its + or -, ScaledYield scaling each coefficient, ForwardStarting shifting the base tail, and HullWhite and the primal view delegating. Any other curve contributes its zero rate at infinity as the slope with no known intercept, which is exactly the previous sum of zero rates. The existing classification (the first nonzero of a2 then a1 decides 0 or Inf, else exp(-a0); a deciding coefficient that is zero but carries partials has no derivative) runs on the combined coefficients, so cancelling long-run forwards leave their finite intercept instead of NaN, and a single knot curve's limits are unchanged bit for bit. The wrappers' limits are out of line so the finite-time path stays small. ForwardStarting's log-discount subtracted the base curve's two absolute log-discounts, which throw where a signed Smith-Wilson fit has negative discount factors at both ends although the rebased factor is positive. It is now the base curve's log-discount over the interval: the difference of L for a log-native curve, otherwise the log of the curve's own interval factor, and for Smith-Wilson ufr·(b - a) - log((1 + s(b))/(1 + s(a))), which is also finite in the far tail. A nonpositive rebased factor still raises the natural DomainError. Tests: the reviewer's opposite flat forwards (a + b, b + a, a - a/2 at Inf and (1, Inf)); a cancelled flat-forward slope leaving exp(0.03125); equal :linear slopes; ForwardStarting(c, 3) - c; 0 * a; the no-derivative throw on a combined zero forward; -Inf still rejected; Nelson-Siegel and TenorShift composites equal to the zero-rate formula; and zero, forward, scaling and + of a Smith-Wilson rebased where both absolute factors are negative, against the relative factor. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Nw2FiivhYrC16532c1mJQN
…finity The fallback tail for a curve without a closed form is (0, z(∞), NaN). When z(∞) = ±Inf, L grows faster than t at an order the curve doesn't state, so its a2 = 0 is not a known coefficient. The classifier read a2 first, so a known quadratic of the other sign decided the limit: a TenorShift with zero rate t/32 minus a `:linear` curve with t/64 (L = t²/64 → +Inf) gave discount Inf instead of 0, and the reverse gave 0 instead of Inf. The forward slot is now read first. NaN there is unknown. ±Inf is unknown growth of that sign: it decides the limit unless a2 has the other sign, and then the limit is NaN. Combining, scaling and rebasing already carry ±Inf correctly (Inf − Inf is NaN), so the fallback's numbers don't change, and a fallback curve plus a flat or same-sign tail keeps FinanceModels 6's limit. Also documents the internal curve capabilities (`__log_discount`, `__log_native`, `__log_interval`, `__log_tail`): what each means, its default, and which types define it. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Nw2FiivhYrC16532c1mJQN
alecloudenback
force-pushed
the
refactor/composite-yield-factor-ops
branch
from
September 27, 2026 15:19
51de88a to
32d1d0a
Compare
alecloudenback
force-pushed
the
feat/log-discount-intervals
branch
from
September 27, 2026 15:19
a96e0e3 to
5698867
Compare
5698867 returned as soon as a tail with unknown growth had a primal zero quadratic coefficient, so a coefficient that was zero only at the primal values skipped the check that a deciding coefficient carrying partials has no derivative. With L = t^1.5 + k·t²/64 the limit at infinity is 0 at k = 0 but Inf for every k < 0, yet d/dk returned 0.0 at k = 0. The zero quadratic coefficient now goes through the same check as a known tail's and throws; the check is one shared helper. NEWS and the tests call the opposing-growth limit unsupported. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Nw2FiivhYrC16532c1mJQN
This was referenced Sep 27, 2026
Closed
Merged
This was referenced Sep 30, 2026
Merged
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Merge map (updated 2026-10-01)
Part of the coordinated FinanceCore 2.8, FinanceModels 7.0, ActuaryUtilities 6.0 and MortalityTables 3.0 releases. Merge each stack bottom-up with Create a merge commit. Automatic branch deletion is on, so after each merge GitHub retargets the next PR onto the default branch: check that its base says
masterand let its CI finish before merging it.master; #327 lands them)master; #152 lands them. Independent of the other three)This PR: FinanceModels #324, number 8 of 10 open (base #323; next #325).
Updated 2026-09-27. Rebased onto #323, plus two commits from the published-stack review and its follow-up:
Inf, the log-discount grows faster than t at an order the curve doesn't state, so the 0 in a₂ isn't known. The classifier read a₂ first, so a known quadratic of the other sign decided the limit. ATenorShiftwith zero rate t/32 minus a:linearcurve with t/64 has L = t²/64 → +Inf, so its discount at infinity is 0; the old result wasInf, and the reverse gave 0 instead ofInf.Infthere decides the limit unless a₂ has the other sign. Then the limit is unsupported and returnsNaN, rather than a guess.Inffor every k < 0, so d/dk at 0 doesn't exist. 5698867 had returned 0.0 there.Yield.jl. It covers__log_discount,__log_native,__log_intervaland__log_tail: each one's meaning, its default and which types define it.Verification (2026-09-27, at 5698867; the suites again at d8dc11f):
NaN, the conservative answer it recommended, not the opposite limit;The rebase changed the earlier commits' SHAs: a96e0e3 → 8448cb1, f742b4e → 68ed969, 638c38f → 16685a6, 68ec1ed → a3bddf9. The 2026-09-26 verification below refers to the old ones.
Stacked on #323 (
refactor/composite-yield-factor-ops). Review against that branch.Interval factors are computed from cumulative log-discounts.
discount(curve, from, to),accumulation(curve, from, to)andforward(curve, from, to)used to divide two discount factors. That is 0/0 =NaNonce both underflow:discount(Yield.NelsonSiegel(1.0, 0.05, -0.02, 0.01), 20000, 20001)wasNaN. They now take differences of L(t) = −log D(t), the force accumulated from valuation time 0:The earlier review showed why this can't be the literal
rate(zero(c, t))·tfor every curve: Smith–Wilson's generic zero rate is 0/0 at the origin, sodiscount(c, 0, 1)would have becomeNaN. So L is an internal, per-model primitive,Yield.__log_discount(c, t). Whether a curve's intervals use it depends only on its type (Yield.__log_native), never on the values:-log(discount(c, t)), used forzeroandforward. Intervals of a curve that defines onlydiscountstay the exact ratio D(to)/D(from), since its discount factor may be nonpositive or differ from 1 at 0Constant, NelsonSiegel(Svensson), CairnsPritchard(Extended), MonotoneConvex,TenorShift/ProjectedShiftdiscountalready used. At an exact zero time it is 0 (a wrapper over a discount-native curve has a 0/0 zero rate there); under ForwardDiff 1.xiszeroneeds zero partials, so a time derivative at 0 is keptYield.Spline_fn(t)·t, and the tail limit atInfSmithWilsonufr·t − log1p(s(t))forzero/forward. Intervals areexp(-ufr·Δt)·(1+s(to))/(1+s(from)), exact for either sign: a fit to arbitrary prices can make the discount factor negative, as the bullet-bond round-trip test doesCompositeYield(+/-)+/-restriction in the base PR is what makes this exactScaledYieldForwardStartingzero/forward, L is the base curve's log-discount over [τ, τ + t]: L(curve, τ + t) − L(curve, τ) for a log-native base, and otherwise the log of the base curve's own interval factor. So a rebased signed Smith–Wilson fit keepszero,forwardand scaling wherever its relative factor is positive, even where both absolute discount factors are negative. The cached discount-factor field is goneVasicekHullWhite: its curve's L and intervals;RatePath: the integrated short rateLimits at infinity combine tails. A composite can't combine its components' limits: flat forwards of 4% and −2% have L =
Infand-Inf, andInf + -InfisNaN, though the sum tends to +Inf. Each curve therefore describes the long-run behavior of L, L(t) = a₂t² + a₁t + a₀ + o(1), and the limit is taken after combining:ZeroRateCurve/Yield.Spline,MonotoneConvex): (γ, α − γtₙ, 0) under:linear, (0, α, βtₙ) under the flat policies;Constant: (0, r, 0).CompositeYieldcombines componentwise with its+/-;ScaledYieldscales each coefficient;ForwardStartingshifts the base tail;HullWhiteand the internal primal view delegate.NaN, as FinanceModels 6'sInf − Infwas.NaN) givesNaN.Inf) decides the limit unless a₂ has the other sign.Inf, and otherwise the limit is exp(−a₀). A deciding coefficient that is zero but carries partials still throws "no derivative".a + b,b + aanda − a/2give 0.0 again. Cancellations that wereNaNin FinanceModels 6 are now exact:Constantat its tail forward gives exp(−a₀);:lineartails with equal slopes;ForwardStarting(c, τ) − c;0 * cgives 1.What stays the same:
discount(c, t)is exactlyexp(-L), sodiscount(c, 0, t) === discount(c, t)for every type. That covers everypresent_valueof a contract (Projection evaluatesdiscount(model, 0.0, t)). A test checks===for 18 curve types.discount(curve, Inf)for a single knot curve keeps its documented limits (0,Inf, or the finite factor) and its AD rule, bit for bit; that classification now runs on the tail coefficients. Each result keeps its own type and partials.accumulation(c, s, t) == discount(c, t, s).What changes:
ForwardStarting, Vasicek, Hull–White),zeroandforwardmove by at most about 5e-15 relative, because they no longer round-trip through-log(discount)/t.exp(-0.05), and Smith–Wilson's 30000 → 30001 isexp(-ufr). So iszero(sw, 30000).discount(c, t, t)is exactly 1, includingt = Inf.discount(sw, t)for Smith–Wilson is unchanged. Its intervals are exact and finite in the far tail (30000 → 30001 wasNaN), and an interval from exactly 0 evaluates one sum, not two.Out of scope (recorded; this PR doesn't make them worse):
zero(c, 0)is still NaN. Both belong to the t = 0 limits item.Tests (
test/log_discount.jl):===from 0 for every type;+,-and scaling;ForwardStartingas a shift; reversed = inverse; the composition law;forward= the interval's rate;forwardthrows aDomainError;ForwardStartingover it. Rebased where both absolute factors are negative,zero,forward, scaling and+agree with the positive relative factor; rebased across the sign change,zerothrows aDomainError;a + b,b + aanda − a/2, atInfand at(1, Inf);:linearslopes;ForwardStarting(c, 3) − c;0 * a;-Infstill rejected;TenorShiftcomposites equal to FinanceModels 6's zero-rate formula;discount(c, t, t + h)at h = 0 = −(instantaneous forward) for linear, Nelson–Siegel and MonotoneConvex curves.Verification (local, 2026-09-26, at a96e0e3):
full suite at 7.0.0-DEV: 6,506 pass, Aqua 10/10;
Runic 1.11.1 clean; docs build clean;
the plan-review probes: Smith–Wilson
discount(c, 0, 1)is still 0.9704455335485082, and its 30000 → 30001 interval is finite (wasNaN);the implementation-rereview probe (
review-artifacts/2026-09-26-implementation-rereview/fm/probe.jl):discount(a + b, Inf),discount(b + a, Inf),discount(a − a/2, Inf)and their(1, Inf)intervals are 0.0 (wereNaN);zero(f, 1) = Continuous(-0.5222246045208919)= −logdiscount(f, 1),forward(f, 1, 2) = Continuous(-0.2949429547513457),discount(2f, 1) = discount(f, 1)²anddiscount(2f, 0) = 1.0(all threw);ActuaryUtilities' full suite (4,638, Define an irr/YTM for quotes #161 at b45b9f6) plus the 12 Downstream integration checks, against a96e0e3 and FinanceCore Let Rate do the trivial interest manipulations #56 at 09a7b56;
the tails commit's finite-time cost (warmed loop, ns, f742b4e → a96e0e3):
discount(c, 1.3, t): Linear 48 → 49, NelsonSiegel 10.3 → 10.4, SmithWilson 48 → 48, Linear + NS 69 → 68,ForwardStarting47–48 → 47–48;2 * Linear47 → 51, from the infinite-time branch inScaledYield;benchmark (ns, before → after):
present_value(curve, bond)discount(c, 1.3, 7.2)🤖 Generated with Claude Code