Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
cpicoupon.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file cpicoupon.hpp
20 \brief CPI leg builder extending QuantLib's to handle caps and floors
21*/
22
23#ifndef quantext_cpicoupon_hpp
24#define quantext_cpicoupon_hpp
25
26#include <ql/cashflows/cpicoupon.hpp>
27#include <ql/instruments/cpicapfloor.hpp>
28#include <ql/time/schedule.hpp>
29
30namespace QuantExt {
31using namespace QuantLib;
32
33class InflationCashFlowPricer;
34
35class CPICoupon : public QuantLib::CPICoupon {
36public:
37 CPICoupon(Real baseCPI, // user provided, could be arbitrary
38 const Date& paymentDate, Real nominal, const Date& startDate, const Date& endDate,
39 const ext::shared_ptr<ZeroInflationIndex>& index, const Period& observationLag,
40 CPI::InterpolationType observationInterpolation, const DayCounter& dayCounter,
41 Real fixedRate, // aka gearing
42 const Date& refPeriodStart = Date(), const Date& refPeriodEnd = Date(),
43 const Date& exCouponDate = Date(), bool subtractInflationNominal = false)
44 : QuantLib::CPICoupon(baseCPI, paymentDate, nominal, startDate, endDate, index, observationLag,
45 observationInterpolation, dayCounter, fixedRate, refPeriodStart,
46 refPeriodEnd, exCouponDate),
47 subtractInflationNominal_(subtractInflationNominal){};
48
49 CPICoupon(Real baseCPI,
50 const Date& baseDate, // user provided, could be arbitrary
51 const Date& paymentDate, Real nominal, const Date& startDate, const Date& endDate,
52 const ext::shared_ptr<ZeroInflationIndex>& index, const Period& observationLag,
53 CPI::InterpolationType observationInterpolation, const DayCounter& dayCounter,
54 Real fixedRate, // aka gearing
55 const Date& refPeriodStart = Date(), const Date& refPeriodEnd = Date(),
56 const Date& exCouponDate = Date(), bool subtractInflationNominal = false)
57 : QuantLib::CPICoupon(baseCPI, baseDate, paymentDate, nominal, startDate, endDate, index,
58 observationLag,
59 observationInterpolation, dayCounter, fixedRate, refPeriodStart, refPeriodEnd,
60 exCouponDate),
61 subtractInflationNominal_(subtractInflationNominal){};
62
63 virtual Rate rate() const override;
65
66protected:
68};
69
70
71//! Capped or floored CPI cashflow.
72/*! Extended QuantLib::CPICashFlow
73 */
74class CappedFlooredCPICashFlow : public QuantLib::CPICashFlow {
75public:
76 CappedFlooredCPICashFlow(const ext::shared_ptr<CPICashFlow>& underlying, Date startDate = Date(),
77 Period observationLag = 0 * Days, Rate cap = Null<Rate>(), Rate floor = Null<Rate>());
78
79 virtual Real amount() const override;
80 void setPricer(const ext::shared_ptr<InflationCashFlowPricer>& pricer);
81 bool isCapped() const { return isCapped_; }
82 bool isFloored() const { return isFloored_; }
83 ext::shared_ptr<CPICashFlow> underlying() const { return underlying_; }
84
85private:
86 void setCommon(Rate cap, Rate floor);
87 Real cap_, floor_;
88 ext::shared_ptr<CPICashFlow> underlying_;
89 ext::shared_ptr<CPICapFloor> cpiCap_, cpiFloor_;
93 ext::shared_ptr<InflationCashFlowPricer> pricer_;
94};
95
96//! Capped or floored CPI coupon.
97/*! Extended QuantLib::CPICoupon
98 */
100public:
101 CappedFlooredCPICoupon(const ext::shared_ptr<CPICoupon>& underlying, Date startDate = Date(),
102 Rate cap = Null<Rate>(), Rate floor = Null<Rate>());
103 virtual Rate rate() const override;
104
105 ext::shared_ptr<CPICoupon> underlying() const { return underlying_; }
106
107 //! \name Observer interface
108 //@{
109 void update() override;
110 //@}
111
112 //! \name Visitability
113 //@{
114 virtual void accept(AcyclicVisitor& v) override;
115 //@}
116
117 bool isCapped() const { return isCapped_; }
118 bool isFloored() const { return isFloored_; }
119
120protected:
121 virtual void setCommon(Rate cap, Rate floor);
122
123 ext::shared_ptr<CPICoupon> underlying_;
124 ext::shared_ptr<CPICapFloor> cpiCap_, cpiFloor_;
128};
129
130//! Helper class building a sequence of capped/floored CPI coupons.
131/*! Also allowing for the inflated notional at the end...
132 especially if there is only one date in the schedule.
133 If a fixedRate is zero you get a FixedRateCoupon, otherwise
134 you get a ZeroInflationCoupon.
135
136 payoff is: spread + fixedRate x index
137*/
138class CPILeg {
139public:
140 CPILeg(const Schedule& schedule, const ext::shared_ptr<ZeroInflationIndex>& index,
141 const Handle<YieldTermStructure>& rateCurve, const Real baseCPI, const Period& observationLag);
142 CPILeg& withNotionals(Real notional);
143 CPILeg& withNotionals(const std::vector<Real>& notionals);
144 CPILeg& withFixedRates(Real fixedRate);
145 CPILeg& withFixedRates(const std::vector<Real>& fixedRates);
146 CPILeg& withPaymentDayCounter(const DayCounter&);
147 CPILeg& withPaymentAdjustment(BusinessDayConvention);
148 CPILeg& withPaymentCalendar(const Calendar&);
149 CPILeg& withPaymentLag(Natural lag);
150 CPILeg& withFixingDays(Natural fixingDays);
151 CPILeg& withFixingDays(const std::vector<Natural>& fixingDays);
152 CPILeg& withObservationInterpolation(CPI::InterpolationType);
154 CPILeg& withCaps(Rate cap);
155 CPILeg& withCaps(const std::vector<Rate>& caps);
156 CPILeg& withFloors(Rate floor);
157 CPILeg& withFloors(const std::vector<Rate>& floors);
158 CPILeg& withFinalFlowCap(Rate cap);
159 CPILeg& withFinalFlowFloor(Rate floor);
160 CPILeg& withExCouponPeriod(const Period&, const Calendar&, BusinessDayConvention, bool endOfMonth = false);
161 CPILeg& withStartDate(const Date& startDate);
162 CPILeg& withObservationLag(const Period& observationLag);
163 CPILeg& withSubtractInflationNominalAllCoupons(bool subtractInflationNominalAllCoupons);
164 CPILeg& withBaseDate(const Date& baseDate);
165 operator Leg() const;
166
167private:
168 Schedule schedule_;
169 ext::shared_ptr<ZeroInflationIndex> index_;
170 Handle<YieldTermStructure> rateCurve_;
173 std::vector<Real> notionals_;
174 std::vector<Real> fixedRates_; // aka gearing
176 BusinessDayConvention paymentAdjustment_;
178 Natural paymentLag_;
179 std::vector<Natural> fixingDays_;
180 CPI::InterpolationType observationInterpolation_;
182 std::vector<Rate> caps_, floors_;
186 BusinessDayConvention exCouponAdjustment_;
189
190 // Needed for pricing the embedded caps/floors
192 // Needed if baseCPI is not given
194
195};
196
197} // namespace QuantExt
198
199#endif
virtual Rate rate() const override
Definition: cpicoupon.cpp:34
bool subtractInflationNotional()
Definition: cpicoupon.hpp:64
CPICoupon(Real baseCPI, const Date &baseDate, const Date &paymentDate, Real nominal, const Date &startDate, const Date &endDate, const ext::shared_ptr< ZeroInflationIndex > &index, const Period &observationLag, CPI::InterpolationType observationInterpolation, const DayCounter &dayCounter, Real fixedRate, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date(), const Date &exCouponDate=Date(), bool subtractInflationNominal=false)
Definition: cpicoupon.hpp:49
bool subtractInflationNominal_
Definition: cpicoupon.hpp:67
CPICoupon(Real baseCPI, const Date &paymentDate, Real nominal, const Date &startDate, const Date &endDate, const ext::shared_ptr< ZeroInflationIndex > &index, const Period &observationLag, CPI::InterpolationType observationInterpolation, const DayCounter &dayCounter, Real fixedRate, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date(), const Date &exCouponDate=Date(), bool subtractInflationNominal=false)
Definition: cpicoupon.hpp:37
Helper class building a sequence of capped/floored CPI coupons.
Definition: cpicoupon.hpp:138
CPILeg & withNotionals(Real notional)
Definition: cpicoupon.cpp:250
Schedule schedule_
Definition: cpicoupon.hpp:168
BusinessDayConvention paymentAdjustment_
Definition: cpicoupon.hpp:176
std::vector< Rate > caps_
Definition: cpicoupon.hpp:182
CPILeg & withPaymentAdjustment(BusinessDayConvention)
Definition: cpicoupon.cpp:270
BusinessDayConvention exCouponAdjustment_
Definition: cpicoupon.hpp:186
CPILeg & withFinalFlowCap(Rate cap)
Definition: cpicoupon.cpp:310
CPILeg & withSubtractInflationNominal(bool)
Definition: cpicoupon.cpp:260
Period observationLag_
Definition: cpicoupon.hpp:172
Calendar paymentCalendar_
Definition: cpicoupon.hpp:177
CPILeg & withFixingDays(Natural fixingDays)
Definition: cpicoupon.cpp:285
CPILeg & withObservationLag(const Period &observationLag)
Definition: cpicoupon.cpp:330
ext::shared_ptr< ZeroInflationIndex > index_
Definition: cpicoupon.hpp:169
Natural paymentLag_
Definition: cpicoupon.hpp:178
CPILeg & withBaseDate(const Date &baseDate)
CPILeg & withFixedRates(Real fixedRate)
Definition: cpicoupon.cpp:240
CPILeg & withFinalFlowFloor(Rate floor)
Definition: cpicoupon.cpp:315
CPILeg & withExCouponPeriod(const Period &, const Calendar &, BusinessDayConvention, bool endOfMonth=false)
Definition: cpicoupon.cpp:335
std::vector< Real > notionals_
Definition: cpicoupon.hpp:173
CPILeg & withCaps(Rate cap)
Definition: cpicoupon.cpp:295
Handle< YieldTermStructure > rateCurve_
Definition: cpicoupon.hpp:170
CPILeg & withPaymentDayCounter(const DayCounter &)
Definition: cpicoupon.cpp:265
CPILeg & withStartDate(const Date &startDate)
Definition: cpicoupon.cpp:325
CPILeg & withPaymentCalendar(const Calendar &)
Definition: cpicoupon.cpp:275
CPILeg & withFloors(Rate floor)
Definition: cpicoupon.cpp:305
double finalFlowCap_
Definition: cpicoupon.hpp:183
bool exCouponEndOfMonth_
Definition: cpicoupon.hpp:187
std::vector< Real > fixedRates_
Definition: cpicoupon.hpp:174
std::vector< Natural > fixingDays_
Definition: cpicoupon.hpp:179
CPI::InterpolationType observationInterpolation_
Definition: cpicoupon.hpp:180
Period exCouponPeriod_
Definition: cpicoupon.hpp:184
bool subtractInflationNominalAllCoupons_
Definition: cpicoupon.hpp:188
Calendar exCouponCalendar_
Definition: cpicoupon.hpp:185
CPILeg & withSubtractInflationNominalAllCoupons(bool subtractInflationNominalAllCoupons)
Definition: cpicoupon.cpp:344
double finalFlowFloor_
Definition: cpicoupon.hpp:183
std::vector< Rate > floors_
Definition: cpicoupon.hpp:182
CPILeg & withPaymentLag(Natural lag)
Definition: cpicoupon.cpp:280
bool subtractInflationNominal_
Definition: cpicoupon.hpp:181
CPILeg & withObservationInterpolation(CPI::InterpolationType)
Definition: cpicoupon.cpp:235
DayCounter paymentDayCounter_
Definition: cpicoupon.hpp:175
Capped or floored CPI cashflow.
Definition: cpicoupon.hpp:74
ext::shared_ptr< CPICashFlow > underlying_
Definition: cpicoupon.hpp:88
ext::shared_ptr< CPICashFlow > underlying() const
Definition: cpicoupon.hpp:83
void setPricer(const ext::shared_ptr< InflationCashFlowPricer > &pricer)
Definition: cpicoupon.cpp:197
ext::shared_ptr< CPICapFloor > cpiCap_
Definition: cpicoupon.hpp:89
void setCommon(Rate cap, Rate floor)
Definition: cpicoupon.cpp:180
ext::shared_ptr< InflationCashFlowPricer > pricer_
Definition: cpicoupon.hpp:93
virtual Real amount() const override
Definition: cpicoupon.cpp:206
ext::shared_ptr< CPICapFloor > cpiFloor_
Definition: cpicoupon.hpp:89
Capped or floored CPI coupon.
Definition: cpicoupon.hpp:99
ext::shared_ptr< CPICapFloor > cpiCap_
Definition: cpicoupon.hpp:124
virtual void setCommon(Rate cap, Rate floor)
Definition: cpicoupon.cpp:43
virtual Rate rate() const override
Definition: cpicoupon.cpp:111
ext::shared_ptr< CPICoupon > underlying() const
Definition: cpicoupon.hpp:105
ext::shared_ptr< CPICoupon > underlying_
Definition: cpicoupon.hpp:123
virtual void accept(AcyclicVisitor &v) override
Definition: cpicoupon.cpp:143
ext::shared_ptr< CPICapFloor > cpiFloor_
Definition: cpicoupon.hpp:124