Skip to content

Commit

Permalink
Add check on guess size for fitted bond curves
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio committed Jun 20, 2024
1 parent 5a22e17 commit c79c8e0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions ql/termstructures/yield/fittedbonddiscountcurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ namespace QuantLib {
// start with the guess solution, if it exists
Array x(size(), 0.0);
if (!curve_->guessSolution_.empty()) {
QL_REQUIRE(curve_->guessSolution_.size() == size(), "wrong size for guess");
x = curve_->guessSolution_;
}

Expand Down
28 changes: 28 additions & 0 deletions test-suite/fittedbonddiscountcurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,34 @@ BOOST_AUTO_TEST_CASE(testRequiredGuess) {
ExpectedErrorMessage("L2 penalty requires a guess"));
}

BOOST_AUTO_TEST_CASE(testGuessSize) {

BOOST_TEST_MESSAGE("Testing that fitted bond curves check the guess size when given...");

Date today = Settings::instance().evaluationDate();
auto bond1 = ext::make_shared<ZeroCouponBond>(3, TARGET(), 100.0, today + Period(1, Years));
auto bond2 = ext::make_shared<ZeroCouponBond>(3, TARGET(), 100.0, today + Period(2, Years));
auto bond3 = ext::make_shared<ZeroCouponBond>(3, TARGET(), 100.0, today + Period(5, Years));
auto bond4 = ext::make_shared<ZeroCouponBond>(3, TARGET(), 100.0, today + Period(10, Years));

std::vector<ext::shared_ptr<BondHelper> > helpers(4);
helpers[0] = ext::make_shared<BondHelper>(makeQuoteHandle(99.0), bond1);
helpers[1] = ext::make_shared<BondHelper>(makeQuoteHandle(98.0), bond2);
helpers[2] = ext::make_shared<BondHelper>(makeQuoteHandle(95.0), bond3);
helpers[3] = ext::make_shared<BondHelper>(makeQuoteHandle(90.0), bond4);

NelsonSiegelFitting fittingMethod;

Real accuracy = 1e-10;
Size maxIterations = 10000;
Array guess = { 0.01, 0.0, 0.0 }; // too few
FittedBondDiscountCurve curve(0, TARGET(), helpers, Actual365Fixed(),
fittingMethod, accuracy, maxIterations, guess);

BOOST_CHECK_EXCEPTION(curve.discount(3.0), Error,
ExpectedErrorMessage("wrong size for guess"));
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit c79c8e0

Please sign in to comment.