Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect null time-to-reference when calculating zero rates #1798

Merged
merged 1 commit into from
Sep 29, 2023
Merged
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/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