QuantLib icon indicating copy to clipboard operation
QuantLib copied to clipboard

ZeroInflationTermStructure::zeroRate with forceLinearInterpolation uses the un-lagged query date in its interpolation offset

Open leafyoung opened this issue 2 weeks ago • 0 comments

Summary

On the lagged overloads of ZeroInflationTermStructure::zeroRate and yoyRate (the ones taking instObsLag, deprecated in 58036268f but still present and callable), the forceLinearInterpolation = true branch builds the inflation period dd from the lagged date but computes the interpolation offset dt from the un-lagged query date. With any nonzero observation lag the offset is not the within-period fraction the branch interpolates with, and the returned rate extrapolates past the period-end value instead of interpolating inside it.

Mechanism

ql/termstructures/inflationtermstructure.cpp, current master:

if (forceLinearInterpolation) {
    std::pair<Date,Date> dd = inflationPeriod(d-useLag, frequency());
    dd.second = dd.second + Period(1,Days);
    Real dp = dd.second - dd.first;
    Real dt = d - dd.first;                 // <-- the un-lagged date
    ...
    Rate z1 = zeroRateImpl(t1);
    Rate z2 = zeroRateImpl(t2);
    zeroRate = z1 + (z2-z1) * (dt/dp);
}

dd is the period containing d - useLag, so the within-period fraction is (d - useLag) - dd.first. The sibling yoyRate computes exactly that for the textually identical construction (dt = (d-useLag) - dd.first, same file, yoyRate's forceLinearInterpolation branch) — the two branches disagree whenever the observation lag is nonzero, which is the normal case for a real inflation curve.

Numbers

Query date 2020-01-15, observation lag 91 days, monthly frequency:

dt dp dt/dp
as implemented (dt = d - dd.first) 106 32 3.31
lagged-date offset (dt = (d-useLag) - dd.first) 15 32 0.47

The implemented branch therefore evaluates z1 + (z2 - z1) * 3.31 — extrapolating roughly 2.3× past the period-end value z2 rather than interpolating inside [z1, z2]. A daily sweep over two years with a 91-day lag leaves the fraction outside [0, 1) on 100% of query dates (the lag crosses a period boundary for essentially every date); the divergence grows with the lag-to-period-length ratio.

Suggested fix

Use the lagged date in the offset, matching the sibling branch:

Real dt = (d - useLag) - dd.first;

Since the lagged overloads are already deprecated ("use the overload without a lag instead"), removing them also resolves this; whichever route is taken, the two branches of the same file should agree on the interpolation offset while they coexist.

Found during a blinded code audit of the module; reproducible from the source alone.

Prepared with AI assistance (ZCode/GLM-5.3-flash).

leafyoung avatar Nov 16 '26 15:11 leafyoung