QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
cpiswap.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2007, 2009, 2011 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
21#include <ql/cashflows/cashflows.hpp>
22#include <ql/cashflows/cashflowvectors.hpp>
23#include <ql/cashflows/couponpricer.hpp>
24#include <ql/cashflows/cpicoupon.hpp>
25#include <ql/cashflows/fixedratecoupon.hpp>
26#include <ql/cashflows/iborcoupon.hpp>
27#include <ql/cashflows/simplecashflow.hpp>
28#include <ql/indexes/inflationindex.hpp>
29#include <ql/instruments/cpiswap.hpp>
30#include <ql/termstructures/yieldtermstructure.hpp>
31#include <ql/time/schedule.hpp>
32#include <utility>
33
34namespace QuantLib {
35
36 // accrual adjustment is already in the schedules, as are calendars
38 Real nominal,
39 bool subtractInflationNominal,
40 // float + spread leg
41 Spread spread,
42 DayCounter floatDayCount,
43 Schedule floatSchedule,
44 const BusinessDayConvention& floatPaymentRoll,
45 Natural fixingDays,
46 ext::shared_ptr<IborIndex> floatIndex,
47 // fixed x inflation leg
48 Rate fixedRate,
49 Real baseCPI,
50 DayCounter fixedDayCount,
51 Schedule fixedSchedule,
52 const BusinessDayConvention& fixedPaymentRoll,
53 const Period& observationLag,
54 ext::shared_ptr<ZeroInflationIndex> fixedIndex,
55 CPI::InterpolationType observationInterpolation,
56 Real inflationNominal)
57 : Swap(2), type_(type), nominal_(nominal), subtractInflationNominal_(subtractInflationNominal),
58 spread_(spread), floatDayCount_(std::move(floatDayCount)),
59 floatSchedule_(std::move(floatSchedule)), floatPaymentRoll_(floatPaymentRoll),
60 fixingDays_(fixingDays), floatIndex_(std::move(floatIndex)), fixedRate_(fixedRate),
61 baseCPI_(baseCPI), fixedDayCount_(std::move(fixedDayCount)),
62 fixedSchedule_(std::move(fixedSchedule)), fixedPaymentRoll_(fixedPaymentRoll),
63 fixedIndex_(std::move(fixedIndex)), observationLag_(observationLag),
64 observationInterpolation_(observationInterpolation) {
65 QL_REQUIRE(!floatSchedule_.empty(), "empty float schedule");
66 QL_REQUIRE(!fixedSchedule_.empty(), "empty fixed schedule");
67 // \todo if roll!=unadjusted then need calendars ...
68
71
72 Leg floatingLeg;
73 if (floatSchedule_.size() > 1) {
74 floatingLeg = IborLeg(floatSchedule_, floatIndex_)
80 }
81
82 if (floatSchedule_.size()==1 ||
85 )
86 {
87 Date payNotional;
88 if (floatSchedule_.size()==1) { // no coupons
89 payNotional = floatSchedule_[0];
90 payNotional = floatSchedule_.calendar().adjust(payNotional, floatPaymentRoll_);
91 } else { // use the pay date of the last coupon
92 payNotional = floatingLeg.back()->date();
93 }
94
96 ext::shared_ptr<CashFlow> nf(new SimpleCashFlow(floatAmount, payNotional));
97 floatingLeg.push_back(nf);
98 }
99
100 // a CPIleg know about zero legs and inclusion of base inflation notional
109
110
111 Leg::const_iterator i;
112 for (i = cpiLeg.begin(); i < cpiLeg.end(); ++i) {
113 registerWith(*i);
114 }
115
116 for (i = floatingLeg.begin(); i < floatingLeg.end(); ++i) {
117 registerWith(*i);
118 }
119
120 legs_[0] = cpiLeg;
121 legs_[1] = floatingLeg;
122
123 if (type_==Payer) {
124 payer_[0] = 1.0;
125 payer_[1] = -1.0;
126 } else {
127 payer_[0] = -1.0;
128 payer_[1] = 1.0;
129 }
130 }
131
132
135
137
138 auto* arguments = dynamic_cast<CPISwap::arguments*>(args);
139
140 if (arguments == nullptr)
141 return; // it's a swap engine...
142 }
143
144
146 calculate();
147 QL_REQUIRE(fairRate_ != Null<Rate>(), "result not available");
148 return fairRate_;
149 }
150
152 calculate();
153 QL_REQUIRE(fairSpread_ != Null<Spread>(), "result not available");
154 return fairSpread_;
155 }
156
157
158 Real CPISwap::fixedLegNPV() const {//FIXME
159 calculate();
160 QL_REQUIRE(legNPV_[0] != Null<Real>(), "result not available");
161 return legNPV_[0];
162 }
163
164 Real CPISwap::floatLegNPV() const {//FIXME
165 calculate();
166 QL_REQUIRE(legNPV_[1] != Null<Real>(), "result not available");
167 return legNPV_[1];
168 }
169
172 legBPS_[0] = legBPS_[1] = 0.0;
175 }
176
178 static const Spread basisPoint = 1.0e-4;
179
180 // copy from VanillaSwap
181 // works because similarly simple instrument
182 // that we always expect to be priced with a swap engine
183
185
186 const auto* results = dynamic_cast<const CPISwap::results*>(r);
187 if (results != nullptr) { // might be a swap engine, so no error is thrown
190 } else {
193 }
194
195 if (fairRate_ == Null<Rate>()) {
196 // calculate it from other results
197 if (legBPS_[0] != Null<Real>())
198 fairRate_ = fixedRate_ - NPV_/(legBPS_[0]/basisPoint);
199 }
200 if (fairSpread_ == Null<Spread>()) {
201 // ditto
202 if (legBPS_[1] != Null<Real>())
203 fairSpread_ = spread_ - NPV_/(legBPS_[1]/basisPoint);
204 }
205
206 }
207
210 }
211
216 }
217
218}
219
Helper class building a sequence of capped/floored CPI coupons.
Definition: cpicoupon.hpp:277
CPILeg & withNotionals(Real notional)
Definition: cpicoupon.cpp:285
CPILeg & withPaymentAdjustment(BusinessDayConvention)
Definition: cpicoupon.cpp:305
CPILeg & withSubtractInflationNominal(bool)
Definition: cpicoupon.cpp:295
CPILeg & withFixedRates(Real fixedRate)
Definition: cpicoupon.cpp:275
CPILeg & withPaymentDayCounter(const DayCounter &)
Definition: cpicoupon.cpp:300
CPILeg & withObservationInterpolation(CPI::InterpolationType)
Definition: cpicoupon.cpp:269
Arguments for swap calculation
Definition: cpiswap.hpp:167
void validate() const override
Definition: cpiswap.cpp:208
Results from swap calculation
Definition: cpiswap.hpp:177
void reset() override
Definition: cpiswap.cpp:212
ext::shared_ptr< IborIndex > floatIndex_
Definition: cpiswap.hpp:147
Real inflationNominal_
Definition: cpiswap.hpp:158
BusinessDayConvention fixedPaymentRoll_
Definition: cpiswap.hpp:154
Period observationLag_
Definition: cpiswap.hpp:156
virtual Rate fairRate() const
Definition: cpiswap.cpp:145
BusinessDayConvention floatPaymentRoll_
Definition: cpiswap.hpp:145
ext::shared_ptr< ZeroInflationIndex > fixedIndex_
Definition: cpiswap.hpp:155
DayCounter fixedDayCount_
Definition: cpiswap.hpp:152
CPISwap(Type type, Real nominal, bool subtractInflationNominal, Spread spread, DayCounter floatDayCount, Schedule floatSchedule, const BusinessDayConvention &floatRoll, Natural fixingDays, ext::shared_ptr< IborIndex > floatIndex, Rate fixedRate, Real baseCPI, DayCounter fixedDayCount, Schedule fixedSchedule, const BusinessDayConvention &fixedRoll, const Period &observationLag, ext::shared_ptr< ZeroInflationIndex > fixedIndex, CPI::InterpolationType observationInterpolation=CPI::AsIndex, Real inflationNominal=Null< Real >())
Definition: cpiswap.cpp:37
Spread fairSpread_
Definition: cpiswap.hpp:160
DayCounter floatDayCount_
Definition: cpiswap.hpp:143
virtual Spread fairSpread() const
Definition: cpiswap.cpp:151
virtual Real floatLegNPV() const
Definition: cpiswap.cpp:164
virtual Real inflationNominal() const
Definition: cpiswap.hpp:212
Natural fixingDays_
Definition: cpiswap.hpp:146
Schedule fixedSchedule_
Definition: cpiswap.hpp:153
virtual const Leg & cpiLeg() const
Definition: cpiswap.hpp:214
void setupArguments(PricingEngine::arguments *args) const override
for simple case sufficient to copy base class
Definition: cpiswap.cpp:134
CPI::InterpolationType observationInterpolation_
Definition: cpiswap.hpp:157
void setupExpired() const override
Definition: cpiswap.cpp:170
void fetchResults(const PricingEngine::results *) const override
Definition: cpiswap.cpp:177
virtual bool subtractInflationNominal() const
Definition: cpiswap.hpp:193
Schedule floatSchedule_
Definition: cpiswap.hpp:144
bool subtractInflationNominal_
Definition: cpiswap.hpp:139
virtual Real fixedLegNPV() const
Definition: cpiswap.cpp:158
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
helper class building a sequence of capped/floored ibor-rate coupons
Definition: iborcoupon.hpp:133
IborLeg & withSpreads(Spread spread)
Definition: iborcoupon.cpp:208
IborLeg & withPaymentAdjustment(BusinessDayConvention)
Definition: iborcoupon.cpp:173
IborLeg & withPaymentDayCounter(const DayCounter &)
Definition: iborcoupon.cpp:168
IborLeg & withNotionals(Real notional)
Definition: iborcoupon.cpp:158
IborLeg & withFixingDays(Natural fixingDays)
Definition: iborcoupon.cpp:188
void calculate() const override
Definition: instrument.hpp:129
template class providing a null value for a given type.
Definition: null.hpp:76
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Payment schedule.
Definition: schedule.hpp:40
const Calendar & calendar() const
Definition: schedule.hpp:176
bool empty() const
Definition: schedule.hpp:82
Size size() const
Definition: schedule.hpp:69
Predetermined cash flow.
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
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
Definition: any.hpp:35
std::vector< ext::shared_ptr< CashFlow > > Leg
Sequence of cash-flows.
Definition: cashflow.hpp:78
STL namespace.
InterpolationType
when you observe an index, how do you interpolate between fixings?