QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
calibrationhelper.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb
5 Copyright (C) 2015 Peter Caspers
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21#include <ql/models/calibrationhelper.hpp>
22#include <ql/math/solvers1d/brent.hpp>
23
24namespace QuantLib {
25
26 class BlackCalibrationHelper::ImpliedVolatilityHelper {
27 public:
28 ImpliedVolatilityHelper(const BlackCalibrationHelper& helper,
29 Real value)
30 : helper_(helper), value_(value) {}
31
32 Real operator()(Volatility x) const {
33 return value_ - helper_.blackPrice(x);
34 }
35 private:
36 const BlackCalibrationHelper& helper_;
37 Real value_;
38 };
39
41 Real accuracy,
42 Size maxEvaluations,
43 Volatility minVol,
44 Volatility maxVol) const {
45
46 ImpliedVolatilityHelper f(*this,targetValue);
47 Brent solver;
48 solver.setMaxEvaluations(maxEvaluations);
49 return solver.solve(f,accuracy,volatility_->value(),minVol,maxVol);
50 }
51
53 Real error;
54
55 switch (calibrationErrorType_) {
57 error = std::fabs(marketValue() - modelValue())/marketValue();
58 break;
59 case PriceError:
60 error = marketValue() - modelValue();
61 break;
62 case ImpliedVolError:
63 {
64 Real minVol = volatilityType_ == ShiftedLognormal ? 0.0010 : 0.00005;
65 Real maxVol = volatilityType_ == ShiftedLognormal ? 10.0 : 0.50;
66 const Real lowerPrice = blackPrice(minVol);
67 const Real upperPrice = blackPrice(maxVol);
68 const Real modelPrice = modelValue();
69
70 Volatility implied;
71 if (modelPrice <= lowerPrice)
72 implied = minVol;
73 else if (modelPrice >= upperPrice)
74 implied = maxVol;
75 else
76 implied = this->impliedVolatility(
77 modelPrice, 1e-12, 5000, minVol, maxVol);
78 error = implied - volatility_->value();
79 }
80 break;
81 default:
82 QL_FAIL("unknown Calibration Error Type");
83 }
84
85 return error;
86 }
87}
Volatility impliedVolatility(Real targetValue, Real accuracy, Size maxEvaluations, Volatility minVol, Volatility maxVol) const
Black volatility implied by the model.
virtual Real modelValue() const =0
returns the price of the instrument according to the model
Real calibrationError() override
returns the error resulting from the model valuation
Real marketValue() const
returns the actual price of the instrument (from volatility)
const CalibrationErrorType calibrationErrorType_
virtual Real blackPrice(Volatility volatility) const =0
Black or Bachelier price given a volatility.
Brent 1-D solver
Definition: brent.hpp:37
void setMaxEvaluations(Size evaluations)
Definition: solver1d.hpp:238
Real solve(const F &f, Real accuracy, Real guess, Real step) const
Definition: solver1d.hpp:84
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35