Skip to content

Add natural cubic spline to fitting methods for bond curves - #2378

Merged
lballabio merged 7 commits into
lballabio:masterfrom
Krishn1412:natural_cubic_spline_fitter
Dec 5, 2025
Merged

lballabio merged 7 commits into
lballabio:masterfrom
Krishn1412:natural_cubic_spline_fitter

Conversation

@Krishn1412

@Krishn1412 Krishn1412 commented Nov 23, 2025 •

Copy link
Copy Markdown
Contributor
  • Solves Request: Natural cubic spline fitter for FittedBondDiscountCurve #2314.
  • NaturalCubicFitting takes a strictly increasing list of knot times (must be ≥2 and must have positive spacing), and constructs a natural cubic spline where the second derivative at the endpoints is forced to be zero. These knots define the points where the spline’s curvature can change.

@CLAassistant

CLAassistant commented Nov 23, 2025 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@boring-cyborg

boring-cyborg Bot commented Nov 23, 2025

Copy link
Copy Markdown

Thanks for opening this pull request! It might take a while before we look at it, so don't worry if there seems to be no feedback. We'll get to it.

@lballabio

Copy link
Copy Markdown
Owner

We already have an implementation of natural cubic splines in the library (see https://github.com/lballabio/QuantLib/blob/master/ql/math/interpolations/cubicinterpolation.hpp). I'd use that one instead of coding a new one here.

Refactored NaturalCubicFitting to use QuantLib’s CubicInterpolation for
natural spline second-derivative computation and removed custom tridiagonal solver.
@Krishn1412

Copy link
Copy Markdown
Contributor Author

@lballabio, can you take a look now?

Comment on lines +421 to +423
Array M = computeSecondDerivatives(y);

return evaluateWithPrecomputedM(y, M, t);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be

        CubicNaturalSpline spline(knotTimes_.begin(), knotTimes_.end(), y.begin());
        spline.update();
        return spline(std::clamp(t, knotTimes_.front(), knotTimes_.back()));

reusing the existing natural spline implementation. No need to reimplement it in evaluateWithPrecomputedM.


printOutput("(g) exponential splines, fixed kappa", ts7);

Time knotsNatural[] = { 5, 10, 15, 20, 25};

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you wrote, having the first node at 5 years shows a case in which the curve doesn't really work. I would also add a case in which it does work and returns reliable rates for short tenors. If there's no case in which it works reliably, I'm not sure we should add it...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we ask the original poster if he has any ideas?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please do.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought: a possibility might be to add to the spline an explicit point at t=0 with discount=1.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll run and see how that works out.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding constraintAtZero and t=0, does make the result consistent with other fitting models, even for short tenors.

@coveralls

coveralls commented Nov 27, 2025 •

Copy link
Copy Markdown

Coverage Status

coverage: 74.355% (+0.03%) from 74.324%
when pulling c988da9 on Krishn1412:natural_cubic_spline_fitter
into e46ecf9 on lballabio:master.

@lballabio lballabio left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good! I added a few more comments now that it works better.

Comment on lines +276 to +278
if (constrainAtZero) {
knotVectorNatural.push_back(0.0);
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add this to the constructor of NaturalCubicFitting instead, so code using the class (like this example) doesn't have to do it.

class NaturalCubicFitting : public FittedBondDiscountCurve::FittingMethod {
public:
NaturalCubicFitting(const std::vector<Time>& knotTimes,
bool constrainAtZero = true,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to leave the choice? Without the constraint it doesn't work anyway. We can remove the argument from the constructors and treat it as always true.

Comment on lines +322 to +332
Size NaturalCubicFitting::findInterval(Time t) const {
const Size n = knotTimes_.size();
if (t <= knotTimes_.front()) return 0;
if (t >= knotTimes_.back()) return n - 2;
auto it = std::upper_bound(knotTimes_.begin(), knotTimes_.end(), t);
Size idx = std::distance(knotTimes_.begin(), it);
QL_REQUIRE(idx > 0, "NaturalCubicFitting::findInterval(): internal error");
idx = idx - 1;
if (idx >= n - 1) idx = n - 2;
return idx;
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer used, it can be removed.

Size size() const override;
DiscountFactor discountFunction(const Array& x, Time t) const override;

private:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The methods below have been removed from the cpp, they should be removed from here as well.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're missing the (h) here.

@Krishn1412

Copy link
Copy Markdown
Contributor Author

Can you please take a look now @lballabio

@lballabio

Copy link
Copy Markdown
Owner

Looks good—I removed a few more unneeded lines but otherwise it should be ready to merge. Thanks!

@lballabio
lballabio merged commit 9c6bbcf into lballabio:master Dec 5, 2025
@boring-cyborg

boring-cyborg Bot commented Dec 5, 2025

Copy link
Copy Markdown

Congratulations on your first merged pull request!

@lballabio lballabio added this to the Release 1.41 milestone Jan 3, 2026
@lballabio lballabio changed the title Natural Cubic Spline Fitting method Add natural cubic spline to fitting methods for bond curves Jan 5, 2026
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.

4 participants