QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
abcdcalibration.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2006, 2015 Ferdinando Ametrano
5 Copyright (C) 2006 Cristina Duminuco
6 Copyright (C) 2005, 2006 Klaus Spanderen
7 Copyright (C) 2007 Giorgio Facchinetti
8 Copyright (C) 2015 Paolo Mazzocchi
9
10 This file is part of QuantLib, a free-software/open-source library
11 for financial quantitative analysts and developers - http://quantlib.org/
12
13 QuantLib is free software: you can redistribute it and/or modify it
14 under the terms of the QuantLib license. You should have received a
15 copy of the license along with this program; if not, please email
16 <quantlib-dev@lists.sf.net>. The license is also available online at
17 <http://quantlib.org/license.shtml>.
18
19 This program is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21 FOR A PARTICULAR PURPOSE. See the license for more details.
22*/
23
24#include <ql/math/distributions/normaldistribution.hpp>
25#include <ql/math/interpolations/abcdinterpolation.hpp>
26#include <ql/math/optimization/constraint.hpp>
27#include <ql/math/optimization/levenbergmarquardt.hpp>
28#include <ql/math/optimization/method.hpp>
29#include <ql/pricingengines/blackformula.hpp>
30#include <ql/termstructures/volatility/abcd.hpp>
31#include <ql/termstructures/volatility/abcdcalibration.hpp>
32#include <utility>
33
34namespace QuantLib {
35
36 // to constrained <- from unconstrained
38 y_[1] = x[1];
39 y_[2] = std::exp(x[2]);
40 y_[3] = std::exp(x[3]);
41 y_[0] = std::exp(x[0]) - y_[3];
42 return y_;
43 }
44
45 // to unconstrained <- from constrained
47 y_[1] = x[1];
48 y_[2] = std::log(x[2]);
49 y_[3] = std::log(x[3]);
50 y_[0] = std::log(x[0] + x[3]);
51 return y_;
52 }
53
54 // to constrained <- from unconstrained
55
56 AbcdCalibration::AbcdCalibration(const std::vector<Real>& t,
57 const std::vector<Real>& blackVols,
58 Real a,
59 Real b,
60 Real c,
61 Real d,
62 bool aIsFixed,
63 bool bIsFixed,
64 bool cIsFixed,
65 bool dIsFixed,
66 bool vegaWeighted,
67 ext::shared_ptr<EndCriteria> endCriteria,
68 ext::shared_ptr<OptimizationMethod> optMethod)
69 : aIsFixed_(aIsFixed), bIsFixed_(bIsFixed), cIsFixed_(cIsFixed), dIsFixed_(dIsFixed), a_(a),
70 b_(b), c_(c), d_(d), abcdEndCriteria_(EndCriteria::None),
71 endCriteria_(std::move(endCriteria)), optMethod_(std::move(optMethod)),
72 weights_(blackVols.size(), 1.0 / blackVols.size()), vegaWeighted_(vegaWeighted), times_(t),
73 blackVols_(blackVols) {
74
76
77 QL_REQUIRE(blackVols.size()==t.size(),
78 "mismatch between number of times (" << t.size() <<
79 ") and blackVols (" << blackVols.size() << ")");
80
81 // if no optimization method or endCriteria is provided, we provide one
82 if (!optMethod_) {
83 Real epsfcn = 1.0e-8;
84 Real xtol = 1.0e-8;
85 Real gtol = 1.0e-8;
86 bool useCostFunctionsJacobian = false;
87 optMethod_ = ext::shared_ptr<OptimizationMethod>(new
88 LevenbergMarquardt(epsfcn, xtol, gtol, useCostFunctionsJacobian));
89 }
90 if (!endCriteria_) {
91 Size maxIterations = 10000;
92 Size maxStationaryStateIterations = 1000;
93 Real rootEpsilon = 1.0e-8;
94 Real functionEpsilon = 0.3e-4; // Why 0.3e-4 ?
95 Real gradientNormEpsilon = 0.3e-4; // Why 0.3e-4 ?
96 endCriteria_ = ext::make_shared<EndCriteria>(maxIterations, maxStationaryStateIterations,
97 rootEpsilon, functionEpsilon, gradientNormEpsilon);
98 }
99 }
100
102 if (vegaWeighted_) {
103 Real weightsSum = 0.0;
104 for (Size i=0; i<times_.size() ; i++) {
105 Real stdDev = std::sqrt(blackVols_[i]* blackVols_[i]* times_[i]);
106 // when strike==forward, the blackFormulaStdDevDerivative becomes
108 weightsSum += weights_[i];
109 }
110 // weight normalization
111 for (Size i=0; i<times_.size() ; i++) {
112 weights_[i] /= weightsSum;
113 }
114 }
115
116 // there is nothing to optimize
117 if (aIsFixed_ && bIsFixed_ && cIsFixed_ && dIsFixed_) {
119 //error_ = interpolationError();
120 //maxError_ = interpolationMaxError();
121 return;
122 } else {
123
124 AbcdError costFunction(this);
125 transformation_ = ext::shared_ptr<ParametersTransformation>(new
127
128 Array guess(4);
129 guess[0] = a_;
130 guess[1] = b_;
131 guess[2] = c_;
132 guess[3] = d_;
133
134 std::vector<bool> parameterAreFixed(4);
135 parameterAreFixed[0] = aIsFixed_;
136 parameterAreFixed[1] = bIsFixed_;
137 parameterAreFixed[2] = cIsFixed_;
138 parameterAreFixed[3] = dIsFixed_;
139
140 Array inversedTransformatedGuess(transformation_->inverse(guess));
141
142 ProjectedCostFunction projectedAbcdCostFunction(costFunction,
143 inversedTransformatedGuess, parameterAreFixed);
144
145 Array projectedGuess
146 (projectedAbcdCostFunction.project(inversedTransformatedGuess));
147
148 NoConstraint constraint;
149 Problem problem(projectedAbcdCostFunction, constraint, projectedGuess);
150 abcdEndCriteria_ = optMethod_->minimize(problem, *endCriteria_);
151 Array projectedResult(problem.currentValue());
152 Array transfResult(projectedAbcdCostFunction.include(projectedResult));
153
154 Array result = transformation_->direct(transfResult);
156 a_ = result[0];
157 b_ = result[1];
158 c_ = result[2];
159 d_ = result[3];
160
161 }
162 }
163
165 return abcdBlackVolatility(x,a_,b_,c_,d_);
166 }
167
168 std::vector<Real> AbcdCalibration::k(const std::vector<Real>& t,
169 const std::vector<Real>& blackVols) const {
170 QL_REQUIRE(blackVols.size()==t.size(),
171 "mismatch between number of times (" << t.size() <<
172 ") and blackVols (" << blackVols.size() << ")");
173 std::vector<Real> k(t.size());
174 for (Size i=0; i<t.size() ; i++) {
175 k[i]=blackVols[i]/value(t[i]);
176 }
177 return k;
178 }
179
181 Size n = times_.size();
182 Real error, squaredError = 0.0;
183 for (Size i=0; i<times_.size() ; i++) {
184 error = (value(times_[i]) - blackVols_[i]);
185 squaredError += error * error * weights_[i];
186 }
187 return std::sqrt(n*squaredError/(n-1));
188 }
189
192 for (Size i=0; i<times_.size() ; i++) {
193 error = std::fabs(value(times_[i]) - blackVols_[i]);
194 maxError = std::max(maxError, error);
195 }
196 return maxError;
197 }
198
199 // calculate weighted differences
201 Array results(times_.size());
202 for (Size i=0; i<times_.size() ; i++) {
203 results[i] = (value(times_[i]) - blackVols_[i])* std::sqrt(weights_[i]);
204 }
205 return results;
206 }
207
209 return abcdEndCriteria_;
210 }
211
212}
ext::shared_ptr< EndCriteria > endCriteria_
std::vector< Real > blackVols_
EndCriteria::Type endCriteria() const
ext::shared_ptr< ParametersTransformation > transformation_
std::vector< Real > k(const std::vector< Real > &t, const std::vector< Real > &blackVols) const
adjustment factors needed to match Black vols
std::vector< Real > weights_
std::vector< Real > times_
Parameters.
EndCriteria::Type abcdEndCriteria_
ext::shared_ptr< OptimizationMethod > optMethod_
static void validate(Real a, Real b, Real c, Real d)
1-D array used in linear algebra.
Definition: array.hpp:52
Cumulative normal distribution function.
Criteria to end optimization process:
Definition: endcriteria.hpp:40
Levenberg-Marquardt optimization method.
No constraint.
Definition: constraint.hpp:79
Constrained optimization problem.
Definition: problem.hpp:42
const Array & currentValue() const
current value of the local minimum
Definition: problem.hpp:81
Parameterized cost function.
virtual Array include(const Array &projectedParameters) const
returns whole set of parameters corresponding to the set
Definition: projection.cpp:67
virtual Array project(const Array &parameters) const
returns the subset of free parameters corresponding
Definition: projection.cpp:54
#define QL_MIN_REAL
Definition: qldefines.hpp:175
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Real abcdBlackVolatility(Time u, Real a, Real b, Real c, Real d)
Definition: abcd.hpp:116
STL namespace.