QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
inflationhelpers.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) 2007 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
24#include <ql/shared_ptr.hpp>
27#include <utility>
28
29namespace QuantLib {
30
32 const Handle<Quote>& quote,
33 const Period& swapObsLag,
34 const Date& maturity,
35 Calendar calendar,
36 BusinessDayConvention paymentConvention,
37 DayCounter dayCounter,
38 ext::shared_ptr<ZeroInflationIndex> zii,
39 CPI::InterpolationType observationInterpolation,
40 Handle<YieldTermStructure> nominalTermStructure)
41 : BootstrapHelper<ZeroInflationTermStructure>(quote), swapObsLag_(swapObsLag),
42 maturity_(maturity), calendar_(std::move(calendar)), paymentConvention_(paymentConvention),
43 dayCounter_(std::move(dayCounter)), zii_(std::move(zii)),
44 observationInterpolation_(observationInterpolation),
45 nominalTermStructure_(std::move(nominalTermStructure)) {
46
47 auto fixingPeriod = inflationPeriod(maturity_ - swapObsLag_, zii_->frequency());
48 auto interpolationPeriod = inflationPeriod(maturity, zii_->frequency());
49
50 if (detail::CPI::isInterpolated(observationInterpolation_) && maturity > interpolationPeriod.first) {
51 // if interpolated, we need to cover the end of the interpolation period
52 earliestDate_ = fixingPeriod.first;
53 latestDate_ = fixingPeriod.second + 1;
54 } else {
55 // if not interpolated, the date of the initial fixing is enough
56 earliestDate_ = fixingPeriod.first;
57 latestDate_ = fixingPeriod.first;
58 }
59
60 // check that the observation lag of the swap
61 // is compatible with the availability lag of the index AND
62 // it's interpolation (assuming the start day is spot)
64 Period pShift(zii_->frequency());
65 QL_REQUIRE(swapObsLag_ - pShift >= zii_->availabilityLag(),
66 "inconsistency between swap observation lag "
67 << swapObsLag_ << ", index period " << pShift << " and index availability "
68 << zii_->availabilityLag() << ": need (obsLag-index period) >= availLag");
69 }
70
71 registerWith(Settings::instance().evaluationDate());
73 }
74
75
77 zciis_->deepUpdate();
78 return zciis_->fairRate();
79 }
80
81
83
85
86 // set up a new ZCIIS
87 // but this one does NOT own its inflation term structure
88 const bool own = false;
89 Rate K = quote()->value();
90
91 // The effect of the new inflation term structure is
92 // felt via the effect on the inflation index
94 ext::shared_ptr<ZeroInflationTermStructure>(z, null_deleter()), own);
95
96 ext::shared_ptr<ZeroInflationIndex> new_zii = zii_->clone(zits);
97
98 Real nominal = 1000000.0; // has to be something but doesn't matter what
99 Date start = nominalTermStructure_->referenceDate();
100 zciis_ = ext::make_shared<ZeroCouponInflationSwap>(Swap::Payer, nominal, start,
102 dayCounter_, K, // fixed side & fixed rate
104 // Because very simple instrument only takes
105 // standard discounting swap engine.
106 zciis_->setPricingEngine(
107 ext::shared_ptr<PricingEngine>(new DiscountingSwapEngine(nominalTermStructure_)));
108 }
109
110
112 const Handle<Quote>& quote,
113 const Period& swapObsLag,
114 const Date& maturity,
115 Calendar calendar,
116 BusinessDayConvention paymentConvention,
117 DayCounter dayCounter,
118 ext::shared_ptr<YoYInflationIndex> yii,
119 CPI::InterpolationType interpolation,
120 Handle<YieldTermStructure> nominalTermStructure)
121 : BootstrapHelper<YoYInflationTermStructure>(quote), swapObsLag_(swapObsLag),
122 maturity_(maturity), calendar_(std::move(calendar)), paymentConvention_(paymentConvention),
123 dayCounter_(std::move(dayCounter)), yii_(std::move(yii)), interpolation_(interpolation),
124 nominalTermStructure_(std::move(nominalTermStructure)) {
125
126 auto fixingPeriod = inflationPeriod(maturity_ - swapObsLag_, yii_->frequency());
127 auto interpolationPeriod = inflationPeriod(maturity, yii_->frequency());
128
129 if (detail::CPI::isInterpolated(interpolation_, yii_) && maturity > interpolationPeriod.first) {
130 // if interpolated, we need to cover the end of the interpolation period
131 earliestDate_ = fixingPeriod.first;
132 latestDate_ = fixingPeriod.second + 1;
133 } else {
134 // if not interpolated, the date of the initial fixing is enough
135 earliestDate_ = fixingPeriod.first;
136 latestDate_ = fixingPeriod.first;
137 }
138
139 // check that the observation lag of the swap
140 // is compatible with the availability lag of the index AND
141 // its interpolation (assuming the start day is spot)
143 Period pShift(yii_->frequency());
144 QL_REQUIRE(swapObsLag_ - pShift >= yii_->availabilityLag(),
145 "inconsistency between swap observation lag "
146 << swapObsLag_ << ", index period " << pShift << " and index availability "
147 << yii_->availabilityLag() << ": need (obsLag-index period) >= availLag");
148 }
149
150 registerWith(Settings::instance().evaluationDate());
152 }
153
155 const Handle<Quote>& quote,
156 const Period& swapObsLag,
157 const Date& maturity,
158 Calendar calendar,
159 BusinessDayConvention paymentConvention,
160 DayCounter dayCounter,
161 ext::shared_ptr<YoYInflationIndex> yii,
162 Handle<YieldTermStructure> nominalTermStructure)
163 : YearOnYearInflationSwapHelper(quote, swapObsLag, maturity, std::move(calendar), paymentConvention,
164 std::move(dayCounter), std::move(yii), CPI::AsIndex, std::move(nominalTermStructure)) {}
165
166
168 yyiis_->deepUpdate();
169 return yyiis_->fairRate();
170 }
171
172
174
176
177 // set up a new YYIIS
178 // but this one does NOT own its inflation term structure
179 const bool own = false;
180
181 // The effect of the new inflation term structure is
182 // felt via the effect on the inflation index
184 ext::shared_ptr<YoYInflationTermStructure>(y, null_deleter()), own);
185
186 ext::shared_ptr<YoYInflationIndex> new_yii = yii_->clone(yyts);
187
188 // always works because tenor is always 1 year so
189 // no problem with different days-in-month
191 Date to = maturity_;
192 Schedule fixedSchedule = MakeSchedule()
193 .from(from)
194 .to(to)
195 .withTenor(1 * Years)
197 .withCalendar(calendar_) // fixed leg gets cal from sched
198 .backwards();
199 const Schedule& yoySchedule = fixedSchedule;
200 Spread spread = 0.0;
201 Rate fixedRate = quote()->value();
202
203 Real nominal = 1000000.0; // has to be something but doesn't matter what
204 yyiis_ = ext::make_shared<YearOnYearInflationSwap>(
205 Swap::Payer, nominal, fixedSchedule, fixedRate, dayCounter_,
206 yoySchedule, new_yii, swapObsLag_, interpolation_,
208
209 // The instrument takes a standard discounting swap engine.
210 // The inflation-related work is done by the coupons.
211
212 yyiis_->setPricingEngine(
213 ext::shared_ptr<PricingEngine>(new DiscountingSwapEngine(nominalTermStructure_)));
214 }
215
216}
Base helper class for bootstrapping.
virtual void setTermStructure(TS *)
sets the term structure to be used for pricing
calendar class
Definition: calendar.hpp:61
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Discounting engine for swaps.
Shared handle to an observable.
Definition: handle.hpp:41
MakeSchedule & withConvention(BusinessDayConvention)
Definition: schedule.cpp:552
MakeSchedule & backwards()
Definition: schedule.cpp:573
MakeSchedule & to(const Date &terminationDate)
Definition: schedule.cpp:532
MakeSchedule & withTenor(const Period &)
Definition: schedule.cpp:537
MakeSchedule & from(const Date &effectiveDate)
Definition: schedule.cpp:527
MakeSchedule & withCalendar(const Calendar &)
Definition: schedule.cpp:547
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:226
Payment schedule.
Definition: schedule.hpp:40
DateProxy & evaluationDate()
the date at which pricing is to be performed.
Definition: settings.hpp:147
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
Year-on-year inflation-swap bootstrap helper.
void setTermStructure(YoYInflationTermStructure *) override
ext::shared_ptr< YearOnYearInflationSwap > yyiis_
YearOnYearInflationSwapHelper(const Handle< Quote > &quote, const Period &swapObsLag_, const Date &maturity, Calendar calendar, BusinessDayConvention paymentConvention, DayCounter dayCounter, ext::shared_ptr< YoYInflationIndex > yii, CPI::InterpolationType interpolation, Handle< YieldTermStructure > nominalTermStructure)
ext::shared_ptr< YoYInflationIndex > yii_
Handle< YieldTermStructure > nominalTermStructure_
Base class for year-on-year inflation term structures.
Handle< YieldTermStructure > nominalTermStructure_
ZeroCouponInflationSwapHelper(const Handle< Quote > &quote, const Period &swapObsLag, const Date &maturity, Calendar calendar, BusinessDayConvention paymentConvention, DayCounter dayCounter, ext::shared_ptr< ZeroInflationIndex > zii, CPI::InterpolationType observationInterpolation, Handle< YieldTermStructure > nominalTermStructure)
void setTermStructure(ZeroInflationTermStructure *) override
CPI::InterpolationType observationInterpolation_
ext::shared_ptr< ZeroInflationIndex > zii_
ext::shared_ptr< ZeroCouponInflationSwap > zciis_
Interface for zero inflation term structures.
discounting swap engine
#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
Real Spread
spreads on interest rates
Definition: types.hpp:74
Real Rate
interest rates
Definition: types.hpp:70
inflation-coupon pricers
Bootstrap helpers for inflation term structures.
base classes for inflation indexes
bool isInterpolated(const QuantLib::CPI::InterpolationType &type)
Definition: any.hpp:37
std::pair< Date, Date > inflationPeriod(const Date &d, Frequency frequency)
utility function giving the inflation period for a given date
STL namespace.
empty deleter for shared_ptr
Maps shared_ptr to either the boost or std implementation.
InterpolationType
when you observe an index, how do you interpolate between fixings?