Skip to content

Commit

Permalink
Make ZeroInflationCashFlow properly lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio committed Apr 11, 2024
1 parent daec722 commit d6beb70
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
3 changes: 2 additions & 1 deletion ql/cashflows/indexedcashflow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ namespace QuantLib {
//@{
void performCalculations() const override;
//@}
protected:
mutable Real amount_;
private:
Real notional_;
ext::shared_ptr<Index> index_;
Date baseDate_, fixingDate_, paymentDate_;
bool growthOnly_;
mutable Real amount_;
};


Expand Down
6 changes: 3 additions & 3 deletions ql/cashflows/zeroinflationcashflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace QuantLib {
zeroInflationIndex_(index), observationInterpolation_(observationInterpolation),
startDate_(startDate), endDate_(endDate), observationLag_(observationLag) {}

Real ZeroInflationCashFlow::amount() const {
void ZeroInflationCashFlow::performCalculations() const {

Real I0, I1;

Expand All @@ -51,9 +51,9 @@ namespace QuantLib {
}

if (growthOnly())
return notional() * (I1 / I0 - 1.0);
amount_ = notional() * (I1 / I0 - 1.0);
else
return notional() * (I1 / I0);
amount_ = notional() * (I1 / I0);
}

void ZeroInflationCashFlow::accept(AcyclicVisitor& v) {
Expand Down
2 changes: 1 addition & 1 deletion ql/cashflows/zeroinflationcashflow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace QuantLib {

//! \name CashFlow interface
//@{
Real amount() const override;
void performCalculations() const override;
//@}
//! \name Visitability
//@{
Expand Down
37 changes: 37 additions & 0 deletions test-suite/inflation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <ql/cashflows/yoyinflationcoupon.hpp>
#include <ql/cashflows/inflationcouponpricer.hpp>
#include <ql/cashflows/fixedratecoupon.hpp>
#include <ql/cashflows/zeroinflationcashflow.hpp>
#include <ql/instruments/yearonyearinflationswap.hpp>
#include <functional>

Expand Down Expand Up @@ -1602,6 +1603,42 @@ BOOST_AUTO_TEST_CASE(testCpiAsIndexInterpolation) {
"\n calculated: " << calculated);
}

BOOST_AUTO_TEST_CASE(testNotifications) {
BOOST_TEST_MESSAGE("Testing notifications from zero-inflation cash flow...");

Date today = Settings::instance().evaluationDate();
Real nominal = 10000.0;

std::vector<Date> dates = { today - 3*Months, today + 5*Years };
std::vector<Rate> rates = { 0.02, 0.02 };

RelinkableHandle<ZeroInflationTermStructure> inflation_handle;
inflation_handle.linkTo(
ext::make_shared<ZeroInflationCurve>(today, dates, rates, Monthly, Actual360()));

auto index = ext::make_shared<UKRPI>(inflation_handle);
index->addFixing(inflationPeriod(today - 3 * Months, index->frequency()).first, 100.0);
auto cashflow =
ext::make_shared<ZeroInflationCashFlow>(nominal,
index,
CPI::Flat,
today,
today + 1 * Years,
3 * Months,
today + 1 * Years);
cashflow->amount();

Flag flag;
flag.registerWith(cashflow);
flag.lower();

inflation_handle.linkTo(
ext::make_shared<ZeroInflationCurve>(today, dates, rates, Monthly, Actual360()));

if (!flag.isUp())
BOOST_FAIL("cash flow did not notify observer of curve change");
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit d6beb70

Please sign in to comment.