Coverage Report

Created: 2026-01-25 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/models/calibrationhelper.cpp
Line
Count
Source
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
 <https://www.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
    Volatility BlackCalibrationHelper::impliedVolatility(Real targetValue,
27
                                                         Real accuracy,
28
                                                         Size maxEvaluations,
29
                                                         Volatility minVol,
30
0
                                                         Volatility maxVol) const {
31
32
0
        auto error = [&](Volatility x) { return targetValue - blackPrice(x); };
33
0
        Brent solver;
34
0
        solver.setMaxEvaluations(maxEvaluations);
35
0
        return solver.solve(error, accuracy, volatility_->value(), minVol, maxVol);
36
0
    }
37
38
0
    Real BlackCalibrationHelper::calibrationError() {
39
0
        Real error;
40
        
41
0
        switch (calibrationErrorType_) {
42
0
          case RelativePriceError:
43
0
            error = std::fabs(marketValue() - modelValue())/marketValue();
44
0
            break;
45
0
          case PriceError:
46
0
            error = marketValue() - modelValue();
47
0
            break;
48
0
          case ImpliedVolError: 
49
0
            {
50
0
              Real minVol = volatilityType_ == ShiftedLognormal ? 0.0010 : 0.00005;
51
0
              Real maxVol = volatilityType_ == ShiftedLognormal ? 10.0 : 0.50;
52
0
              const Real lowerPrice = blackPrice(minVol);
53
0
              const Real upperPrice = blackPrice(maxVol);
54
0
              const Real modelPrice = modelValue();
55
56
0
              Volatility implied;
57
0
              if (modelPrice <= lowerPrice)
58
0
                  implied = minVol;
59
0
              else if (modelPrice >= upperPrice)
60
0
                  implied = maxVol;
61
0
              else
62
0
                  implied = this->impliedVolatility(
63
0
                                          modelPrice, 1e-12, 5000, minVol, maxVol);
64
0
              error = implied - volatility_->value();
65
0
            }
66
0
            break;
67
0
          default:
68
0
            QL_FAIL("unknown Calibration Error Type");
69
0
        }
70
        
71
0
        return error;
72
0
    }
73
}