Repository navigation
Add natural cubic spline to fitting methods for bond curves - #2378
Conversation
|
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. |
|
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.
|
@lballabio, can you take a look now? |
| Array M = computeSecondDerivatives(y); | ||
|
|
||
| return evaluateWithPrecomputedM(y, M, t); |
There was a problem hiding this comment.
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}; |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
Should we ask the original poster if he has any ideas?
There was a problem hiding this comment.
Just a thought: a possibility might be to add to the spline an explicit point at t=0 with discount=1.
There was a problem hiding this comment.
I'll run and see how that works out.
There was a problem hiding this comment.
Adding constraintAtZero and t=0, does make the result consistent with other fitting models, even for short tenors.
lballabio
left a comment
There was a problem hiding this comment.
Good! I added a few more comments now that it works better.
| if (constrainAtZero) { | ||
| knotVectorNatural.push_back(0.0); | ||
| } |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
| 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; | ||
| } |
There was a problem hiding this comment.
This is no longer used, it can be removed.
| Size size() const override; | ||
| DiscountFactor discountFunction(const Array& x, Time t) const override; | ||
|
|
||
| private: |
There was a problem hiding this comment.
The methods below have been removed from the cpp, they should be removed from here as well.
|
Can you please take a look now @lballabio |
|
Looks good—I removed a few more unneeded lines but otherwise it should be ready to merge. Thanks! |
|
Congratulations on your first merged pull request! |
Uh oh!
There was an error while loading. Please reload this page.