QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
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 const DayCounter& dayCounter,
38 Real gearing,
39 Spread spread,
40 const Date& refPeriodStart,
41 const Date& refPeriodEnd)
42 : InflationCoupon(paymentDate, nominal, startDate, endDate,
43 fixingDays, yoyIndex, observationLag,
44 dayCounter, refPeriodStart, refPeriodEnd),
45 yoyIndex_(yoyIndex), gearing_(gearing), spread_(spread) {}
46
47
49 auto* v1 = dynamic_cast<Visitor<YoYInflationCoupon>*>(&v);
50 if (v1 != nullptr)
51 v1->visit(*this);
52 else
54 }
55
56
58 const ext::shared_ptr<InflationCouponPricer>&pricer) const {
59 return static_cast<bool>(
60 ext::dynamic_pointer_cast<YoYInflationCouponPricer>(pricer));
61 }
62
63
65 Calendar paymentCalendar,
66 ext::shared_ptr<YoYInflationIndex> index,
67 const Period& observationLag)
68 : schedule_(std::move(schedule)), index_(std::move(index)), observationLag_(observationLag),
69 paymentCalendar_(std::move(paymentCalendar)) {}
70
71
73 notionals_ = std::vector<Real>(1,notional);
74 return *this;
75 }
76
77 yoyInflationLeg& yoyInflationLeg::withNotionals(const std::vector<Real>& notionals) {
78 notionals_ = notionals;
79 return *this;
80 }
81
83 paymentDayCounter_ = dayCounter;
84 return *this;
85 }
86
88 paymentAdjustment_ = convention;
89 return *this;
90 }
91
93 fixingDays_ = std::vector<Natural>(1,fixingDays);
94 return *this;
95 }
96
97 yoyInflationLeg& yoyInflationLeg::withFixingDays(const std::vector<Natural>& fixingDays) {
98 fixingDays_ = fixingDays;
99 return *this;
100 }
101
103 gearings_ = std::vector<Real>(1,gearing);
104 return *this;
105 }
106
107 yoyInflationLeg& yoyInflationLeg::withGearings(const std::vector<Real>& gearings) {
108 gearings_ = gearings;
109 return *this;
110 }
111
113 spreads_ = std::vector<Spread>(1,spread);
114 return *this;
115 }
116
117 yoyInflationLeg& yoyInflationLeg::withSpreads(const std::vector<Spread>& spreads) {
118 spreads_ = spreads;
119 return *this;
120 }
121
123 caps_ = std::vector<Rate>(1,cap);
124 return *this;
125 }
126
127 yoyInflationLeg& yoyInflationLeg::withCaps(const std::vector<Rate>& caps) {
128 caps_ = caps;
129 return *this;
130 }
131
133 floors_ = std::vector<Rate>(1,floor);
134 return *this;
135 }
136
137 yoyInflationLeg& yoyInflationLeg::withFloors(const std::vector<Rate>& floors) {
138 floors_ = floors;
139 return *this;
140 }
141
142
143 yoyInflationLeg::operator Leg() const {
144
145 Size n = schedule_.size()-1;
146 QL_REQUIRE(!paymentDayCounter_.empty(), "no payment daycounter given");
147 QL_REQUIRE(!notionals_.empty(), "no notional given");
148 QL_REQUIRE(notionals_.size() <= n,
149 "too many nominals (" << notionals_.size() <<
150 "), only " << n << " required");
151 QL_REQUIRE(gearings_.size()<=n,
152 "too many gearings (" << gearings_.size() <<
153 "), only " << n << " required");
154 QL_REQUIRE(spreads_.size()<=n,
155 "too many spreads (" << spreads_.size() <<
156 "), only " << n << " required");
157 QL_REQUIRE(caps_.size()<=n,
158 "too many caps (" << caps_.size() <<
159 "), only " << n << " required");
160 QL_REQUIRE(floors_.size()<=n,
161 "too many floors (" << floors_.size() <<
162 "), only " << n << " required");
163
164 Leg leg; leg.reserve(n);
165
166 Calendar calendar = paymentCalendar_;
167
168 Date refStart, start, refEnd, end;
169
170 for (Size i=0; i<n; ++i) {
171 refStart = start = schedule_.date(i);
172 refEnd = end = schedule_.date(i+1);
173 Date paymentDate = calendar.adjust(end, paymentAdjustment_);
174 if (i==0 && schedule_.hasIsRegular() && !schedule_.isRegular(i+1)) {
175 BusinessDayConvention bdc = schedule_.businessDayConvention();
176 refStart = schedule_.calendar().adjust(end - schedule_.tenor(), bdc);
177 }
178 if (i==n-1 && schedule_.hasIsRegular() && !schedule_.isRegular(i+1)) {
179 BusinessDayConvention bdc = schedule_.businessDayConvention();
180 refEnd = schedule_.calendar().adjust(start + schedule_.tenor(), bdc);
181 }
182 if (detail::get(gearings_, i, 1.0) == 0.0) { // fixed coupon
183 leg.push_back(ext::make_shared<FixedRateCoupon>(
184 paymentDate,
185 detail::get(notionals_, i, 1.0),
186 detail::effectiveFixedRate(spreads_,caps_,
187 floors_,i),
188 paymentDayCounter_,
189 start, end, refStart, refEnd));
190 } else { // yoy inflation coupon
191 if (detail::noOption(caps_, floors_, i)) { // just swaplet
192 leg.push_back(ext::make_shared<YoYInflationCoupon>(
193 paymentDate,
194 detail::get(notionals_, i, 1.0),
195 start, end,
196 detail::get(fixingDays_, i, 0),
197 index_,
198 observationLag_,
199 paymentDayCounter_,
200 detail::get(gearings_, i, 1.0),
201 detail::get(spreads_, i, 0.0),
202 refStart, refEnd));
203 } else { // cap/floorlet
204 leg.push_back(ext::make_shared<CappedFlooredYoYInflationCoupon>(
205 paymentDate,
206 detail::get(notionals_, i, 1.0),
207 start, end,
208 detail::get(fixingDays_, i, 0),
209 index_,
210 observationLag_,
211 paymentDayCounter_,
212 detail::get(gearings_, i, 1.0),
213 detail::get(spreads_, i, 0.0),
214 detail::get(caps_, i, Null<Rate>()),
215 detail::get(floors_, i, Null<Rate>()),
216 refStart, refEnd));
217 }
218 }
219 }
220
221 // Without caps or floors, this is enough; otherwise, a more
222 // specific pricer will need to be set in client code.
223 if (caps_.empty() && floors_.empty())
224 setCouponPricer(leg, ext::make_shared<YoYInflationCouponPricer>());
225
226 return leg;
227 }
228
229}
230
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
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
template class providing a null value for a given type.
Definition: null.hpp:76
Payment schedule.
Definition: schedule.hpp:40
Visitor for a specific class
Definition: visitor.hpp:40
virtual void visit(T &)=0
bool checkPricerImpl(const ext::shared_ptr< InflationCouponPricer > &) const override
makes sure you were given the correct type of pricer
YoYInflationCoupon(const Date &paymentDate, Real nominal, const Date &startDate, const Date &endDate, Natural fixingDays, const ext::shared_ptr< YoYInflationIndex > &index, const Period &observationLag, const DayCounter &dayCounter, Real gearing=1.0, Spread spread=0.0, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date())
void accept(AcyclicVisitor &) override
BusinessDayConvention paymentAdjustment_
yoyInflationLeg & withSpreads(Spread spread)
yoyInflationLeg & withFloors(Rate floor)
yoyInflationLeg & withPaymentAdjustment(BusinessDayConvention)
yoyInflationLeg(Schedule schedule, Calendar cal, ext::shared_ptr< YoYInflationIndex > index, const Period &observationLag)
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 & withFixingDays(Natural fixingDays)
#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:35
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:35
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