QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
inflationcapfloor.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2009 Chris Kenyon
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18 */
19
20#include <ql/cashflows/cashflows.hpp>
21#include <ql/instruments/inflationcapfloor.hpp>
22#include <ql/math/solvers1d/newtonsafe.hpp>
23#include <ql/quotes/simplequote.hpp>
24#include <ql/utilities/dataformatters.hpp>
25#include <utility>
26
27namespace QuantLib {
28
29
30 std::ostream& operator<<(std::ostream& out,
32 switch (t) {
34 return out << "YoYInflationCap";
36 return out << "YoYInflationFloor";
38 return out << "YoYInflationCollar";
39 default:
40 QL_FAIL("unknown YoYInflationCapFloor::Type (" << Integer(t) << ")");
41 }
42 }
43
45 Leg yoyLeg,
46 std::vector<Rate> capRates,
47 std::vector<Rate> floorRates)
48 : type_(type), yoyLeg_(std::move(yoyLeg)), capRates_(std::move(capRates)),
49 floorRates_(std::move(floorRates)) {
50 if (type_ == Cap || type_ == Collar) {
51 QL_REQUIRE(!capRates_.empty(), "no cap rates given");
52 capRates_.reserve(yoyLeg_.size());
53 while (capRates_.size() < yoyLeg_.size())
54 capRates_.push_back(capRates_.back());
55 }
56 if (type_ == Floor || type_ == Collar) {
57 QL_REQUIRE(!floorRates_.empty(), "no floor rates given");
58 floorRates_.reserve(yoyLeg_.size());
59 while (floorRates_.size() < yoyLeg_.size())
60 floorRates_.push_back(floorRates_.back());
61 }
62 Leg::const_iterator i;
63 for (i = yoyLeg_.begin(); i != yoyLeg_.end(); ++i)
64 registerWith(*i);
65
66 registerWith(Settings::instance().evaluationDate());
67 }
68
70 Leg yoyLeg,
71 const std::vector<Rate>& strikes)
72 : type_(type), yoyLeg_(std::move(yoyLeg)) {
73 QL_REQUIRE(!strikes.empty(), "no strikes given");
74 if (type_ == Cap) {
75 capRates_ = strikes;
76 capRates_.reserve(yoyLeg_.size());
77 while (capRates_.size() < yoyLeg_.size())
78 capRates_.push_back(capRates_.back());
79 } else if (type_ == Floor) {
80 floorRates_ = strikes;
81 floorRates_.reserve(yoyLeg_.size());
82 while (floorRates_.size() < yoyLeg_.size())
83 floorRates_.push_back(floorRates_.back());
84 } else
85 QL_FAIL("only Cap/Floor types allowed in this constructor");
86
87 Leg::const_iterator i;
88 for (i = yoyLeg_.begin(); i != yoyLeg_.end(); ++i)
89 registerWith(*i);
90
91 registerWith(Settings::instance().evaluationDate());
92 }
93
95 for (Size i=yoyLeg_.size(); i>0; --i)
96 if (!yoyLeg_[i-1]->hasOccurred())
97 return false;
98 return true;
99 }
100
103 }
104
107 }
108
109 ext::shared_ptr<YoYInflationCoupon>
111 ext::shared_ptr<CashFlow> lastCF(yoyLeg_.back());
112 ext::shared_ptr<YoYInflationCoupon> lastYoYInflationCoupon =
113 ext::dynamic_pointer_cast<YoYInflationCoupon>(lastCF);
115 }
116
117 ext::shared_ptr<YoYInflationCapFloor> YoYInflationCapFloor::optionlet(const Size i) const {
118 QL_REQUIRE(i < yoyLeg().size(),
119 io::ordinal(i+1) << " optionlet does not exist, only " <<
120 yoyLeg().size());
121 Leg cf(1, yoyLeg()[i]);
122
123 std::vector<Rate> cap, floor;
124 if (type() == Cap || type() == Collar)
125 cap.push_back(capRates()[i]);
126 if (type() == Floor || type() == Collar)
127 floor.push_back(floorRates()[i]);
128
129 return ext::make_shared<YoYInflationCapFloor>(type(),
130 cf, cap, floor);
131 }
132
134 auto* arguments = dynamic_cast<YoYInflationCapFloor::arguments*>(args);
135 QL_REQUIRE(arguments != nullptr, "wrong argument type");
136
137 Size n = yoyLeg_.size();
138
139 arguments->startDates.resize(n);
140 arguments->fixingDates.resize(n);
141 arguments->payDates.resize(n);
142 arguments->accrualTimes.resize(n);
143 arguments->nominals.resize(n);
144 arguments->gearings.resize(n);
145 arguments->capRates.resize(n);
146 arguments->floorRates.resize(n);
147 arguments->spreads.resize(n);
148
150
151 for (Size i=0; i<n; ++i) {
152 ext::shared_ptr<YoYInflationCoupon> coupon =
153 ext::dynamic_pointer_cast<YoYInflationCoupon>(
154 yoyLeg_[i]);
155 QL_REQUIRE(coupon, "non-YoYInflationCoupon given");
156 arguments->startDates[i] = coupon->accrualStartDate();
157 arguments->fixingDates[i] = coupon->fixingDate();
158 arguments->payDates[i] = coupon->date();
159
160 // this is passed explicitly for precision
161 arguments->accrualTimes[i] = coupon->accrualPeriod();
162
163 arguments->nominals[i] = coupon->nominal();
164 Spread spread = coupon->spread();
165 Real gearing = coupon->gearing();
166 arguments->gearings[i] = gearing;
167 arguments->spreads[i] = spread;
168
169 if (type_ == Cap || type_ == Collar)
170 arguments->capRates[i] = (capRates_[i]-spread)/gearing;
171 else
173
174 if (type_ == Floor || type_ == Collar)
175 arguments->floorRates[i] = (floorRates_[i]-spread)/gearing;
176 else
178 }
179 }
180
182 QL_REQUIRE(payDates.size() == startDates.size(),
183 "number of start dates (" << startDates.size()
184 << ") different from that of pay dates ("
185 << payDates.size() << ")");
186 QL_REQUIRE(accrualTimes.size() == startDates.size(),
187 "number of start dates (" << startDates.size()
188 << ") different from that of accrual times ("
189 << accrualTimes.size() << ")");
190 QL_REQUIRE(type == YoYInflationCapFloor::Floor ||
191 capRates.size() == startDates.size(),
192 "number of start dates (" << startDates.size()
193 << ") different from that of cap rates ("
194 << capRates.size() << ")");
195 QL_REQUIRE(type == YoYInflationCapFloor::Cap ||
196 floorRates.size() == startDates.size(),
197 "number of start dates (" << startDates.size()
198 << ") different from that of floor rates ("
199 << floorRates.size() << ")");
200 QL_REQUIRE(gearings.size() == startDates.size(),
201 "number of start dates (" << startDates.size()
202 << ") different from that of gearings ("
203 << gearings.size() << ")");
204 QL_REQUIRE(spreads.size() == startDates.size(),
205 "number of start dates (" << startDates.size()
206 << ") different from that of spreads ("
207 << spreads.size() << ")");
208 QL_REQUIRE(nominals.size() == startDates.size(),
209 "number of start dates (" << startDates.size()
210 << ") different from that of nominals ("
211 << nominals.size() << ")");
212 }
213
215 return CashFlows::atmRate(yoyLeg_, discountCurve,
216 false, discountCurve.referenceDate());
217 }
218
219
220}
Concrete cap class.
Definition: capfloor.hpp:108
static Date maturityDate(const Leg &leg)
Definition: cashflows.cpp:52
static Date startDate(const Leg &leg)
Definition: cashflows.cpp:38
static Rate atmRate(const Leg &leg, const YieldTermStructure &discountCurve, bool includeSettlementDateFlows, Date settlementDate=Date(), Date npvDate=Date(), Real npv=Null< Real >())
At-the-money rate of the cash flows.
Definition: cashflows.cpp:521
Concrete collar class.
Definition: capfloor.hpp:128
Concrete date class.
Definition: date.hpp:125
Concrete floor class.
Definition: capfloor.hpp:118
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
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
virtual const Date & referenceDate() const
the date at which discount = 1.0 and/or variance = 0.0
Interest-rate term structure.
Arguments for YoY Inflation cap/floor calculation
void setupArguments(PricingEngine::arguments *) const override
bool isExpired() const override
returns whether the instrument might have value greater than zero.
const std::vector< Rate > & capRates() const
ext::shared_ptr< YoYInflationCoupon > lastYoYInflationCoupon() const
virtual Rate atmRate(const YieldTermStructure &discountCurve) const
const std::vector< Rate > & floorRates() const
YoYInflationCapFloor(Type type, Leg yoyLeg, std::vector< Rate > capRates, std::vector< Rate > floorRates)
ext::shared_ptr< YoYInflationCapFloor > optionlet(Size n) const
Returns the n-th optionlet as a cap/floor with only one cash flow.
detail::ordinal_holder ordinal(Size)
outputs naturals as 1st, 2nd, 3rd...
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
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
Definition: any.hpp:35
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
std::vector< ext::shared_ptr< CashFlow > > Leg
Sequence of cash-flows.
Definition: cashflow.hpp:78
STL namespace.