QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
yearonyearinflationswap.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) 2007, 2009 Chris Kenyon
5 Copyright (C) 2009 StatPro Italia srl
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
29#include <ql/time/schedule.hpp>
30#include <utility>
31
32namespace QuantLib {
33
35 Real nominal,
36 Schedule fixedSchedule,
37 Rate fixedRate,
38 DayCounter fixedDayCount,
39 Schedule yoySchedule,
40 ext::shared_ptr<YoYInflationIndex> yoyIndex,
41 const Period& observationLag,
42 CPI::InterpolationType interpolation,
43 Spread spread,
44 DayCounter yoyDayCount,
45 Calendar paymentCalendar,
46 BusinessDayConvention paymentConvention)
47 : Swap(2), type_(type), nominal_(nominal), fixedSchedule_(std::move(fixedSchedule)),
48 fixedRate_(fixedRate), fixedDayCount_(std::move(fixedDayCount)),
49 yoySchedule_(std::move(yoySchedule)), yoyIndex_(std::move(yoyIndex)),
50 observationLag_(observationLag), spread_(spread), yoyDayCount_(std::move(yoyDayCount)),
51 paymentCalendar_(std::move(paymentCalendar)), paymentConvention_(paymentConvention) {
52 // N.B. fixed leg gets its calendar from the schedule!
55 .withCouponRates(fixedRate_, fixedDayCount_) // Simple compounding by default
57
59 observationLag_, interpolation)
64
65 Leg::const_iterator i;
66 for (i = yoyLeg.begin(); i < yoyLeg.end(); ++i)
67 registerWith(*i);
68
69 legs_[0] = fixedLeg;
70 legs_[1] = yoyLeg;
71 if (type_==Payer) {
72 payer_[0] = -1.0;
73 payer_[1] = +1.0;
74 } else {
75 payer_[0] = +1.0;
76 payer_[1] = -1.0;
77 }
78 }
79
81 Real nominal,
82 Schedule fixedSchedule,
83 Rate fixedRate,
84 DayCounter fixedDayCount,
85 Schedule yoySchedule,
86 ext::shared_ptr<YoYInflationIndex> yoyIndex,
87 const Period& observationLag,
88 Spread spread,
89 DayCounter yoyDayCount,
90 Calendar paymentCalendar,
91 BusinessDayConvention paymentConvention)
92 : YearOnYearInflationSwap(type, nominal, std::move(fixedSchedule), fixedRate, std::move(fixedDayCount),
93 std::move(yoySchedule), std::move(yoyIndex), observationLag, CPI::AsIndex,
94 spread, std::move(yoyDayCount), std::move(paymentCalendar), paymentConvention) {}
95
97
99
100 auto* arguments = dynamic_cast<YearOnYearInflationSwap::arguments*>(args);
101
102 if (arguments == nullptr) // it's a swap engine...
103 return;
104
107
108 const Leg& fixedCoupons = fixedLeg();
109
111 std::vector<Date>(fixedCoupons.size());
112 arguments->fixedCoupons = std::vector<Real>(fixedCoupons.size());
113
114 for (Size i=0; i<fixedCoupons.size(); ++i) {
115 ext::shared_ptr<FixedRateCoupon> coupon =
116 ext::dynamic_pointer_cast<FixedRateCoupon>(fixedCoupons[i]);
117
118 arguments->fixedPayDates[i] = coupon->date();
119 arguments->fixedResetDates[i] = coupon->accrualStartDate();
120 arguments->fixedCoupons[i] = coupon->amount();
121 }
122
123 const Leg& yoyCoupons = yoyLeg();
124
127 std::vector<Date>(yoyCoupons.size());
129 std::vector<Time>(yoyCoupons.size());
131 std::vector<Spread>(yoyCoupons.size());
132 arguments->yoyCoupons = std::vector<Real>(yoyCoupons.size());
133 for (Size i=0; i<yoyCoupons.size(); ++i) {
134 ext::shared_ptr<YoYInflationCoupon> coupon =
135 ext::dynamic_pointer_cast<YoYInflationCoupon>(yoyCoupons[i]);
136
137 arguments->yoyResetDates[i] = coupon->accrualStartDate();
138 arguments->yoyPayDates[i] = coupon->date();
139
140 arguments->yoyFixingDates[i] = coupon->fixingDate();
141 arguments->yoyAccrualTimes[i] = coupon->accrualPeriod();
142 arguments->yoySpreads[i] = coupon->spread();
143 try {
144 arguments->yoyCoupons[i] = coupon->amount();
145 } catch (Error&) {
147 }
148 }
149 }
150
151
153 calculate();
154 QL_REQUIRE(fairRate_ != Null<Rate>(), "result not available");
155 return fairRate_;
156 }
157
159 calculate();
160 QL_REQUIRE(fairSpread_ != Null<Spread>(), "result not available");
161 return fairSpread_;
162 }
163
164
166 calculate();
167 QL_REQUIRE(legNPV_[0] != Null<Real>(), "result not available");
168 return legNPV_[0];
169 }
170
172 calculate();
173 QL_REQUIRE(legNPV_[1] != Null<Real>(), "result not available");
174 return legNPV_[1];
175 }
176
179 legBPS_[0] = legBPS_[1] = 0.0;
182 }
183
185 static const Spread basisPoint = 1.0e-4;
186
187 // copy from VanillaSwap
188 // works because similarly simple instrument
189 // that we always expect to be priced with a swap engine
190
192
193 const auto* results = dynamic_cast<const YearOnYearInflationSwap::results*>(r);
194 if (results != nullptr) { // might be a swap engine, so no error is thrown
197 } else {
200 }
201
202 if (fairRate_ == Null<Rate>()) {
203 // calculate it from other results
204 if (legBPS_[0] != Null<Real>())
205 fairRate_ = fixedRate_ - NPV_/(legBPS_[0]/basisPoint);
206 }
207 if (fairSpread_ == Null<Spread>()) {
208 // ditto
209 if (legBPS_[1] != Null<Real>())
210 fairSpread_ = spread_ - NPV_/(legBPS_[1]/basisPoint);
211 }
212
213 }
214
217 QL_REQUIRE(nominal != Null<Real>(), "nominal null or not set");
219 "number of fixed start dates different from "
220 "number of fixed payment dates");
221 QL_REQUIRE(fixedPayDates.size() == fixedCoupons.size(),
222 "number of fixed payment dates different from "
223 "number of fixed coupon amounts");
224 QL_REQUIRE(yoyResetDates.size() == yoyPayDates.size(),
225 "number of yoy start dates different from "
226 "number of yoy payment dates");
227 QL_REQUIRE(yoyFixingDates.size() == yoyPayDates.size(),
228 "number of yoy fixing dates different from "
229 "number of yoy payment dates");
230 QL_REQUIRE(yoyAccrualTimes.size() == yoyPayDates.size(),
231 "number of yoy accrual Times different from "
232 "number of yoy payment dates");
233 QL_REQUIRE(yoySpreads.size() == yoyPayDates.size(),
234 "number of yoy spreads different from "
235 "number of yoy payment dates");
236 QL_REQUIRE(yoyPayDates.size() == yoyCoupons.size(),
237 "number of yoy payment dates different from "
238 "number of yoy coupon amounts");
239 }
240
245 }
246
247}
248
Cash-flow analysis functions.
Cash flow vector builders.
calendar class
Definition: calendar.hpp:61
day counter class
Definition: daycounter.hpp:44
Base error class.
Definition: errors.hpp:39
helper class building a sequence of fixed rate coupons
FixedRateLeg & withNotionals(Real)
FixedRateLeg & withPaymentAdjustment(BusinessDayConvention)
FixedRateLeg & withCouponRates(Rate, const DayCounter &paymentDayCounter, Compounding comp=Simple, Frequency freq=Annual)
void calculate() const override
Definition: instrument.hpp:129
template class providing a null value for a given type.
Definition: null.hpp:59
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:226
Payment schedule.
Definition: schedule.hpp:40
void validate() const override
Definition: swap.cpp:171
void reset() override
Definition: swap.cpp:176
Interest rate swap.
Definition: swap.hpp:41
void setupArguments(PricingEngine::arguments *) const override
Definition: swap.cpp:87
std::vector< Leg > legs_
Definition: swap.hpp:133
std::vector< Real > legNPV_
Definition: swap.hpp:135
std::vector< Real > legBPS_
Definition: swap.hpp:136
void setupExpired() const override
Definition: swap.cpp:78
void fetchResults(const PricingEngine::results *) const override
Definition: swap.cpp:95
std::vector< Real > payer_
Definition: swap.hpp:134
Year-on-year inflation-indexed swap.
void setupArguments(PricingEngine::arguments *args) const override
void fetchResults(const PricingEngine::results *) const override
ext::shared_ptr< YoYInflationIndex > yoyIndex_
YearOnYearInflationSwap(Type type, Real nominal, Schedule fixedSchedule, Rate fixedRate, DayCounter fixedDayCount, Schedule yoySchedule, ext::shared_ptr< YoYInflationIndex > yoyIndex, const Period &observationLag, CPI::InterpolationType interpolation, Spread spread, DayCounter yoyDayCount, Calendar paymentCalendar, BusinessDayConvention paymentConvention=ModifiedFollowing)
Helper class building a sequence of capped/floored yoy inflation coupons.
yoyInflationLeg & withSpreads(Spread spread)
yoyInflationLeg & withPaymentAdjustment(BusinessDayConvention)
yoyInflationLeg & withNotionals(Real notional)
yoyInflationLeg & withPaymentDayCounter(const DayCounter &)
Coupon pricers.
Calendar paymentCalendar_
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Coupon paying a fixed annual rate.
BusinessDayConvention
Business Day conventions.
QL_REAL Real
real number
Definition: types.hpp:50
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
base classes for inflation indexes
Definition: any.hpp:37
std::vector< ext::shared_ptr< CashFlow > > Leg
Sequence of cash-flows.
Definition: cashflow.hpp:78
STL namespace.
ext::shared_ptr< YieldTermStructure > r
date schedule
InterpolationType
when you observe an index, how do you interpolate between fixings?
Year-on-year inflation-indexed swap.
Interest-rate term structure.
Coupon paying a yoy inflation index.