Coverage Report

Created: 2025-09-04 07:11

/src/quantlib/ql/models/calibrationhelper.hpp
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
 <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
/*! \file calibrationhelper.hpp
22
    \brief Calibration helper class
23
*/
24
25
#ifndef quantlib_interest_rate_modelling_calibration_helper_h
26
#define quantlib_interest_rate_modelling_calibration_helper_h
27
28
#include <ql/patterns/lazyobject.hpp>
29
#include <ql/quote.hpp>
30
#include <ql/termstructures/volatility/volatilitytype.hpp>
31
#include <ql/termstructures/yieldtermstructure.hpp>
32
#include <list>
33
#include <utility>
34
35
namespace QuantLib {
36
37
    class PricingEngine;
38
39
    //! abstract base class for calibration helpers
40
    class CalibrationHelper {
41
      public:
42
0
        virtual ~CalibrationHelper() = default;
43
        //! returns the error resulting from the model valuation
44
        virtual Real calibrationError() = 0;
45
    };
46
47
    //! liquid Black76 market instrument used during calibration
48
    class BlackCalibrationHelper : public LazyObject, public CalibrationHelper {
49
      public:
50
        enum CalibrationErrorType {
51
                            RelativePriceError, PriceError, ImpliedVolError};
52
53
        BlackCalibrationHelper(Handle<Quote> volatility,
54
                               CalibrationErrorType calibrationErrorType = RelativePriceError,
55
                               const VolatilityType type = ShiftedLognormal,
56
                               const Real shift = 0.0)
57
0
        : volatility_(std::move(volatility)), volatilityType_(type), shift_(shift),
58
0
          calibrationErrorType_(calibrationErrorType) {
59
0
            registerWith(volatility_);
60
0
        }
61
62
0
        void performCalculations() const override {
63
0
            marketValue_ = blackPrice(volatility_->value());
64
0
        }
65
66
        //! returns the volatility Handle
67
0
        Handle<Quote> volatility() const { return volatility_; }
68
69
        //! returns the volatility type
70
0
        VolatilityType volatilityType() const { return volatilityType_; }
71
72
        //! returns the actual price of the instrument (from volatility)
73
0
        Real marketValue() const { calculate(); return marketValue_; }
74
75
        //! returns the price of the instrument according to the model
76
        virtual Real modelValue() const = 0;
77
78
        //! returns the error resulting from the model valuation
79
        Real calibrationError() override;
80
81
        virtual void addTimesTo(std::list<Time>& times) const = 0;
82
83
        //! Black volatility implied by the model
84
        Volatility impliedVolatility(Real targetValue,
85
                                     Real accuracy,
86
                                     Size maxEvaluations,
87
                                     Volatility minVol,
88
                                     Volatility maxVol) const;
89
90
        //! Black or Bachelier price given a volatility
91
        virtual Real blackPrice(Volatility volatility) const = 0;
92
93
0
        void setPricingEngine(const ext::shared_ptr<PricingEngine>& engine) {
94
0
            engine_ = engine;
95
0
        }
96
97
      protected:
98
        mutable Real marketValue_;
99
        Handle<Quote> volatility_;
100
        ext::shared_ptr<PricingEngine> engine_;
101
        const VolatilityType volatilityType_;
102
        const Real shift_;
103
104
      private:
105
        class ImpliedVolatilityHelper;
106
        const CalibrationErrorType calibrationErrorType_;
107
    };
108
109
}
110
111
112
#endif