QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
inflationcapfloorengines.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
21#include <ql/pricingengines/blackformula.hpp>
22#include <ql/pricingengines/inflation/inflationcapfloorengines.hpp>
23#include <ql/termstructures/volatility/inflation/yoyinflationoptionletvolatilitystructure.hpp>
24#include <utility>
25
26namespace QuantLib {
27
28
30 ext::shared_ptr<YoYInflationIndex> index,
32 Handle<YieldTermStructure> nominalTermStructure)
33 : index_(std::move(index)), volatility_(std::move(volatility)),
34 nominalTermStructure_(std::move(nominalTermStructure)) {
38 }
39
40
43 if (!volatility_.empty())
45 volatility_ = v;
47 update();
48 }
49
50
52
53 // copy black version then adapt to others
54
55 Real value = 0.0;
56 Size optionlets = arguments_.startDates.size();
57 std::vector<Real> values(optionlets, 0.0);
58 std::vector<Real> stdDevs(optionlets, 0.0);
59 std::vector<Real> forwards(optionlets, 0.0);
61
62 auto yoyTS = index()->yoyInflationTermStructure();
63
64 Date settlement = nominalTermStructure_->referenceDate();
65
66 for (Size i=0; i<optionlets; ++i) {
67 Date paymentDate = arguments_.payDates[i];
68 if (paymentDate > settlement) { // discard expired caplets
71 nominalTermStructure_->discount(paymentDate) *
73
74 // We explicitly have the index and assume that
75 // the fixing is natural, i.e. no convexity adjustment.
76 // If that was required then we would also need
77 // nominal vols in the pricing engine, i.e. a different engine.
78 // This also means that we do not need the coupon to have
79 // a pricing engine to return the swaplet rate and then
80 // the adjusted fixing in the instrument.
81 forwards[i] = yoyTS->yoyRate(arguments_.fixingDates[i],Period(0,Days));
82 Rate forward = forwards[i];
83
84 Date fixingDate = arguments_.fixingDates[i];
85 Time sqrtTime = 0.0;
86 if (fixingDate > volatility_->baseDate()){
87 sqrtTime = std::sqrt(
88 volatility_->timeFromBase(fixingDate));
89 }
90
92 Rate strike = arguments_.capRates[i];
93 if (sqrtTime>0.0) {
94 stdDevs[i] = std::sqrt(
95 volatility_->totalVariance(fixingDate, strike, Period(0,Days)));
96
97 }
98
99 // sttDev=0 for already-fixed dates so everything on forward
100 values[i] = optionletImpl(Option::Call, strike,
101 forward, stdDevs[i], d);
102 }
104 Rate strike = arguments_.floorRates[i];
105 if (sqrtTime>0.0) {
106 stdDevs[i] = std::sqrt(
107 volatility_->totalVariance(fixingDate, strike, Period(0,Days)));
108 }
109 Real floorlet = optionletImpl(Option::Put, strike,
110 forward, stdDevs[i], d);
111 if (type == YoYInflationCapFloor::Floor) {
112 values[i] = floorlet;
113 } else {
114 // a collar is long a cap and short a floor
115 values[i] -= floorlet;
116 }
117
118 }
119 value += values[i];
120 }
121 }
122 results_.value = value;
123
124 results_.additionalResults["optionletsPrice"] = values;
125 results_.additionalResults["optionletsAtmForward"] = forwards;
127 results_.additionalResults["optionletsStdDev"] = stdDevs;
128 }
129
130
131 //======================================================================
132 // pricer implementations
133 //======================================================================
134
136 const ext::shared_ptr<YoYInflationIndex>& index,
138 const Handle<YieldTermStructure>& nominalTermStructure)
139 : YoYInflationCapFloorEngine(index, volatility, nominalTermStructure) {}
140
141
143 Rate forward, Real stdDev,
144 Real d) const
145 {
146 return blackFormula(type, strike,
147 forward, stdDev, d);
148 }
149
150
151
152 YoYInflationUnitDisplacedBlackCapFloorEngine
153 ::YoYInflationUnitDisplacedBlackCapFloorEngine(
154 const ext::shared_ptr<YoYInflationIndex>& index,
156 const Handle<YieldTermStructure>& nominalTermStructure)
157 : YoYInflationCapFloorEngine(index, volatility, nominalTermStructure) {}
158
159
161 Option::Type type, Rate strike,
162 Rate forward, Real stdDev,
163 Real d) const
164 {
165 // could use displacement parameter in blackFormula but this is clearer
166 return blackFormula(type, strike+1.0,
167 forward+1.0, stdDev, d);
168 }
169
170
172 const ext::shared_ptr<YoYInflationIndex>& index,
174 const Handle<YieldTermStructure>& nominalTermStructure)
175 : YoYInflationCapFloorEngine(index, volatility, nominalTermStructure) {}
176
177
179 Rate forward, Real stdDev,
180 Real d) const
181 {
182 return bachelierBlackFormula(type, strike,
183 forward, stdDev, d);
184 }
185
186}
187
Concrete date class.
Definition: date.hpp:125
Shared handle to an observable.
Definition: handle.hpp:41
std::map< std::string, ext::any > additionalResults
Definition: instrument.hpp:123
Size unregisterWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:245
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Real optionletImpl(Option::Type, Real strike, Real forward, Real stdDev, Real d) const override
descendents only need to implement this
YoYInflationBachelierCapFloorEngine(const ext::shared_ptr< YoYInflationIndex > &, const Handle< YoYOptionletVolatilitySurface > &vol, const Handle< YieldTermStructure > &nominalTermStructure)
Real optionletImpl(Option::Type, Real strike, Real forward, Real stdDev, Real d) const override
descendents only need to implement this
YoYInflationBlackCapFloorEngine(const ext::shared_ptr< YoYInflationIndex > &, const Handle< YoYOptionletVolatilitySurface > &vol, const Handle< YieldTermStructure > &nominalTermStructure)
Base YoY inflation cap/floor engine.
void setVolatility(const Handle< YoYOptionletVolatilitySurface > &vol)
ext::shared_ptr< YoYInflationIndex > index() const
ext::shared_ptr< YoYInflationIndex > index_
YoYInflationCapFloorEngine(ext::shared_ptr< YoYInflationIndex >, Handle< YoYOptionletVolatilitySurface > vol, Handle< YieldTermStructure > nominalTermStructure)
Handle< YieldTermStructure > nominalTermStructure_
virtual Real optionletImpl(Option::Type type, Rate strike, Rate forward, Real stdDev, Real d) const =0
descendents only need to implement this
Handle< YoYOptionletVolatilitySurface > volatility_
Real optionletImpl(Option::Type, Real strike, Real forward, Real stdDev, Real d) const override
descendents only need to implement this
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Real bachelierBlackFormula(Option::Type optionType, Real strike, Real forward, Real stdDev, Real discount)
Real blackFormula(Option::Type optionType, Real strike, Real forward, Real stdDev, Real discount, Real displacement)
STL namespace.