QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
abcdinterpolation.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2007 Ferdinando Ametrano
5 Copyright (C) 2007 Cristina Duminuco
6 Copyright (C) 2007 Giorgio Facchinetti
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
26#ifndef quantlib_abcd_interpolation_hpp
27#define quantlib_abcd_interpolation_hpp
28
29#include <ql/math/interpolation.hpp>
30#include <ql/math/interpolations/linearinterpolation.hpp>
31#include <ql/termstructures/volatility/abcd.hpp>
32#include <ql/termstructures/volatility/abcdcalibration.hpp>
33#include <utility>
34
35namespace QuantLib {
36
37 class EndCriteria;
38 class OptimizationMethod;
39
40 namespace detail {
41
43 public:
45 Real b,
46 Real c,
47 Real d,
48 bool aIsFixed,
49 bool bIsFixed,
50 bool cIsFixed,
51 bool dIsFixed)
52 : a_(a), b_(b), c_(c), d_(d),
53
55 if (a_ != Null<Real>())
56 aIsFixed_ = aIsFixed;
57 else a_ = -0.06;
58 if (b_ != Null<Real>())
59 bIsFixed_ = bIsFixed;
60 else b_ = 0.17;
61 if (c_ != Null<Real>())
62 cIsFixed_ = cIsFixed;
63 else c_ = 0.54;
64 if (d_ != Null<Real>())
65 dIsFixed_ = dIsFixed;
66 else d_ = 0.17;
67
69 }
70 virtual ~AbcdCoeffHolder() = default;
72 bool aIsFixed_ = false, bIsFixed_ = false, cIsFixed_ = false, dIsFixed_ = false;
73 std::vector<Real> k_;
76 };
77
78 template <class I1, class I2>
80 public AbcdCoeffHolder {
81 public:
82 AbcdInterpolationImpl(const I1& xBegin,
83 const I1& xEnd,
84 const I2& yBegin,
85 Real a,
86 Real b,
87 Real c,
88 Real d,
89 bool aIsFixed,
90 bool bIsFixed,
91 bool cIsFixed,
92 bool dIsFixed,
93 bool vegaWeighted,
94 ext::shared_ptr<EndCriteria> endCriteria,
95 ext::shared_ptr<OptimizationMethod> optMethod)
96 : Interpolation::templateImpl<I1, I2>(xBegin, xEnd, yBegin),
97 AbcdCoeffHolder(a, b, c, d, aIsFixed, bIsFixed, cIsFixed, dIsFixed),
98 endCriteria_(std::move(endCriteria)), optMethod_(std::move(optMethod)),
99 vegaWeighted_(vegaWeighted) {}
100
101 void update() override {
102 auto x = this->xBegin_;
103 auto y = this->yBegin_;
104 std::vector<Real> times, blackVols;
105 for ( ; x!=this->xEnd_; ++x, ++y) {
106 times.push_back(*x);
107 blackVols.push_back(*y);
108 }
109 abcdCalibrator_ = ext::shared_ptr<AbcdCalibration>(
110 new AbcdCalibration(times, blackVols,
111 a_, b_, c_, d_,
116 optMethod_));
117 abcdCalibrator_->compute();
118 a_ = abcdCalibrator_->a();
119 b_ = abcdCalibrator_->b();
120 c_ = abcdCalibrator_->c();
121 d_ = abcdCalibrator_->d();
122 k_ = abcdCalibrator_->k(times, blackVols);
123 error_ = abcdCalibrator_->error();
124 maxError_ = abcdCalibrator_->maxError();
125 abcdEndCriteria_ = abcdCalibrator_->endCriteria();
126 }
127 Real value(Real x) const override {
128 QL_REQUIRE(x>=0.0, "time must be non negative: " <<
129 x << " not allowed");
130 return abcdCalibrator_->value(x);
131 }
132 Real primitive(Real) const override { QL_FAIL("Abcd primitive not implemented"); }
133 Real derivative(Real) const override { QL_FAIL("Abcd derivative not implemented"); }
134 Real secondDerivative(Real) const override {
135 QL_FAIL("Abcd secondDerivative not implemented");
136 }
137 Real k(Time t) const {
138 LinearInterpolation li(this->xBegin_, this->xEnd_, this->yBegin_);
139 return li(t);
140 }
141
142 private:
143 const ext::shared_ptr<EndCriteria> endCriteria_;
144 const ext::shared_ptr<OptimizationMethod> optMethod_;
146 ext::shared_ptr<AbcdCalibration> abcdCalibrator_;
147
148 };
149
150 }
151
153
158 public:
160 template <class I1, class I2>
161 AbcdInterpolation(const I1& xBegin, // x = times
162 const I1& xEnd,
163 const I2& yBegin, // y = volatilities
164 Real a = -0.06,
165 Real b = 0.17,
166 Real c = 0.54,
167 Real d = 0.17,
168 bool aIsFixed = false,
169 bool bIsFixed = false,
170 bool cIsFixed = false,
171 bool dIsFixed = false,
172 bool vegaWeighted = false,
173 const ext::shared_ptr<EndCriteria>& endCriteria
174 = ext::shared_ptr<EndCriteria>(),
175 const ext::shared_ptr<OptimizationMethod>& optMethod
176 = ext::shared_ptr<OptimizationMethod>()) {
177
178 impl_ = ext::shared_ptr<Interpolation::Impl>(new
179 detail::AbcdInterpolationImpl<I1,I2>(xBegin, xEnd, yBegin,
180 a, b, c, d,
181 aIsFixed, bIsFixed,
182 cIsFixed, dIsFixed,
183 vegaWeighted,
185 optMethod));
186 impl_->update();
187 }
189
190 Real a() const { return coeffs().a_; }
191 Real b() const { return coeffs().b_; }
192 Real c() const { return coeffs().c_; }
193 Real d() const { return coeffs().d_; }
194 std::vector<Real> k() const { return coeffs().k_; }
195 Real rmsError() const { return coeffs().error_; }
196 Real maxError() const { return coeffs().maxError_; }
198 template <class I1>
199 Real k(Time t, const I1& xBegin, const I1& xEnd) const {
200 LinearInterpolation li(xBegin, xEnd, (coeffs().k_).begin());
201 return li(t);
202 }
203 private:
205 return *dynamic_cast<detail::AbcdCoeffHolder*>(impl_.get());
206 }
207 };
208
210
211 class Abcd {
212 public:
214 Real b,
215 Real c,
216 Real d,
217 bool aIsFixed,
218 bool bIsFixed,
219 bool cIsFixed,
220 bool dIsFixed,
221 bool vegaWeighted = false,
222 ext::shared_ptr<EndCriteria> endCriteria = ext::shared_ptr<EndCriteria>(),
223 ext::shared_ptr<OptimizationMethod> optMethod = ext::shared_ptr<OptimizationMethod>())
224 : a_(a), b_(b), c_(c), d_(d), aIsFixed_(aIsFixed), bIsFixed_(bIsFixed), cIsFixed_(cIsFixed),
225 dIsFixed_(dIsFixed), vegaWeighted_(vegaWeighted), endCriteria_(std::move(endCriteria)),
226 optMethod_(std::move(optMethod)) {}
227 template <class I1, class I2>
228 Interpolation interpolate(const I1& xBegin, const I1& xEnd,
229 const I2& yBegin) const {
230 return AbcdInterpolation(xBegin, xEnd, yBegin,
231 a_, b_, c_, d_,
236 }
237 static const bool global = true;
238 private:
242 const ext::shared_ptr<EndCriteria> endCriteria_;
243 const ext::shared_ptr<OptimizationMethod> optMethod_;
244 };
245
246}
247
248#endif
Abcd interpolation factory and traits
static const bool global
Abcd(Real a, Real b, Real c, Real d, bool aIsFixed, bool bIsFixed, bool cIsFixed, bool dIsFixed, bool vegaWeighted=false, ext::shared_ptr< EndCriteria > endCriteria=ext::shared_ptr< EndCriteria >(), ext::shared_ptr< OptimizationMethod > optMethod=ext::shared_ptr< OptimizationMethod >())
const ext::shared_ptr< OptimizationMethod > optMethod_
const ext::shared_ptr< EndCriteria > endCriteria_
Interpolation interpolate(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin) const
Abcd interpolation between discrete points.
AbcdInterpolation(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, Real a=-0.06, Real b=0.17, Real c=0.54, Real d=0.17, bool aIsFixed=false, bool bIsFixed=false, bool cIsFixed=false, bool dIsFixed=false, bool vegaWeighted=false, const ext::shared_ptr< EndCriteria > &endCriteria=ext::shared_ptr< EndCriteria >(), const ext::shared_ptr< OptimizationMethod > &optMethod=ext::shared_ptr< OptimizationMethod >())
const detail::AbcdCoeffHolder & coeffs() const
Real k(Time t, const I1 &xBegin, const I1 &xEnd) const
std::vector< Real > k() const
static void validate(Real a, Real b, Real c, Real d)
basic template implementation
templateImpl(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const int requiredPoints=2)
base class for 1-D interpolations.
ext::shared_ptr< Impl > impl_
Linear interpolation between discrete points
template class providing a null value for a given type.
Definition: null.hpp:76
AbcdCoeffHolder(Real a, Real b, Real c, Real d, bool aIsFixed, bool bIsFixed, bool cIsFixed, bool dIsFixed)
virtual ~AbcdCoeffHolder()=default
AbcdInterpolationImpl(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, Real a, Real b, Real c, Real d, bool aIsFixed, bool bIsFixed, bool cIsFixed, bool dIsFixed, bool vegaWeighted, ext::shared_ptr< EndCriteria > endCriteria, ext::shared_ptr< OptimizationMethod > optMethod)
ext::shared_ptr< AbcdCalibration > abcdCalibrator_
const ext::shared_ptr< OptimizationMethod > optMethod_
const ext::shared_ptr< EndCriteria > endCriteria_
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
STL namespace.