Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ql/termstructures/yield/nonlinearfittingmethods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,17 @@ namespace QuantLib {
//Use the interal fixedKappa_ member if non-zero, otherwise take kappa from the passed x[] array
Real kappa = (fixedKappa_ != Null<Real>()) ? fixedKappa_: x[N-1];
Real coeff = 0;
Size n = (fixedKappa_ != Null<Real>()) ? N : N - 1;

if (!constrainAtZero_) {
for (Size i = 0; i < N - 1; ++i) {
for (Size i = 0; i < n; ++i) {
d += x[i] * std::exp(-kappa * (i + 1) * t);
}
} else {
// notation:
// d(t) = coeff* exp(-kappa*1*t) + x[0]* exp(-kappa*2*t) +
// x[1]* exp(-kappa*3*t) + ..+ x[7]* exp(-kappa*9*t)
for (Size i = 0; i < N - 1; i++) {
for (Size i = 0; i < n; i++) {
d += x[i] * std::exp(-kappa * (i + 2) * t);
coeff += x[i];
}
Expand Down
50 changes: 50 additions & 0 deletions test-suite/fittedbonddiscountcurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,56 @@ BOOST_AUTO_TEST_CASE(testConstraint) {
BOOST_CHECK_GT(positiveCurve.fitResults().solution()[0], 0.0);
}

BOOST_AUTO_TEST_CASE(testExponentialSplinesFixedKappa) {
BOOST_TEST_MESSAGE("Testing exponential splines fitting with fixed kappa...");

Date today = Settings::instance().evaluationDate();
Date maxDate = today + Period(30, Years);
Actual365Fixed dayCounter;
Real t = 1.0;
Real kappa = 0.1;

// Expected discount factor:
// d(1.0) = 0.2 * exp(-0.1 * 1.0) + 0.3 * exp(-0.2 * 1.0) + 0.5 * exp(-0.3 * 1.0)
Real expected = 0.2 * std::exp(-kappa * 1.0 * t) +
0.3 * std::exp(-kappa * 2.0 * t) +
0.5 * std::exp(-kappa * 3.0 * t);

// 1. Unconstrained: 3 coefficients, fixed kappa
ExponentialSplinesFitting unconstrainedFixed(false, 3, kappa);
Array unconstrainedCoeffs = {0.2, 0.3, 0.5};
FittedBondDiscountCurve curveUnconstrainedFixed(today, unconstrainedFixed, unconstrainedCoeffs, maxDate, dayCounter);
BOOST_CHECK_CLOSE(curveUnconstrainedFixed.discount(t), expected, 1e-10);

// Free kappa with same parameters
ExponentialSplinesFitting unconstrainedFree(false, 3);
Array unconstrainedParamsFree = {0.2, 0.3, 0.5, kappa};
FittedBondDiscountCurve curveUnconstrainedFree(today, unconstrainedFree, unconstrainedParamsFree, maxDate, dayCounter);
BOOST_CHECK_CLOSE(curveUnconstrainedFree.discount(t), expected, 1e-10);

// Verify last coefficient affects the discount factor (not ignored)
Array unconstrainedBumped = {0.2, 0.3, 0.6};
FittedBondDiscountCurve curveUnconstrainedBumped(today, unconstrainedFixed, unconstrainedBumped, maxDate, dayCounter);
BOOST_CHECK(curveUnconstrainedBumped.discount(t) != curveUnconstrainedFixed.discount(t));

// 2. Constrained at zero: first coefficient is 1 - (0.3 + 0.5) = 0.2
ExponentialSplinesFitting constrainedFixed(true, 3, kappa);
Array constrainedCoeffs = {0.3, 0.5};
FittedBondDiscountCurve curveConstrainedFixed(today, constrainedFixed, constrainedCoeffs, maxDate, dayCounter);
BOOST_CHECK_CLOSE(curveConstrainedFixed.discount(t), expected, 1e-10);

// Free kappa with same parameters
ExponentialSplinesFitting constrainedFree(true, 3);
Array constrainedParamsFree = {0.3, 0.5, kappa};
FittedBondDiscountCurve curveConstrainedFree(today, constrainedFree, constrainedParamsFree, maxDate, dayCounter);
BOOST_CHECK_CLOSE(curveConstrainedFree.discount(t), expected, 1e-10);

// Verify last coefficient affects the discount factor (not ignored)
Array constrainedBumped = {0.3, 0.6};
FittedBondDiscountCurve curveConstrainedBumped(today, constrainedFixed, constrainedBumped, maxDate, dayCounter);
BOOST_CHECK(curveConstrainedBumped.discount(t) != curveConstrainedFixed.discount(t));
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()