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