QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
zabrinterpolatedsmilesection.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2014 Peter Caspers
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
24#ifndef quantlib_zabr_interpolated_smile_section_hpp
25#define quantlib_zabr_interpolated_smile_section_hpp
26
27#include <ql/experimental/volatility/zabrinterpolation.hpp>
28#include <ql/handle.hpp>
29#include <ql/patterns/lazyobject.hpp>
30#include <ql/quotes/simplequote.hpp>
31#include <ql/termstructures/volatility/smilesection.hpp>
32#include <ql/time/daycounters/actual365fixed.hpp>
33#include <utility>
34
35namespace QuantLib {
36
37template <typename Evaluation>
39 public:
41
42
44 const Date& optionDate,
45 Handle<Quote> forward,
46 const std::vector<Rate>& strikes,
47 bool hasFloatingStrikes,
48 Handle<Quote> atmVolatility,
49 const std::vector<Handle<Quote> >& volHandles,
50 Real alpha,
51 Real beta,
52 Real nu,
53 Real rho,
54 Real gamma,
55 bool isAlphaFixed = false,
56 bool isBetaFixed = false,
57 bool isNuFixed = false,
58 bool isRhoFixed = false,
59 bool isGammaFixed = false,
60 bool vegaWeighted = true,
61 ext::shared_ptr<EndCriteria> endCriteria = ext::shared_ptr<EndCriteria>(),
62 ext::shared_ptr<OptimizationMethod> method = ext::shared_ptr<OptimizationMethod>(),
63 const DayCounter& dc = Actual365Fixed());
66 const Date& optionDate,
67 const Rate& forward,
68 const std::vector<Rate>& strikes,
69 bool hasFloatingStrikes,
70 const Volatility& atmVolatility,
71 const std::vector<Volatility>& vols,
72 Real alpha,
73 Real beta,
74 Real nu,
75 Real rho,
76 Real gamma,
77 bool isAlphaFixed = false,
78 bool isBetaFixed = false,
79 bool isNuFixed = false,
80 bool isRhoFixed = false,
81 bool isGammaFixed = false,
82 bool vegaWeighted = true,
83 ext::shared_ptr<EndCriteria> endCriteria = ext::shared_ptr<EndCriteria>(),
84 ext::shared_ptr<OptimizationMethod> method = ext::shared_ptr<OptimizationMethod>(),
85 const DayCounter& dc = Actual365Fixed());
87
89 void performCalculations() const override;
90 void update() override;
92
94 Real minStrike() const override;
95 Real maxStrike() const override;
96 Real atmLevel() const override;
98 Real varianceImpl(Rate strike) const override;
99 Volatility volatilityImpl(Rate strike) const override;
101
102 Real alpha() const;
103 Real beta() const;
104 Real nu() const;
105 Real rho() const;
106 Real gamma() const;
107 Real rmsError() const;
108 Real maxError() const;
111
112 protected:
114 void createInterpolation() const;
115 mutable ext::shared_ptr<ZabrInterpolation<Evaluation> > zabrInterpolation_;
116
120 std::vector<Handle<Quote> > volHandles_;
121 mutable std::vector<Rate> strikes_;
123 mutable std::vector<Rate> actualStrikes_;
125
127 mutable std::vector<Volatility> vols_;
133 const ext::shared_ptr<EndCriteria> endCriteria_;
134 const ext::shared_ptr<OptimizationMethod> method_;
135};
136
137template <typename Evaluation>
141}
142
143template <typename Evaluation>
144inline Real
146 calculate();
147 return (*zabrInterpolation_)(strike, true);
148}
149
150template <typename Evaluation>
152 calculate();
153 return zabrInterpolation_->alpha();
154}
155
156template <typename Evaluation>
158 calculate();
159 return zabrInterpolation_->beta();
160}
161
162template <typename Evaluation>
164 calculate();
165 return zabrInterpolation_->nu();
166}
167
168template <typename Evaluation>
170 calculate();
171 return zabrInterpolation_->rho();
172}
173
174template <typename Evaluation>
176 calculate();
177 return zabrInterpolation_->rmsError();
178}
179
180template <typename Evaluation>
182 calculate();
183 return zabrInterpolation_->maxError();
184}
185
186template <typename Evaluation>
189 calculate();
190 return zabrInterpolation_->endCriteria();
191}
192
193template <typename Evaluation>
195 calculate();
196 return actualStrikes_.front();
197}
198
199template <typename Evaluation>
201 calculate();
202 return actualStrikes_.back();
203}
204
205template <typename Evaluation>
207 calculate();
208 return forwardValue_;
209}
210
211template <typename Evaluation>
213 const Date& optionDate,
214 Handle<Quote> forward,
215 const std::vector<Rate>& strikes,
216 bool hasFloatingStrikes,
217 Handle<Quote> atmVolatility,
218 const std::vector<Handle<Quote> >& volHandles,
219 Real alpha,
220 Real beta,
221 Real nu,
222 Real rho,
223 Real gamma,
224 bool isAlphaFixed,
225 bool isBetaFixed,
226 bool isNuFixed,
227 bool isRhoFixed,
228 bool isGammaFixed,
229 bool vegaWeighted,
230 ext::shared_ptr<EndCriteria> endCriteria,
231 ext::shared_ptr<OptimizationMethod> method,
232 const DayCounter& dc)
233: SmileSection(optionDate, dc), forward_(std::move(forward)),
234 atmVolatility_(std::move(atmVolatility)), volHandles_(volHandles), strikes_(strikes),
235 actualStrikes_(strikes), hasFloatingStrikes_(hasFloatingStrikes), vols_(volHandles.size()),
236 alpha_(alpha), beta_(beta), nu_(nu), rho_(rho), gamma_(gamma), isAlphaFixed_(isAlphaFixed),
237 isBetaFixed_(isBetaFixed), isNuFixed_(isNuFixed), isRhoFixed_(isRhoFixed),
238 isGammaFixed_(isGammaFixed), vegaWeighted_(vegaWeighted), endCriteria_(std::move(endCriteria)),
239 method_(std::move(method)) {
240
243 for (auto& volHandle : volHandles_)
244 LazyObject::registerWith(volHandle);
245}
246
247template <typename Evaluation>
249 const Date& optionDate,
250 const Rate& forward,
251 const std::vector<Rate>& strikes,
252 bool hasFloatingStrikes,
253 const Volatility& atmVolatility,
254 const std::vector<Volatility>& volHandles,
255 Real alpha,
256 Real beta,
257 Real nu,
258 Real rho,
259 Real gamma,
260 bool isAlphaFixed,
261 bool isBetaFixed,
262 bool isNuFixed,
263 bool isRhoFixed,
264 bool isGammaFixed,
265 bool vegaWeighted,
266 ext::shared_ptr<EndCriteria> endCriteria,
267 ext::shared_ptr<OptimizationMethod> method,
268 const DayCounter& dc)
269: SmileSection(optionDate, dc),
270 forward_(Handle<Quote>(ext::shared_ptr<Quote>(new SimpleQuote(forward)))),
271 atmVolatility_(Handle<Quote>(ext::shared_ptr<Quote>(new SimpleQuote(atmVolatility)))),
272 volHandles_(volHandles.size()), strikes_(strikes), actualStrikes_(strikes),
273 hasFloatingStrikes_(hasFloatingStrikes), vols_(volHandles.size()), alpha_(alpha), beta_(beta),
274 nu_(nu), rho_(rho), gamma_(gamma), isAlphaFixed_(isAlphaFixed), isBetaFixed_(isBetaFixed),
275 isNuFixed_(isNuFixed), isRhoFixed_(isRhoFixed), isGammaFixed_(isGammaFixed),
276 vegaWeighted_(vegaWeighted), endCriteria_(std::move(endCriteria)), method_(std::move(method)) {
277
278 for (Size i = 0; i < volHandles_.size(); ++i)
280 ext::shared_ptr<Quote>(new SimpleQuote(volHandles[i])));
281}
282
283template <typename Evaluation>
285 ext::shared_ptr<ZabrInterpolation<Evaluation> > tmp(
287 actualStrikes_.begin(), actualStrikes_.end(), vols_.begin(),
288 exerciseTime(), forwardValue_, alpha_, beta_, nu_, rho_, gamma_,
289 isAlphaFixed_, isBetaFixed_, isNuFixed_, isRhoFixed_, isGammaFixed_,
290 vegaWeighted_, endCriteria_, method_));
291 swap(tmp, zabrInterpolation_);
292}
293
294template <typename Evaluation>
296 forwardValue_ = forward_->value();
297 vols_.clear();
298 actualStrikes_.clear();
299 // we populate the volatilities, skipping the invalid ones
300 for (Size i = 0; i < volHandles_.size(); ++i) {
301 if (volHandles_[i]->isValid()) {
302 if (hasFloatingStrikes_) {
303 actualStrikes_.push_back(forwardValue_ + strikes_[i]);
304 vols_.push_back(atmVolatility_->value() +
305 volHandles_[i]->value());
306 } else {
307 actualStrikes_.push_back(strikes_[i]);
308 vols_.push_back(volHandles_[i]->value());
309 }
310 }
311 }
312 // we are recreating the sabrinterpolation object unconditionnaly to
313 // avoid iterator invalidation
314 createInterpolation();
315 zabrInterpolation_->update();
316}
317
318template <typename Evaluation>
320 calculate();
321 Real v = (*zabrInterpolation_)(strike, true);
322 return v * v * exerciseTime();
323}
324}
325
326#endif
Actual/365 (Fixed) day count convention.
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Shared handle to an observable.
Definition: handle.hpp:41
Framework for calculation on demand and result caching.
Definition: lazyobject.hpp:35
void update() override
Definition: lazyobject.hpp:188
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
purely virtual base class for market observables
Definition: quote.hpp:37
market element returning a stored value
Definition: simplequote.hpp:33
interest rate volatility smile section
void update() override
const ext::shared_ptr< EndCriteria > endCriteria_
void createInterpolation() const
Creates the mutable SABRInterpolation.
std::vector< Rate > actualStrikes_
Only strikes corresponding to valid market data.
const ext::shared_ptr< OptimizationMethod > method_
ZabrInterpolatedSmileSection(const Date &optionDate, Handle< Quote > forward, const std::vector< Rate > &strikes, bool hasFloatingStrikes, Handle< Quote > atmVolatility, const std::vector< Handle< Quote > > &volHandles, Real alpha, Real beta, Real nu, Real rho, Real gamma, bool isAlphaFixed=false, bool isBetaFixed=false, bool isNuFixed=false, bool isRhoFixed=false, bool isGammaFixed=false, bool vegaWeighted=true, ext::shared_ptr< EndCriteria > endCriteria=ext::shared_ptr< EndCriteria >(), ext::shared_ptr< OptimizationMethod > method=ext::shared_ptr< OptimizationMethod >(), const DayCounter &dc=Actual365Fixed())
all market data are quotes
ext::shared_ptr< ZabrInterpolation< Evaluation > > zabrInterpolation_
Volatility volatilityImpl(Rate strike) const override
zabr smile interpolation between discrete volatility points.
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
void swap(Array &v, Array &w) noexcept
Definition: array.hpp:903
STL namespace.