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

Make ZeroInflationCashFlow properly lazy #1947

Merged
merged 1 commit into from
Apr 12, 2024
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
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()
Loading