QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
zabrinterpolatedsmilesection.hpp
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) 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
20/*! \file zabrinterpolatedsmilesection.hpp
21 \brief zabr interpolating smile section
22*/
23
24#ifndef quantlib_zabr_interpolated_smile_section_hpp
25#define quantlib_zabr_interpolated_smile_section_hpp
26
28#include <ql/handle.hpp>
33#include <utility>
34
35namespace QuantLib {
36
37template <typename Evaluation>
39 public:
40 //! \name Constructors
41 //@{
42 //! all market data are quotes
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());
64 //! no quotes
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());
86 //@}
87 //! \name LazyObject interface
88 //@{
89 void performCalculations() const override;
90 void update() override;
91 //@}
92 //! \name SmileSection interface
93 //@{
94 Real minStrike() const override;
95 Real maxStrike() const override;
96 Real atmLevel() const override;
97 //@}
98 Real varianceImpl(Rate strike) const override;
99 Volatility volatilityImpl(Rate strike) const override;
100 //! \name Inspectors
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;
110 //@}
111
112 protected:
113 //! Creates the mutable SABRInterpolation
114 void createInterpolation() const;
115 mutable ext::shared_ptr<ZabrInterpolation<Evaluation> > zabrInterpolation_;
116
117 //! Market data
120 std::vector<Handle<Quote> > volHandles_;
121 mutable std::vector<Rate> strikes_;
122 //! Only strikes corresponding to valid market data
123 mutable std::vector<Rate> actualStrikes_;
125
127 mutable std::vector<Volatility> vols_;
128 //! Sabr parameters
130 //! Sabr interpolation settings
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_->gamma();
178}
179
180template <typename Evaluation>
182 calculate();
183 return zabrInterpolation_->rmsError();
184}
185
186template <typename Evaluation>
188 calculate();
189 return zabrInterpolation_->maxError();
190}
191
192template <typename Evaluation>
195 calculate();
196 return zabrInterpolation_->endCriteria();
197}
198
199template <typename Evaluation>
201 calculate();
202 return actualStrikes_.front();
203}
204
205template <typename Evaluation>
207 calculate();
208 return actualStrikes_.back();
209}
210
211template <typename Evaluation>
213 calculate();
214 return forwardValue_;
215}
216
217template <typename Evaluation>
219 const Date& optionDate,
220 Handle<Quote> forward,
221 const std::vector<Rate>& strikes,
222 bool hasFloatingStrikes,
223 Handle<Quote> atmVolatility,
224 const std::vector<Handle<Quote> >& volHandles,
225 Real alpha,
226 Real beta,
227 Real nu,
228 Real rho,
229 Real gamma,
230 bool isAlphaFixed,
231 bool isBetaFixed,
232 bool isNuFixed,
233 bool isRhoFixed,
234 bool isGammaFixed,
235 bool vegaWeighted,
236 ext::shared_ptr<EndCriteria> endCriteria,
237 ext::shared_ptr<OptimizationMethod> method,
238 const DayCounter& dc)
239: SmileSection(optionDate, dc), forward_(std::move(forward)),
240 atmVolatility_(std::move(atmVolatility)), volHandles_(volHandles), strikes_(strikes),
241 actualStrikes_(strikes), hasFloatingStrikes_(hasFloatingStrikes), vols_(volHandles.size()),
242 alpha_(alpha), beta_(beta), nu_(nu), rho_(rho), gamma_(gamma), isAlphaFixed_(isAlphaFixed),
243 isBetaFixed_(isBetaFixed), isNuFixed_(isNuFixed), isRhoFixed_(isRhoFixed),
244 isGammaFixed_(isGammaFixed), vegaWeighted_(vegaWeighted), endCriteria_(std::move(endCriteria)),
245 method_(std::move(method)) {
246
249 for (auto& volHandle : volHandles_)
250 LazyObject::registerWith(volHandle);
251}
252
253template <typename Evaluation>
255 const Date& optionDate,
256 const Rate& forward,
257 const std::vector<Rate>& strikes,
258 bool hasFloatingStrikes,
259 const Volatility& atmVolatility,
260 const std::vector<Volatility>& volHandles,
261 Real alpha,
262 Real beta,
263 Real nu,
264 Real rho,
265 Real gamma,
266 bool isAlphaFixed,
267 bool isBetaFixed,
268 bool isNuFixed,
269 bool isRhoFixed,
270 bool isGammaFixed,
271 bool vegaWeighted,
272 ext::shared_ptr<EndCriteria> endCriteria,
273 ext::shared_ptr<OptimizationMethod> method,
274 const DayCounter& dc)
275: SmileSection(optionDate, dc),
276 forward_(Handle<Quote>(ext::shared_ptr<Quote>(new SimpleQuote(forward)))),
277 atmVolatility_(Handle<Quote>(ext::shared_ptr<Quote>(new SimpleQuote(atmVolatility)))),
278 volHandles_(volHandles.size()), strikes_(strikes), actualStrikes_(strikes),
279 hasFloatingStrikes_(hasFloatingStrikes), vols_(volHandles.size()), alpha_(alpha), beta_(beta),
280 nu_(nu), rho_(rho), gamma_(gamma), isAlphaFixed_(isAlphaFixed), isBetaFixed_(isBetaFixed),
281 isNuFixed_(isNuFixed), isRhoFixed_(isRhoFixed), isGammaFixed_(isGammaFixed),
282 vegaWeighted_(vegaWeighted), endCriteria_(std::move(endCriteria)), method_(std::move(method)) {
283
284 for (Size i = 0; i < volHandles_.size(); ++i)
286 ext::shared_ptr<Quote>(new SimpleQuote(volHandles[i])));
287}
288
289template <typename Evaluation>
291 ext::shared_ptr<ZabrInterpolation<Evaluation> > tmp(
293 actualStrikes_.begin(), actualStrikes_.end(), vols_.begin(),
294 exerciseTime(), forwardValue_, alpha_, beta_, nu_, rho_, gamma_,
295 isAlphaFixed_, isBetaFixed_, isNuFixed_, isRhoFixed_, isGammaFixed_,
296 vegaWeighted_, endCriteria_, method_));
297 swap(tmp, zabrInterpolation_);
298}
299
300template <typename Evaluation>
302 forwardValue_ = forward_->value();
303 vols_.clear();
304 actualStrikes_.clear();
305 // we populate the volatilities, skipping the invalid ones
306 for (Size i = 0; i < volHandles_.size(); ++i) {
307 if (volHandles_[i]->isValid()) {
308 if (hasFloatingStrikes_) {
309 actualStrikes_.push_back(forwardValue_ + strikes_[i]);
310 vols_.push_back(atmVolatility_->value() +
311 volHandles_[i]->value());
312 } else {
313 actualStrikes_.push_back(strikes_[i]);
314 vols_.push_back(volHandles_[i]->value());
315 }
316 }
317 }
318 // we are recreating the sabrinterpolation object unconditionnaly to
319 // avoid iterator invalidation
320 createInterpolation();
321 zabrInterpolation_->update();
322}
323
324template <typename Evaluation>
326 calculate();
327 Real v = (*zabrInterpolation_)(strike, true);
328 return v * v * exerciseTime();
329}
330}
331
332#endif
Actual/365 (Fixed) day counter.
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
Globally accessible relinkable pointer.
Real rho
framework for calculation on demand and result caching
Definition: any.hpp:35
void swap(Array &v, Array &w) noexcept
Definition: array.hpp:903
STL namespace.
ext::shared_ptr< BlackVolTermStructure > v
Real beta
Definition: sabr.cpp:200
Real nu
Definition: sabr.cpp:200
Real alpha
Definition: sabr.cpp:200
simple quote class
Smile section base class.
ZABR interpolation interpolation between discrete points.