From 6061700cdfe6880ce36e72363142a6e88a48c277 Mon Sep 17 00:00:00 2001 From: Luigi Ballabio Date: Thu, 19 Sep 2024 15:52:56 +0200 Subject: [PATCH] More uses of CPI::laggedFixing and laggedYoYRate --- ql/cashflows/cpicoupon.cpp | 29 +++++--------------------- ql/cashflows/cpicoupon.hpp | 2 -- ql/cashflows/indexedcashflow.cpp | 5 +++-- ql/cashflows/yoyinflationcoupon.cpp | 5 ++++- ql/cashflows/yoyinflationcoupon.hpp | 4 +++- ql/cashflows/zeroinflationcashflow.cpp | 22 ++++++------------- ql/cashflows/zeroinflationcashflow.hpp | 10 +++++---- test-suite/inflationcpicapfloor.cpp | 2 +- test-suite/inflationcpiswap.cpp | 3 +-- 9 files changed, 29 insertions(+), 53 deletions(-) diff --git a/ql/cashflows/cpicoupon.cpp b/ql/cashflows/cpicoupon.cpp index 16e66f8e044..4a33ef1fc0f 100644 --- a/ql/cashflows/cpicoupon.cpp +++ b/ql/cashflows/cpicoupon.cpp @@ -166,33 +166,14 @@ namespace QuantLib { } Real CPICashFlow::baseFixing() const { - return baseFixing_; + if (baseFixing_ != Null()) + return baseFixing_; + else + return CPI::laggedFixing(cpiIndex(), baseDate(), 0 * Months, interpolation_); } Real CPICashFlow::indexFixing() const { - if (observationDate_ != Date()) { - return CPI::laggedFixing(cpiIndex(), observationDate_, observationLag_, interpolation_); - } else { - // we get to this branch when the deprecated constructor was used; it will be phased out - return CPI::laggedFixing(cpiIndex(), fixingDate() + observationLag_, observationLag_, - interpolation_); - } - } - - Real CPICashFlow::amount() const { - Rate I0 = baseFixing(); - - // If BaseFixing is null, use the observed index fixing - if (I0 == Null()) { - I0 = IndexedCashFlow::baseFixing(); - } - - Rate I1 = indexFixing(); - - if (growthOnly()) - return notional() * (I1 / I0 - 1.0); - else - return notional() * (I1 / I0); + return CPI::laggedFixing(cpiIndex(), observationDate_, observationLag_, interpolation_); } CPILeg::CPILeg(Schedule schedule, diff --git a/ql/cashflows/cpicoupon.hpp b/ql/cashflows/cpicoupon.hpp index 8ffe3719b99..0267ece0669 100644 --- a/ql/cashflows/cpicoupon.hpp +++ b/ql/cashflows/cpicoupon.hpp @@ -191,8 +191,6 @@ namespace QuantLib { Real indexFixing() const override; - Real amount() const override; - protected: Real baseFixing_; Date observationDate_; diff --git a/ql/cashflows/indexedcashflow.cpp b/ql/cashflows/indexedcashflow.cpp index 867727b3cc6..2490a65fdb3 100644 --- a/ql/cashflows/indexedcashflow.cpp +++ b/ql/cashflows/indexedcashflow.cpp @@ -45,8 +45,9 @@ namespace QuantLib { Real I1 = indexFixing(); if (growthOnly_) - amount_ = notional_ * (I1 / I0 - 1.0); + amount_ = notional() * (I1 / I0 - 1.0); else - amount_ = notional_ * (I1 / I0); + amount_ = notional() * (I1 / I0); } + } diff --git a/ql/cashflows/yoyinflationcoupon.cpp b/ql/cashflows/yoyinflationcoupon.cpp index 9ce0e8cec34..db8e5a0b4ed 100644 --- a/ql/cashflows/yoyinflationcoupon.cpp +++ b/ql/cashflows/yoyinflationcoupon.cpp @@ -53,13 +53,16 @@ namespace QuantLib { InflationCoupon::accept(v); } - bool YoYInflationCoupon::checkPricerImpl( const ext::shared_ptr&pricer) const { return static_cast( ext::dynamic_pointer_cast(pricer)); } + Rate YoYInflationCoupon::indexFixing() const { + return CPI::laggedYoYRate(yoyIndex(), accrualEndDate(), observationLag(), CPI::AsIndex); + } + yoyInflationLeg::yoyInflationLeg(Schedule schedule, Calendar paymentCalendar, diff --git a/ql/cashflows/yoyinflationcoupon.hpp b/ql/cashflows/yoyinflationcoupon.hpp index c924de770e3..8cccf5cdd10 100644 --- a/ql/cashflows/yoyinflationcoupon.hpp +++ b/ql/cashflows/yoyinflationcoupon.hpp @@ -55,11 +55,13 @@ namespace QuantLib { //! spread paid over the fixing of the underlying index Spread spread() const { return spread_; } + Rate indexFixing() const override; + Rate adjustedFixing() const; const ext::shared_ptr& yoyIndex() const; - //@} + //! \name Visitability //@{ void accept(AcyclicVisitor&) override; diff --git a/ql/cashflows/zeroinflationcashflow.cpp b/ql/cashflows/zeroinflationcashflow.cpp index 90eb676c91e..26c79fcd3c3 100644 --- a/ql/cashflows/zeroinflationcashflow.cpp +++ b/ql/cashflows/zeroinflationcashflow.cpp @@ -35,25 +35,15 @@ namespace QuantLib { : IndexedCashFlow(notional, index, startDate - observationLag, endDate - observationLag, paymentDate, growthOnly), - zeroInflationIndex_(index), observationInterpolation_(observationInterpolation), + zeroInflationIndex_(index), interpolation_(observationInterpolation), startDate_(startDate), endDate_(endDate), observationLag_(observationLag) {} - void ZeroInflationCashFlow::performCalculations() const { - - Real I0, I1; - - if (observationInterpolation_ == CPI::AsIndex) { - I0 = zeroInflationIndex_->fixing(baseDate()); - I1 = zeroInflationIndex_->fixing(fixingDate()); - } else { - I0 = CPI::laggedFixing(zeroInflationIndex_, startDate_, observationLag_, observationInterpolation_); - I1 = CPI::laggedFixing(zeroInflationIndex_, endDate_, observationLag_, observationInterpolation_); - } + Real ZeroInflationCashFlow::baseFixing() const { + return CPI::laggedFixing(zeroInflationIndex_, startDate_, observationLag_, interpolation_); + } - if (growthOnly()) - amount_ = notional() * (I1 / I0 - 1.0); - else - amount_ = notional() * (I1 / I0); + Real ZeroInflationCashFlow::indexFixing() const { + return CPI::laggedFixing(zeroInflationIndex_, endDate_, observationLag_, interpolation_); } void ZeroInflationCashFlow::accept(AcyclicVisitor& v) { diff --git a/ql/cashflows/zeroinflationcashflow.hpp b/ql/cashflows/zeroinflationcashflow.hpp index 316e34c6fc8..0b00e4f8eed 100644 --- a/ql/cashflows/zeroinflationcashflow.hpp +++ b/ql/cashflows/zeroinflationcashflow.hpp @@ -55,14 +55,16 @@ namespace QuantLib { return zeroInflationIndex_; } CPI::InterpolationType observationInterpolation() const { - return observationInterpolation_; + return interpolation_; } //@} - //! \name CashFlow interface + //! \name ZeroInflationCashFlow interface //@{ - void performCalculations() const override; + Real baseFixing() const override; + Real indexFixing() const override; //@} + //! \name Visitability //@{ void accept(AcyclicVisitor&) override; @@ -70,7 +72,7 @@ namespace QuantLib { private: ext::shared_ptr zeroInflationIndex_; - CPI::InterpolationType observationInterpolation_; + CPI::InterpolationType interpolation_; Date startDate_, endDate_; Period observationLag_; }; diff --git a/test-suite/inflationcpicapfloor.cpp b/test-suite/inflationcpicapfloor.cpp index b05676a1de3..8b61e8c26c3 100644 --- a/test-suite/inflationcpicapfloor.cpp +++ b/test-suite/inflationcpicapfloor.cpp @@ -401,8 +401,8 @@ BOOST_AUTO_TEST_CASE(cpicapfloorpricer) { Calendar fixCalendar = UnitedKingdom(), payCalendar = UnitedKingdom(); BusinessDayConvention fixConvention(Unadjusted), payConvention(ModifiedFollowing); Rate strike(0.03); - Real baseCPI = common.ii->fixing(fixCalendar.adjust(startDate-common.observationLag,fixConvention)); CPI::InterpolationType observationInterpolation = CPI::AsIndex; + Real baseCPI = CPI::laggedFixing(common.ii, startDate, common.observationLag, observationInterpolation); CPICapFloor aCap(Option::Call, nominal, startDate, // start date of contract (only) diff --git a/test-suite/inflationcpiswap.cpp b/test-suite/inflationcpiswap.cpp index d0762525ca2..3a36f63f20e 100644 --- a/test-suite/inflationcpiswap.cpp +++ b/test-suite/inflationcpiswap.cpp @@ -387,8 +387,7 @@ BOOST_AUTO_TEST_CASE(zciisconsistency) { bool subtractInflationNominal = true; Real dummySpread=0.0, dummyFixedRate=0.0; Natural fixingDays = 0; - Date baseDate = startDate - observationLag; - Real baseCPI = common.ii->fixing(baseDate); + Real baseCPI = CPI::laggedFixing(common.ii, startDate, observationLag, CPI::AsIndex); ext::shared_ptr dummyFloatIndex;