QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
inflationhelpers.cpp
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
21#include <ql/cashflows/inflationcouponpricer.hpp>
22#include <ql/indexes/inflationindex.hpp>
23#include <ql/pricingengines/swap/discountingswapengine.hpp>
24#include <ql/termstructures/inflation/inflationhelpers.hpp>
25#include <ql/utilities/null_deleter.hpp>
26#include <utility>
27
28namespace QuantLib {
29
31 const Handle<Quote>& quote,
32 const Period& swapObsLag,
33 const Date& maturity,
34 Calendar calendar,
35 BusinessDayConvention paymentConvention,
36 DayCounter dayCounter,
37 ext::shared_ptr<ZeroInflationIndex> zii,
38 CPI::InterpolationType observationInterpolation,
39 Handle<YieldTermStructure> nominalTermStructure)
40 : BootstrapHelper<ZeroInflationTermStructure>(quote), swapObsLag_(swapObsLag),
41 maturity_(maturity), calendar_(std::move(calendar)), paymentConvention_(paymentConvention),
42 dayCounter_(std::move(dayCounter)), zii_(std::move(zii)),
43 observationInterpolation_(observationInterpolation),
44 nominalTermStructure_(std::move(nominalTermStructure)) {
45
46 std::pair<Date, Date> limStart = inflationPeriod(maturity_ - swapObsLag_, zii_->frequency());
47 std::pair<Date, Date> interpolationPeriod = inflationPeriod(maturity, zii_->frequency());
48
50 (maturity > interpolationPeriod.first)) {
51 // if interpolated, we need to cover the end of the interpolation period
52 earliestDate_ = limStart.first;
53 latestDate_ = limStart.second + 1;
54 } else {
55 // if not interpolated, the date of the initial fixing is enough
56 earliestDate_ = limStart.first;
57 latestDate_ = limStart.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_.reset(new 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 Handle<YieldTermStructure> nominalTermStructure)
120 : BootstrapHelper<YoYInflationTermStructure>(quote), swapObsLag_(swapObsLag),
121 maturity_(maturity), calendar_(std::move(calendar)), paymentConvention_(paymentConvention),
122 dayCounter_(std::move(dayCounter)), yii_(std::move(yii)),
123 nominalTermStructure_(std::move(nominalTermStructure)) {
124
125 if (yii_->interpolated()) {
126 // if interpolated then simple
129 } else {
130 // but if NOT interpolated then the value is valid
131 // for every day in an inflation period so you actually
132 // get an extended validity, however for curve building
133 // just put the first date because using that convention
134 // for the base date throughout
135 std::pair<Date, Date> limStart =
136 inflationPeriod(maturity_ - swapObsLag_, yii_->frequency());
137 earliestDate_ = limStart.first;
138 latestDate_ = limStart.first;
139 }
140
141 // check that the observation lag of the swap
142 // is compatible with the availability lag of the index AND
143 // it's interpolation (assuming the start day is spot)
144 if (yii_->interpolated()) {
145 Period pShift(yii_->frequency());
146 QL_REQUIRE(swapObsLag_ - pShift >= yii_->availabilityLag(),
147 "inconsistency between swap observation lag "
148 << swapObsLag_ << ", index period " << pShift << " and index availability "
149 << yii_->availabilityLag() << ": need (obsLag-index period) >= availLag");
150 }
151
152 registerWith(Settings::instance().evaluationDate());
154 }
155
156
158 yyiis_->deepUpdate();
159 return yyiis_->fairRate();
160 }
161
162
164
166
167 // set up a new YYIIS
168 // but this one does NOT own its inflation term structure
169 const bool own = false;
170
171 // The effect of the new inflation term structure is
172 // felt via the effect on the inflation index
174 ext::shared_ptr<YoYInflationTermStructure>(y, null_deleter()), own);
175
176 ext::shared_ptr<YoYInflationIndex> new_yii = yii_->clone(yyts);
177
178 // always works because tenor is always 1 year so
179 // no problem with different days-in-month
181 Date to = maturity_;
182 Schedule fixedSchedule = MakeSchedule()
183 .from(from)
184 .to(to)
185 .withTenor(1 * Years)
187 .withCalendar(calendar_) // fixed leg gets cal from sched
188 .backwards();
189 const Schedule& yoySchedule = fixedSchedule;
190 Spread spread = 0.0;
191 Rate fixedRate = quote()->value();
192
193 Real nominal = 1000000.0; // has to be something but doesn't matter what
195 Swap::Payer, nominal, fixedSchedule, fixedRate, dayCounter_,
196 yoySchedule, new_yii, swapObsLag_, spread, dayCounter_,
197 calendar_, // inflation index does not have a calendar
199
200 // The instrument takes a standard discounting swap engine.
201 // The inflation-related work is done by the coupons.
202
203 yyiis_->setPricingEngine(
204 ext::shared_ptr<PricingEngine>(new DiscountingSwapEngine(nominalTermStructure_)));
205 }
206
207}
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
Shared handle to an observable.
Definition: handle.hpp:41
MakeSchedule & withConvention(BusinessDayConvention)
Definition: schedule.cpp:557
MakeSchedule & backwards()
Definition: schedule.cpp:578
MakeSchedule & to(const Date &terminationDate)
Definition: schedule.cpp:537
MakeSchedule & withTenor(const Period &)
Definition: schedule.cpp:542
MakeSchedule & from(const Date &effectiveDate)
Definition: schedule.cpp:532
MakeSchedule & withCalendar(const Calendar &)
Definition: schedule.cpp:552
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
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
YearOnYearInflationSwapHelper(const Handle< Quote > &quote, const Period &swapObsLag_, const Date &maturity, Calendar calendar, BusinessDayConvention paymentConvention, DayCounter dayCounter, ext::shared_ptr< YoYInflationIndex > yii, Handle< YieldTermStructure > nominalTermStructure)
void setTermStructure(YoYInflationTermStructure *) override
ext::shared_ptr< YearOnYearInflationSwap > yyiis_
ext::shared_ptr< YoYInflationIndex > yii_
Handle< YieldTermStructure > nominalTermStructure_
Year-on-year inflation-indexed swap.
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_
Zero-coupon inflation-indexed swap.
Interface for zero inflation term structures.
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
QuantLib::CPI::InterpolationType effectiveInterpolationType(const ext::shared_ptr< ZeroInflationIndex > &index, const QuantLib::CPI::InterpolationType &type=QuantLib::CPI::AsIndex)
Definition: any.hpp:35
std::pair< Date, Date > inflationPeriod(const Date &d, Frequency frequency)
utility function giving the inflation period for a given date
STL namespace.
InterpolationType
when you observe an index, how do you interpolate between fixings?
@ Linear
linearly between bracketing fixings