Skip to content

Commit

Permalink
Detect null time-to-reference
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio committed Sep 28, 2023
1 parent a2e4bc5 commit 6dc78b2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
5 changes: 3 additions & 2 deletions ql/termstructures/yieldtermstructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,16 @@ namespace QuantLib {
Compounding comp,
Frequency freq,
bool extrapolate) const {
if (d==referenceDate()) {
Time t = timeFromReference(d);
if (t == 0) {
Real compound = 1.0/discount(dt, extrapolate);
// t has been calculated with a possibly different daycounter
// but the difference should not matter for very small times
return InterestRate::impliedRate(compound,
dayCounter, comp, freq,
dt);
}
Real compound = 1.0/discount(d, extrapolate);
Real compound = 1.0/discount(t, extrapolate);
return InterestRate::impliedRate(compound,
dayCounter, comp, freq,
referenceDate(), d);
Expand Down
30 changes: 24 additions & 6 deletions test-suite/termstructures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,26 @@ void TermStructureTest::testCompositeZeroYieldStructures() {
}
}


void TermStructureTest::testNullTimeToReference() {
BOOST_TEST_MESSAGE("Testing zero-rate calculation for null time-to-reference...");

Rate rate = 0.02;
auto dayCount = Thirty360(Thirty360::BondBasis);
auto curve = FlatForward(Date(30, August, 2023), rate, dayCount);

// the time between August 30th and 31st is null for the 30/360 day count convention
Rate expected = rate;
Rate calculated = curve.zeroRate(Date(31, August, 2023), dayCount, Continuous);
Real tolerance = 1.0e-10;

if (std::fabs(calculated - expected) > tolerance)
BOOST_ERROR("unable to reproduce zero yield rate from curve\n"
<< std::fixed << std::setprecision(10)
<< " calculated: " << calculated << "\n"
<< " expected: " << expected);
}

test_suite* TermStructureTest::suite() {
auto* suite = BOOST_TEST_SUITE("Term structure tests");
suite->add(QUANTLIB_TEST_CASE(&TermStructureTest::testReferenceChange));
Expand All @@ -425,12 +445,10 @@ test_suite* TermStructureTest::suite() {
suite->add(QUANTLIB_TEST_CASE(&TermStructureTest::testFSpreadedObs));
suite->add(QUANTLIB_TEST_CASE(&TermStructureTest::testZSpreaded));
suite->add(QUANTLIB_TEST_CASE(&TermStructureTest::testZSpreadedObs));
suite->add(QUANTLIB_TEST_CASE(
&TermStructureTest::testCreateWithNullUnderlying));
suite->add(QUANTLIB_TEST_CASE(
&TermStructureTest::testLinkToNullUnderlying));
suite->add(QUANTLIB_TEST_CASE(
&TermStructureTest::testCompositeZeroYieldStructures));
suite->add(QUANTLIB_TEST_CASE(&TermStructureTest::testCreateWithNullUnderlying));
suite->add(QUANTLIB_TEST_CASE(&TermStructureTest::testLinkToNullUnderlying));
suite->add(QUANTLIB_TEST_CASE(&TermStructureTest::testCompositeZeroYieldStructures));
suite->add(QUANTLIB_TEST_CASE(&TermStructureTest::testNullTimeToReference));
return suite;
}

1 change: 1 addition & 0 deletions test-suite/termstructures.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class TermStructureTest {
static void testCreateWithNullUnderlying();
static void testLinkToNullUnderlying();
static void testCompositeZeroYieldStructures();
static void testNullTimeToReference();
static boost::unit_test_framework::test_suite* suite();
};

Expand Down

0 comments on commit 6dc78b2

Please sign in to comment.