QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
cpicoupon.hpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2011 Chris Kenyon
5 Copyright (C) 2022 Quaternion Risk Management Ltd
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19 */
20
21/*! \file cpicoupon.hpp
22 \brief Coupon paying a zero-inflation index
23*/
24
25#ifndef quantlib_cpicoupon_hpp
26#define quantlib_cpicoupon_hpp
27
31#include <ql/time/schedule.hpp>
32
33namespace QuantLib {
34
35
36 class CPICouponPricer;
37
38 //! %Coupon paying the performance of a CPI (zero inflation) index
39 /*! The performance is relative to the index value on the base date.
40
41 The other inflation value is taken from the refPeriodEnd date
42 with observation lag, so any roll/calendar etc. will be built
43 in by the caller. By default this is done in the
44 InflationCoupon which uses ModifiedPreceding with fixing days
45 assumed positive meaning earlier, i.e. always stay in same
46 month (relative to referencePeriodEnd).
47
48 This is more sophisticated than an %IndexedCashFlow because it
49 does date calculations itself.
50
51 \todo we do not do any convexity adjustment for lags different
52 to the natural ZCIIS lag that was used to create the
53 forward inflation curve.
54 */
55 class CPICoupon : public InflationCoupon {
56 public:
57 //! \name Constructors
58 //@{
59 /*! This constructor takes the base CPI to be used in the calculations. */
61 const Date& paymentDate,
63 const Date& startDate,
64 const Date& endDate,
65 const ext::shared_ptr<ZeroInflationIndex>& index,
70 const Date& refPeriodStart = Date(),
71 const Date& refPeriodEnd = Date(),
72 const Date& exCouponDate = Date());
73
74 /*! This constructor takes a base date; the coupon will use it
75 to retrieve the base CPI to be used in the calculations.
76 */
77 CPICoupon(const Date& baseDate,
78 const Date& paymentDate,
80 const Date& startDate,
81 const Date& endDate,
82 const ext::shared_ptr<ZeroInflationIndex>& index,
87 const Date& refPeriodStart = Date(),
88 const Date& refPeriodEnd = Date(),
89 const Date& exCouponDate = Date());
90
91 /*! This constructor takes both a base CPI and a base date.
92 If both are passed, the base CPI is used in the calculations.
93 */
95 const Date& baseDate,
96 const Date& paymentDate,
98 const Date& startDate,
99 const Date& endDate,
100 const ext::shared_ptr<ZeroInflationIndex>& index,
101 const Period& observationLag,
103 const DayCounter& dayCounter,
105 const Date& refPeriodStart = Date(),
106 const Date& refPeriodEnd = Date(),
107 const Date& exCouponDate = Date());
108 //@}
109
110 //! \name Inspectors
111 //@{
112 //! fixed rate that will be inflated by the index ratio
113 Real fixedRate() const;
114
115 //! base value for the CPI index
116 /*! \warning make sure that the interpolation used to create
117 this is what you are using for the fixing,
118 i.e. the observationInterpolation.
119 */
120 Rate baseCPI() const;
121
122 //! base date for the base fixing of the CPI index
123 Date baseDate() const;
124
125 //! how do you observe the index? as-is, flat, linear?
127
128 //! index used
129 ext::shared_ptr<ZeroInflationIndex> cpiIndex() const;
130 //@}
131
132 //! \name Calculations
133 //@{
134 Real accruedAmount(const Date&) const override;
135
136 //! the index value observed (with a lag) at the end date
137 Rate indexFixing() const override;
138
139 //! the ratio between the index fixing at the passed date and the base CPI
140 /*! No adjustments are applied */
141 Rate indexRatio(Date d) const;
142
143 //! the ratio between the end index fixing and the base CPI
144 /*! This might include adjustments calculated by the pricer */
146 //@}
147
148 //! \name Visitability
149 //@{
150 void accept(AcyclicVisitor&) override;
151 //@}
152 protected:
157
158 bool checkPricerImpl(const ext::shared_ptr<InflationCouponPricer>&) const override;
159 };
160
161
162 //! Cash flow paying the performance of a CPI (zero inflation) index
163 /*! It is NOT a coupon, i.e. no accruals. */
165 public:
167 const ext::shared_ptr<ZeroInflationIndex>& index,
168 const Date& baseDate,
170 const Date& observationDate,
171 const Period& observationLag,
173 const Date& paymentDate,
174 bool growthOnly = false);
175
176 //! value used on base date
177 /*! This does not have to agree with index on that date. */
178 Real baseFixing() const override;
179 //! you may not have a valid date
180 Date baseDate() const override;
181
184 //! do you want linear/constant/as-index interpolation of future data?
186 return interpolation_;
187 }
188 virtual Frequency frequency() const { return frequency_; }
189
190 ext::shared_ptr<ZeroInflationIndex> cpiIndex() const;
191
192 Real indexFixing() const override;
193
194 protected:
200 };
201
202
203 //! Helper class building a sequence of capped/floored CPI coupons.
204 /*! Also allowing for the inflated notional at the end...
205 especially if there is only one date in the schedule.
206 If the fixed rate is zero you get a FixedRateCoupon, otherwise
207 you get a ZeroInflationCoupon.
208 */
209 class CPILeg {
210 public:
211 CPILeg(Schedule schedule,
212 ext::shared_ptr<ZeroInflationIndex> index,
213 Real baseCPI,
214 const Period& observationLag);
215 CPILeg& withNotionals(Real notional);
216 CPILeg& withNotionals(const std::vector<Real>& notionals);
217 CPILeg& withFixedRates(Real fixedRate);
218 CPILeg& withFixedRates(const std::vector<Real>& fixedRates);
224 CPILeg& withCaps(Rate cap);
225 CPILeg& withCaps(const std::vector<Rate>& caps);
226 CPILeg& withFloors(Rate floor);
227 CPILeg& withFloors(const std::vector<Rate>& floors);
229 const Calendar&,
231 bool endOfMonth = false);
232 CPILeg& withBaseDate(const Date& baseDate);
233
234 operator Leg() const;
235
236 private:
238 ext::shared_ptr<ZeroInflationIndex> index_;
241 std::vector<Real> notionals_;
242 std::vector<Real> fixedRates_;
248 std::vector<Rate> caps_, floors_;
254 };
255
256
257 // inline definitions
258
259 inline Real CPICoupon::fixedRate() const {
260 return fixedRate_;
261 }
262
264 return rate()/fixedRate();
265 }
266
269 }
270
271 inline Rate CPICoupon::baseCPI() const {
272 return baseCPI_;
273 }
274
275 inline Date CPICoupon::baseDate() const {
276 return baseDate_;
277 }
278
281 }
282
283 inline ext::shared_ptr<ZeroInflationIndex> CPICoupon::cpiIndex() const {
284 return ext::dynamic_pointer_cast<ZeroInflationIndex>(index());
285 }
286
287
288 inline ext::shared_ptr<ZeroInflationIndex> CPICashFlow::cpiIndex() const {
289 return ext::dynamic_pointer_cast<ZeroInflationIndex>(index());
290 }
291
292}
293
294#endif
degenerate base class for the Acyclic Visitor pattern
Definition: visitor.hpp:33
Cash flow paying the performance of a CPI (zero inflation) index.
Definition: cpicoupon.hpp:164
Date observationDate() const
Definition: cpicoupon.hpp:182
ext::shared_ptr< ZeroInflationIndex > cpiIndex() const
Definition: cpicoupon.hpp:288
CPI::InterpolationType interpolation_
Definition: cpicoupon.hpp:198
Date baseDate() const override
you may not have a valid date
Definition: cpicoupon.cpp:159
Real baseFixing() const override
value used on base date
Definition: cpicoupon.cpp:168
Period observationLag() const
Definition: cpicoupon.hpp:183
virtual Frequency frequency() const
Definition: cpicoupon.hpp:188
Real indexFixing() const override
Definition: cpicoupon.cpp:175
virtual CPI::InterpolationType interpolation() const
do you want linear/constant/as-index interpolation of future data?
Definition: cpicoupon.hpp:185
Coupon paying the performance of a CPI (zero inflation) index
Definition: cpicoupon.hpp:55
bool checkPricerImpl(const ext::shared_ptr< InflationCouponPricer > &) const override
makes sure you were given the correct type of pricer
Definition: cpicoupon.cpp:131
Rate baseCPI() const
base value for the CPI index
Definition: cpicoupon.hpp:271
Rate indexFixing() const override
the index value observed (with a lag) at the end date
Definition: cpicoupon.hpp:267
ext::shared_ptr< ZeroInflationIndex > cpiIndex() const
index used
Definition: cpicoupon.hpp:283
CPI::InterpolationType observationInterpolation() const
how do you observe the index? as-is, flat, linear?
Definition: cpicoupon.hpp:279
void accept(AcyclicVisitor &) override
Definition: cpicoupon.cpp:93
Rate indexRatio(Date d) const
the ratio between the index fixing at the passed date and the base CPI
Definition: cpicoupon.cpp:112
Date baseDate() const
base date for the base fixing of the CPI index
Definition: cpicoupon.hpp:275
Real accruedAmount(const Date &) const override
accrued amount at the given date
Definition: cpicoupon.cpp:101
CPI::InterpolationType observationInterpolation_
Definition: cpicoupon.hpp:155
Real fixedRate() const
fixed rate that will be inflated by the index ratio
Definition: cpicoupon.hpp:259
Rate adjustedIndexGrowth() const
the ratio between the end index fixing and the base CPI
Definition: cpicoupon.hpp:263
Helper class building a sequence of capped/floored CPI coupons.
Definition: cpicoupon.hpp:209
CPILeg & withNotionals(Real notional)
Definition: cpicoupon.cpp:204
Schedule schedule_
Definition: cpicoupon.hpp:237
BusinessDayConvention paymentAdjustment_
Definition: cpicoupon.hpp:244
std::vector< Rate > caps_
Definition: cpicoupon.hpp:248
CPILeg & withPaymentAdjustment(BusinessDayConvention)
Definition: cpicoupon.cpp:224
BusinessDayConvention exCouponAdjustment_
Definition: cpicoupon.hpp:251
CPILeg & withSubtractInflationNominal(bool)
Definition: cpicoupon.cpp:214
Period observationLag_
Definition: cpicoupon.hpp:240
Calendar paymentCalendar_
Definition: cpicoupon.hpp:245
ext::shared_ptr< ZeroInflationIndex > index_
Definition: cpicoupon.hpp:238
CPILeg & withBaseDate(const Date &baseDate)
Definition: cpicoupon.cpp:266
CPILeg & withFixedRates(Real fixedRate)
Definition: cpicoupon.cpp:194
CPILeg & withExCouponPeriod(const Period &, const Calendar &, BusinessDayConvention, bool endOfMonth=false)
Definition: cpicoupon.cpp:254
std::vector< Real > notionals_
Definition: cpicoupon.hpp:241
CPILeg & withCaps(Rate cap)
Definition: cpicoupon.cpp:234
CPILeg & withPaymentDayCounter(const DayCounter &)
Definition: cpicoupon.cpp:219
CPILeg & withPaymentCalendar(const Calendar &)
Definition: cpicoupon.cpp:229
CPILeg & withFloors(Rate floor)
Definition: cpicoupon.cpp:244
bool exCouponEndOfMonth_
Definition: cpicoupon.hpp:252
std::vector< Real > fixedRates_
Definition: cpicoupon.hpp:242
CPI::InterpolationType observationInterpolation_
Definition: cpicoupon.hpp:246
Period exCouponPeriod_
Definition: cpicoupon.hpp:249
Calendar exCouponCalendar_
Definition: cpicoupon.hpp:250
std::vector< Rate > floors_
Definition: cpicoupon.hpp:248
bool subtractInflationNominal_
Definition: cpicoupon.hpp:247
CPILeg & withObservationInterpolation(CPI::InterpolationType)
Definition: cpicoupon.cpp:188
DayCounter paymentDayCounter_
Definition: cpicoupon.hpp:243
calendar class
Definition: calendar.hpp:61
Date exCouponDate() const override
returns the date that the cash flow trades exCoupon
Definition: coupon.hpp:57
virtual Real nominal() const
Definition: coupon.hpp:100
const Date & accrualEndDate() const
end of the accrual period
Definition: coupon.hpp:108
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Cash flow dependent on an index ratio.
virtual Real notional() const
virtual bool growthOnly() const
virtual ext::shared_ptr< Index > index() const
Base inflation-coupon class.
Rate rate() const override
accrued rate
Period observationLag() const
how the coupon observes the index
DayCounter dayCounter() const override
day counter for accrual calculation
const ext::shared_ptr< InflationIndex > & index() const
yoy inflation index
Payment schedule.
Definition: schedule.hpp:40
Date d
Frequency
Frequency of events.
Definition: frequency.hpp:37
BusinessDayConvention
Business Day conventions.
QL_REAL Real
real number
Definition: types.hpp:50
Real Rate
interest rates
Definition: types.hpp:70
Cash flow dependent on an index ratio (NOT a coupon, i.e. no accruals)
Coupon paying a variable index-based rate.
base classes for inflation indexes
Definition: any.hpp:37
std::vector< ext::shared_ptr< CashFlow > > Leg
Sequence of cash-flows.
Definition: cashflow.hpp:78
date schedule
static Real laggedFixing(const ext::shared_ptr< ZeroInflationIndex > &index, const Date &date, const Period &observationLag, InterpolationType interpolationType)
interpolated inflation fixing
InterpolationType
when you observe an index, how do you interpolate between fixings?
@ AsIndex
same interpolation as index