Coverage Report

Created: 2025-08-05 06:45

/src/quantlib/ql/models/calibrationhelper.cpp
Line
Count
Source (jump to first uncovered line)
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
24
namespace QuantLib {
25
26
    class BlackCalibrationHelper::ImpliedVolatilityHelper {
27
      public:
28
        ImpliedVolatilityHelper(const BlackCalibrationHelper& helper,
29
                                Real value)
30
0
        : helper_(helper), value_(value) {}
31
32
0
        Real operator()(Volatility x) const {
33
0
            return value_ - helper_.blackPrice(x);
34
0
        }
35
      private:
36
        const BlackCalibrationHelper& helper_;
37
        Real value_;
38
    };
39
40
    Volatility BlackCalibrationHelper::impliedVolatility(Real targetValue,
41
                                                         Real accuracy,
42
                                                         Size maxEvaluations,
43
                                                         Volatility minVol,
44
0
                                                         Volatility maxVol) const {
45
46
0
        ImpliedVolatilityHelper f(*this,targetValue);
47
0
        Brent solver;
48
0
        solver.setMaxEvaluations(maxEvaluations);
49
0
        return solver.solve(f,accuracy,volatility_->value(),minVol,maxVol);
50
0
    }
51
52
0
    Real BlackCalibrationHelper::calibrationError() {
53
0
        Real error;
54
        
55
0
        switch (calibrationErrorType_) {
56
0
          case RelativePriceError:
57
0
            error = std::fabs(marketValue() - modelValue())/marketValue();
58
0
            break;
59
0
          case PriceError:
60
0
            error = marketValue() - modelValue();
61
0
            break;
62
0
          case ImpliedVolError: 
63
0
            {
64
0
              Real minVol = volatilityType_ == ShiftedLognormal ? 0.0010 : 0.00005;
65
0
              Real maxVol = volatilityType_ == ShiftedLognormal ? 10.0 : 0.50;
66
0
              const Real lowerPrice = blackPrice(minVol);
67
0
              const Real upperPrice = blackPrice(maxVol);
68
0
              const Real modelPrice = modelValue();
69
70
0
              Volatility implied;
71
0
              if (modelPrice <= lowerPrice)
72
0
                  implied = minVol;
73
0
              else if (modelPrice >= upperPrice)
74
0
                  implied = maxVol;
75
0
              else
76
0
                  implied = this->impliedVolatility(
77
0
                                          modelPrice, 1e-12, 5000, minVol, maxVol);
78
0
              error = implied - volatility_->value();
79
0
            }
80
0
            break;
81
0
          default:
82
0
            QL_FAIL("unknown Calibration Error Type");
83
0
        }
84
        
85
0
        return error;
86
0
    }
87
}