Skip to content

Commit

Permalink
More uses of CPI::laggedFixing and laggedYoYRate
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio committed Sep 19, 2024
1 parent 9686bae commit 6061700
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 53 deletions.
29 changes: 5 additions & 24 deletions ql/cashflows/cpicoupon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,33 +166,14 @@ namespace QuantLib {
}

Real CPICashFlow::baseFixing() const {
return baseFixing_;
if (baseFixing_ != Null<Rate>())
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<Rate>()) {
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,
Expand Down
2 changes: 0 additions & 2 deletions ql/cashflows/cpicoupon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,6 @@ namespace QuantLib {

Real indexFixing() const override;

Real amount() const override;

protected:
Real baseFixing_;
Date observationDate_;
Expand Down
5 changes: 3 additions & 2 deletions ql/cashflows/indexedcashflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
5 changes: 4 additions & 1 deletion ql/cashflows/yoyinflationcoupon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@ namespace QuantLib {
InflationCoupon::accept(v);
}


bool YoYInflationCoupon::checkPricerImpl(
const ext::shared_ptr<InflationCouponPricer>&pricer) const {
return static_cast<bool>(
ext::dynamic_pointer_cast<YoYInflationCouponPricer>(pricer));
}

Rate YoYInflationCoupon::indexFixing() const {
return CPI::laggedYoYRate(yoyIndex(), accrualEndDate(), observationLag(), CPI::AsIndex);
}


yoyInflationLeg::yoyInflationLeg(Schedule schedule,
Calendar paymentCalendar,
Expand Down
4 changes: 3 additions & 1 deletion ql/cashflows/yoyinflationcoupon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<YoYInflationIndex>& yoyIndex() const;

//@}

//! \name Visitability
//@{
void accept(AcyclicVisitor&) override;
Expand Down
22 changes: 6 additions & 16 deletions ql/cashflows/zeroinflationcashflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 6 additions & 4 deletions ql/cashflows/zeroinflationcashflow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,24 @@ 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;
//@}

private:
ext::shared_ptr<ZeroInflationIndex> zeroInflationIndex_;
CPI::InterpolationType observationInterpolation_;
CPI::InterpolationType interpolation_;
Date startDate_, endDate_;
Period observationLag_;
};
Expand Down
2 changes: 1 addition & 1 deletion test-suite/inflationcpicapfloor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions test-suite/inflationcpiswap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<IborIndex> dummyFloatIndex;

Expand Down

0 comments on commit 6061700

Please sign in to comment.