Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
yoyinflationcoupon.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 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#include <ql/cashflows/capflooredinflationcoupon.hpp>
20#include <ql/cashflows/cashflowvectors.hpp>
21#include <ql/cashflows/inflationcoupon.hpp>
22#include <ql/cashflows/inflationcouponpricer.hpp>
24
25namespace QuantExt {
26
27YoYInflationCoupon::YoYInflationCoupon(const Date& paymentDate, Real nominal, const Date& startDate,
28 const Date& endDate, Natural fixingDays,
29 const ext::shared_ptr<YoYInflationIndex>& index, const Period& observationLag,
30 const DayCounter& dayCounter, Real gearing, Spread spread,
31 const Date& refPeriodStart, const Date& refPeriodEnd, bool addInflationNotional)
32 : QuantLib::YoYInflationCoupon(paymentDate, nominal, startDate, endDate, fixingDays, index, observationLag,
33 dayCounter, gearing, spread, refPeriodStart, refPeriodEnd),
34 addInflationNotional_(addInflationNotional) {}
35
36void YoYInflationCoupon::accept(AcyclicVisitor& v) {
37 Visitor<YoYInflationCoupon>* v1 = dynamic_cast<Visitor<YoYInflationCoupon>*>(&v);
38 if (v1 != 0)
39 v1->visit(*this);
40 else
41 QuantLib::YoYInflationCoupon::accept(v);
42}
43
45 Real RateYoY = QuantLib::YoYInflationCoupon::rate();
47 RateYoY = gearing_ * ((RateYoY - spread_) / gearing_ + 1) + spread_;
48 }
49 return RateYoY;
50}
51
53 const ext::shared_ptr<QuantLib::YoYInflationCoupon>& underlying, Rate cap, Rate floor, bool addInflationNotional)
54 : QuantLib::CappedFlooredYoYInflationCoupon(underlying, cap, floor), addInflationNotional_(addInflationNotional) {
56 if (isCapped_) {
57 cap_ = cap_ - 1;
58 }
59 if (isFloored_) {
60 floor_ = floor_ - 1;
61 }
62 }
63}
64
66 const Date& paymentDate, Real nominal, const Date& startDate, const Date& endDate, Natural fixingDays,
67 const ext::shared_ptr<YoYInflationIndex>& index, const Period& observationLag, const DayCounter& dayCounter,
68 Real gearing, Spread spread, const Rate cap, const Rate floor, const Date& refPeriodStart, const Date& refPeriodEnd,
69 bool addInflationNotional)
70 : QuantLib::CappedFlooredYoYInflationCoupon(paymentDate, nominal, startDate, endDate, fixingDays, index,
71 observationLag, dayCounter, gearing, spread, cap, floor, refPeriodStart,
72 refPeriodEnd),
73 addInflationNotional_(addInflationNotional) {
75 if (isCapped_) {
76 cap_ = cap_ - 1;
77 }
78 if (isFloored_) {
79 floor_ = floor_ - 1;
80 }
81 }
82}
83
85 Visitor<CappedFlooredYoYInflationCoupon>* v1 = dynamic_cast<Visitor<CappedFlooredYoYInflationCoupon>*>(&v);
86 if (v1 != 0)
87 v1->visit(*this);
88 else
89 QuantLib::CappedFlooredYoYInflationCoupon::accept(v);
90}
91
93 Real RateYoY = QuantLib::CappedFlooredYoYInflationCoupon::rate();
95 RateYoY = gearing_ * ((RateYoY - spread_) / gearing_ + 1) + spread_;
96 }
97 return RateYoY;
98}
99
100yoyInflationLeg::yoyInflationLeg(Schedule schedule, Calendar paymentCalendar, ext::shared_ptr<YoYInflationIndex> index,
101 const Period& observationLag)
102 : schedule_(std::move(schedule)), index_(std::move(index)), observationLag_(observationLag),
103 paymentAdjustment_(ModifiedFollowing), paymentCalendar_(std::move(paymentCalendar)),
104 addInflationNotional_(false) {}
105
107 notionals_ = std::vector<Real>(1, notional);
108 return *this;
109}
110
111yoyInflationLeg& yoyInflationLeg::withNotionals(const std::vector<Real>& notionals) {
112 notionals_ = notionals;
113 return *this;
114}
115
117 paymentDayCounter_ = dayCounter;
118 return *this;
119}
120
121yoyInflationLeg& yoyInflationLeg::withPaymentAdjustment(BusinessDayConvention convention) {
122 paymentAdjustment_ = convention;
123 return *this;
124}
125
127 fixingDays_ = std::vector<Natural>(1, fixingDays);
128 return *this;
129}
130
131yoyInflationLeg& yoyInflationLeg::withFixingDays(const std::vector<Natural>& fixingDays) {
132 fixingDays_ = fixingDays;
133 return *this;
134}
135
137 gearings_ = std::vector<Real>(1, gearing);
138 return *this;
139}
140
141yoyInflationLeg& yoyInflationLeg::withGearings(const std::vector<Real>& gearings) {
142 gearings_ = gearings;
143 return *this;
144}
145
147 spreads_ = std::vector<Spread>(1, spread);
148 return *this;
149}
150
151yoyInflationLeg& yoyInflationLeg::withSpreads(const std::vector<Spread>& spreads) {
152 spreads_ = spreads;
153 return *this;
154}
155
157 caps_ = std::vector<Rate>(1, cap);
158 return *this;
159}
160
161yoyInflationLeg& yoyInflationLeg::withCaps(const std::vector<Rate>& caps) {
162 caps_ = caps;
163 return *this;
164}
165
167 floors_ = std::vector<Rate>(1, floor);
168 return *this;
169}
170
171yoyInflationLeg& yoyInflationLeg::withFloors(const std::vector<Rate>& floors) {
172 floors_ = floors;
173 return *this;
174}
175
176yoyInflationLeg& yoyInflationLeg::withRateCurve(const Handle<YieldTermStructure>& rateCurve) {
177 rateCurve_ = rateCurve;
178 return *this;
179}
180
182 addInflationNotional_ = addInflationNotional;
183 return *this;
184}
185
186yoyInflationLeg::operator Leg() const {
187
188 Size n = schedule_.size() - 1;
189 QL_REQUIRE(!notionals_.empty(), "no notional given");
190 QL_REQUIRE(notionals_.size() <= n, "too many nominals (" << notionals_.size() << "), only " << n << " required");
191 QL_REQUIRE(gearings_.size() <= n, "too many gearings (" << gearings_.size() << "), only " << n << " required");
192 QL_REQUIRE(spreads_.size() <= n, "too many spreads (" << spreads_.size() << "), only " << n << " required");
193 QL_REQUIRE(caps_.size() <= n, "too many caps (" << caps_.size() << "), only " << n << " required");
194 QL_REQUIRE(floors_.size() <= n, "too many floors (" << floors_.size() << "), only " << n << " required");
195
196 Leg leg;
197 leg.reserve(n);
198
199 Calendar calendar = paymentCalendar_;
200
201 Date refStart, start, refEnd, end;
202
203 for (Size i = 0; i < n; ++i) {
204 refStart = start = schedule_.date(i);
205 refEnd = end = schedule_.date(i + 1);
206 Date paymentDate = calendar.adjust(end, paymentAdjustment_);
207 if (i == 0 && schedule_.hasIsRegular() && !schedule_.isRegular(i + 1)) {
208 BusinessDayConvention bdc = schedule_.businessDayConvention();
209 refStart = schedule_.calendar().adjust(end - schedule_.tenor(), bdc);
210 }
211 if (i == n - 1 && schedule_.hasIsRegular() && !schedule_.isRegular(i + 1)) {
212 BusinessDayConvention bdc = schedule_.businessDayConvention();
213 refEnd = schedule_.calendar().adjust(start + schedule_.tenor(), bdc);
214 }
215 if (detail::get(gearings_, i, 1.0) == 0.0) { // fixed coupon
216 leg.push_back(ext::shared_ptr<CashFlow>(new FixedRateCoupon(
217 paymentDate, detail::get(notionals_, i, 1.0), detail::effectiveFixedRate(spreads_, caps_, floors_, i),
218 paymentDayCounter_, start, end, refStart, refEnd)));
219 } else { // yoy inflation coupon
220 if (detail::noOption(caps_, floors_, i)) { // just swaplet
221 ext::shared_ptr<YoYInflationCoupon> coup(new YoYInflationCoupon(
222 paymentDate, detail::get(notionals_, i, 1.0), start, end, detail::get(fixingDays_, i, 0), index_,
223 observationLag_, paymentDayCounter_, detail::get(gearings_, i, 1.0), detail::get(spreads_, i, 0.0),
224 refStart, refEnd, addInflationNotional_));
225
226 // in this case you can set a pricer
227 // straight away because it only provides computation - not data
228 ext::shared_ptr<YoYInflationCouponPricer> pricer =
229 ext::make_shared<YoYInflationCouponPricer>(rateCurve_);
230 coup->setPricer(pricer);
231 leg.push_back(ext::dynamic_pointer_cast<CashFlow>(coup));
232
233 } else { // cap/floorlet
234 leg.push_back(ext::shared_ptr<CashFlow>(new CappedFlooredYoYInflationCoupon(
235 paymentDate, detail::get(notionals_, i, 1.0), start, end, detail::get(fixingDays_, i, 0), index_,
236 observationLag_, paymentDayCounter_, detail::get(gearings_, i, 1.0), detail::get(spreads_, i, 0.0),
237 detail::get(caps_, i, Null<Rate>()), detail::get(floors_, i, Null<Rate>()), refStart, refEnd,
238 addInflationNotional_)));
239 }
240 }
241 }
242
243 return leg;
244}
245
246} // namespace QuantExt
virtual void accept(AcyclicVisitor &) override
CappedFlooredYoYInflationCoupon(const ext::shared_ptr< YoYInflationCoupon > &underlying, Rate cap=Null< Rate >(), Rate floor=Null< Rate >(), bool addInflationNotional=false)
virtual void accept(AcyclicVisitor &) override
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(), bool addInflationNotional=false)
yoyInflationLeg & withRateCurve(const Handle< YieldTermStructure > &rateCurve)
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 &)
Handle< YieldTermStructure > rateCurve_
std::vector< Natural > fixingDays_
yoyInflationLeg & withCaps(Rate cap)
yoyInflationLeg & withInflationNotional(bool addInflationNotional_)
yoyInflationLeg & withFixingDays(Natural fixingDays)
SimpleQuote & spread_