QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
yoyinflationcoupon.cpp
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) 2009 Chris Kenyon
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18 */
19
20
25#include <utility>
26
27namespace QuantLib {
28
30 YoYInflationCoupon(const Date& paymentDate,
31 Real nominal,
32 const Date& startDate,
33 const Date& endDate,
34 Natural fixingDays,
35 const ext::shared_ptr<YoYInflationIndex>& yoyIndex,
36 const Period& observationLag,
37 CPI::InterpolationType interpolation,
38 const DayCounter& dayCounter,
39 Real gearing,
40 Spread spread,
41 const Date& refPeriodStart,
42 const Date& refPeriodEnd)
43 : InflationCoupon(paymentDate, nominal, startDate, endDate,
44 fixingDays, yoyIndex, observationLag,
45 dayCounter, refPeriodStart, refPeriodEnd),
46 yoyIndex_(yoyIndex), interpolation_(interpolation), gearing_(gearing), spread_(spread) {}
47
49 YoYInflationCoupon(const Date& paymentDate,
50 Real nominal,
51 const Date& startDate,
52 const Date& endDate,
53 Natural fixingDays,
54 const ext::shared_ptr<YoYInflationIndex>& yoyIndex,
55 const Period& observationLag,
56 const DayCounter& dayCounter,
57 Real gearing,
58 Spread spread,
59 const Date& refPeriodStart,
60 const Date& refPeriodEnd)
61 : YoYInflationCoupon(paymentDate, nominal, startDate, endDate,
62 fixingDays, yoyIndex, observationLag, CPI::AsIndex,
63 dayCounter, gearing, spread, refPeriodStart, refPeriodEnd) {}
64
65
67 auto* v1 = dynamic_cast<Visitor<YoYInflationCoupon>*>(&v);
68 if (v1 != nullptr)
69 v1->visit(*this);
70 else
72 }
73
75 const ext::shared_ptr<InflationCouponPricer>&pricer) const {
76 return static_cast<bool>(
77 ext::dynamic_pointer_cast<YoYInflationCouponPricer>(pricer));
78 }
79
82 }
83
84
86 Calendar paymentCalendar,
87 ext::shared_ptr<YoYInflationIndex> index,
88 const Period& observationLag,
89 CPI::InterpolationType interpolation)
90 : schedule_(std::move(schedule)), index_(std::move(index)), observationLag_(observationLag),
91 interpolation_(interpolation), paymentCalendar_(std::move(paymentCalendar)) {}
92
94 Calendar paymentCalendar,
95 ext::shared_ptr<YoYInflationIndex> index,
96 const Period& observationLag)
97 : yoyInflationLeg(std::move(schedule), std::move(paymentCalendar), std::move(index), observationLag, CPI::AsIndex) {}
98
99
101 notionals_ = std::vector<Real>(1,notional);
102 return *this;
103 }
104
105 yoyInflationLeg& yoyInflationLeg::withNotionals(const std::vector<Real>& notionals) {
106 notionals_ = notionals;
107 return *this;
108 }
109
111 paymentDayCounter_ = dayCounter;
112 return *this;
113 }
114
116 paymentAdjustment_ = convention;
117 return *this;
118 }
119
121 fixingDays_ = std::vector<Natural>(1,fixingDays);
122 return *this;
123 }
124
125 yoyInflationLeg& yoyInflationLeg::withFixingDays(const std::vector<Natural>& fixingDays) {
126 fixingDays_ = fixingDays;
127 return *this;
128 }
129
131 gearings_ = std::vector<Real>(1,gearing);
132 return *this;
133 }
134
135 yoyInflationLeg& yoyInflationLeg::withGearings(const std::vector<Real>& gearings) {
136 gearings_ = gearings;
137 return *this;
138 }
139
141 spreads_ = std::vector<Spread>(1,spread);
142 return *this;
143 }
144
145 yoyInflationLeg& yoyInflationLeg::withSpreads(const std::vector<Spread>& spreads) {
146 spreads_ = spreads;
147 return *this;
148 }
149
151 caps_ = std::vector<Rate>(1,cap);
152 return *this;
153 }
154
155 yoyInflationLeg& yoyInflationLeg::withCaps(const std::vector<Rate>& caps) {
156 caps_ = caps;
157 return *this;
158 }
159
161 floors_ = std::vector<Rate>(1,floor);
162 return *this;
163 }
164
165 yoyInflationLeg& yoyInflationLeg::withFloors(const std::vector<Rate>& floors) {
166 floors_ = floors;
167 return *this;
168 }
169
170
171 yoyInflationLeg::operator Leg() const {
172
173 Size n = schedule_.size()-1;
174 QL_REQUIRE(!paymentDayCounter_.empty(), "no payment daycounter given");
175 QL_REQUIRE(!notionals_.empty(), "no notional given");
176 QL_REQUIRE(notionals_.size() <= n,
177 "too many nominals (" << notionals_.size() <<
178 "), only " << n << " required");
179 QL_REQUIRE(gearings_.size()<=n,
180 "too many gearings (" << gearings_.size() <<
181 "), only " << n << " required");
182 QL_REQUIRE(spreads_.size()<=n,
183 "too many spreads (" << spreads_.size() <<
184 "), only " << n << " required");
185 QL_REQUIRE(caps_.size()<=n,
186 "too many caps (" << caps_.size() <<
187 "), only " << n << " required");
188 QL_REQUIRE(floors_.size()<=n,
189 "too many floors (" << floors_.size() <<
190 "), only " << n << " required");
191
192 Leg leg; leg.reserve(n);
193
194 Calendar calendar = paymentCalendar_;
195
196 Date refStart, start, refEnd, end;
197
198 for (Size i=0; i<n; ++i) {
199 refStart = start = schedule_.date(i);
200 refEnd = end = schedule_.date(i+1);
201 Date paymentDate = calendar.adjust(end, paymentAdjustment_);
202 if (i==0 && schedule_.hasIsRegular() && !schedule_.isRegular(i+1)) {
203 BusinessDayConvention bdc = schedule_.businessDayConvention();
204 refStart = schedule_.calendar().adjust(end - schedule_.tenor(), bdc);
205 }
206 if (i==n-1 && schedule_.hasIsRegular() && !schedule_.isRegular(i+1)) {
207 BusinessDayConvention bdc = schedule_.businessDayConvention();
208 refEnd = schedule_.calendar().adjust(start + schedule_.tenor(), bdc);
209 }
210 if (detail::get(gearings_, i, 1.0) == 0.0) { // fixed coupon
211 leg.push_back(ext::make_shared<FixedRateCoupon>(
212 paymentDate,
213 detail::get(notionals_, i, 1.0),
214 detail::effectiveFixedRate(spreads_,caps_,
215 floors_,i),
216 paymentDayCounter_,
217 start, end, refStart, refEnd));
218 } else { // yoy inflation coupon
219 if (detail::noOption(caps_, floors_, i)) { // just swaplet
220 leg.push_back(ext::make_shared<YoYInflationCoupon>(
221 paymentDate,
222 detail::get(notionals_, i, 1.0),
223 start, end,
224 detail::get(fixingDays_, i, 0),
225 index_,
226 observationLag_,
227 interpolation_,
228 paymentDayCounter_,
229 detail::get(gearings_, i, 1.0),
230 detail::get(spreads_, i, 0.0),
231 refStart, refEnd));
232 } else { // cap/floorlet
233 leg.push_back(ext::make_shared<CappedFlooredYoYInflationCoupon>(
234 paymentDate,
235 detail::get(notionals_, i, 1.0),
236 start, end,
237 detail::get(fixingDays_, i, 0),
238 index_,
239 observationLag_,
240 interpolation_,
241 paymentDayCounter_,
242 detail::get(gearings_, i, 1.0),
243 detail::get(spreads_, i, 0.0),
244 detail::get(caps_, i, Null<Rate>()),
245 detail::get(floors_, i, Null<Rate>()),
246 refStart, refEnd));
247 }
248 }
249 }
250
251 // Without caps or floors, this is enough; otherwise, a more
252 // specific pricer will need to be set in client code.
253 if (caps_.empty() && floors_.empty())
254 setCouponPricer(leg, ext::make_shared<YoYInflationCouponPricer>());
255
256 return leg;
257 }
258
259}
260
caplet and floorlet pricing for YoY inflation coupons
Cash flow vector builders.
degenerate base class for the Acyclic Visitor pattern
Definition: visitor.hpp:33
calendar class
Definition: calendar.hpp:61
Date adjust(const Date &, BusinessDayConvention convention=Following) const
Definition: calendar.cpp:84
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
Base inflation-coupon class.
ext::shared_ptr< InflationCouponPricer > pricer() const
void accept(AcyclicVisitor &) override
Period observationLag() const
how the coupon observes the index
template class providing a null value for a given type.
Definition: null.hpp:59
Payment schedule.
Definition: schedule.hpp:40
Visitor for a specific class
Definition: visitor.hpp:40
virtual void visit(T &)=0
Coupon paying a YoY-inflation type index
bool checkPricerImpl(const ext::shared_ptr< InflationCouponPricer > &) const override
makes sure you were given the correct type of pricer
Rate indexFixing() const override
fixing of the underlying index, as observed by the coupon
CPI::InterpolationType interpolation_
void accept(AcyclicVisitor &) override
const ext::shared_ptr< YoYInflationIndex > & yoyIndex() const
YoYInflationCoupon(const Date &paymentDate, Real nominal, const Date &startDate, const Date &endDate, Natural fixingDays, const ext::shared_ptr< YoYInflationIndex > &index, const Period &observationLag, CPI::InterpolationType interpolation, const DayCounter &dayCounter, Real gearing=1.0, Spread spread=0.0, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date())
Helper class building a sequence of capped/floored yoy inflation coupons.
BusinessDayConvention paymentAdjustment_
yoyInflationLeg & withSpreads(Spread spread)
yoyInflationLeg & withFloors(Rate floor)
yoyInflationLeg & withPaymentAdjustment(BusinessDayConvention)
yoyInflationLeg & withNotionals(Real notional)
yoyInflationLeg & withGearings(Real gearing)
std::vector< Real > notionals_
std::vector< Spread > spreads_
yoyInflationLeg & withPaymentDayCounter(const DayCounter &)
std::vector< Natural > fixingDays_
yoyInflationLeg & withCaps(Rate cap)
yoyInflationLeg(Schedule schedule, Calendar cal, ext::shared_ptr< YoYInflationIndex > index, const Period &observationLag, CPI::InterpolationType interpolation)
yoyInflationLeg & withFixingDays(Natural fixingDays)
Calendar paymentCalendar_
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
BusinessDayConvention
Business Day conventions.
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
Real Spread
spreads on interest rates
Definition: types.hpp:74
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Coupon paying a variable index-based rate.
inflation-coupon pricers
T get(const std::vector< T > &v, Size i, U defaultValue)
Definition: vectors.hpp:33
Rate effectiveFixedRate(const std::vector< Spread > &spreads, const std::vector< Rate > &caps, const std::vector< Rate > &floors, Size i)
bool noOption(const std::vector< Rate > &caps, const std::vector< Rate > &floors, Size i)
Definition: any.hpp:37
void setCouponPricer(const Leg &leg, const ext::shared_ptr< FloatingRateCouponPricer > &pricer)
std::vector< ext::shared_ptr< CashFlow > > Leg
Sequence of cash-flows.
Definition: cashflow.hpp:78
STL namespace.
ext::shared_ptr< BlackVolTermStructure > v
static Real laggedYoYRate(const ext::shared_ptr< YoYInflationIndex > &index, const Date &date, const Period &observationLag, InterpolationType interpolationType)
interpolated year-on-year inflation rate
InterpolationType
when you observe an index, how do you interpolate between fixings?