Coverage Report

Created: 2026-01-25 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/models/model.hpp
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) 2005, 2007 StatPro Italia srl
6
 Copyright (C) 2013, 2015 Peter Caspers
7
8
 This file is part of QuantLib, a free-software/open-source library
9
 for financial quantitative analysts and developers - http://quantlib.org/
10
11
 QuantLib is free software: you can redistribute it and/or modify it
12
 under the terms of the QuantLib license.  You should have received a
13
 copy of the license along with this program; if not, please email
14
 <quantlib-dev@lists.sf.net>. The license is also available online at
15
 <https://www.quantlib.org/license.shtml>.
16
17
 This program is distributed in the hope that it will be useful, but WITHOUT
18
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 FOR A PARTICULAR PURPOSE.  See the license for more details.
20
*/
21
22
/*! \file model.hpp
23
    \brief Abstract interest rate model class
24
*/
25
26
#ifndef quantlib_interest_rate_model_hpp
27
#define quantlib_interest_rate_model_hpp
28
29
#include <ql/math/optimization/endcriteria.hpp>
30
#include <ql/methods/lattices/lattice.hpp>
31
#include <ql/models/calibrationhelper.hpp>
32
#include <ql/models/parameter.hpp>
33
#include <ql/option.hpp>
34
#include <utility>
35
36
namespace QuantLib {
37
38
    class OptimizationMethod;
39
40
    //! Affine model class
41
    /*! Base class for analytically tractable models.
42
43
        \ingroup shortrate
44
    */
45
    class AffineModel : public virtual Observable {
46
      public:
47
        //! Implied discount curve
48
        virtual DiscountFactor discount(Time t) const = 0;
49
50
        virtual Real discountBond(Time now,
51
                                  Time maturity,
52
                                  Array factors) const = 0;
53
54
        virtual Real discountBondOption(Option::Type type,
55
                                        Real strike,
56
                                        Time maturity,
57
                                        Time bondMaturity) const = 0;
58
59
        virtual Real discountBondOption(Option::Type type,
60
                                        Real strike,
61
                                        Time maturity,
62
                                        Time bondStart,
63
                                        Time bondMaturity) const;
64
    };
65
66
67
    //! Term-structure consistent model class
68
    /*! This is a base class for models that can reprice exactly
69
        any discount bond.
70
71
        \ingroup shortrate
72
    */
73
    class TermStructureConsistentModel : public virtual Observable {
74
      public:
75
        TermStructureConsistentModel(Handle<YieldTermStructure> termStructure)
76
0
        : termStructure_(std::move(termStructure)) {}
77
0
        const Handle<YieldTermStructure>& termStructure() const {
78
0
            return termStructure_;
79
0
        }
80
      private:
81
        Handle<YieldTermStructure> termStructure_;
82
    };
83
84
85
    //! Calibrated model class
86
    class CalibratedModel : public virtual Observer, public virtual Observable {
87
      public:
88
        CalibratedModel(Size nArguments);
89
90
0
        void update() override {
91
0
            generateArguments();
92
0
            notifyObservers();
93
0
        }
94
95
        //! Calibrate to a set of market instruments (usually caps/swaptions)
96
        /*! An additional constraint can be passed which must be
97
            satisfied in addition to the constraints of the model.
98
        */
99
        virtual void calibrate(
100
                const std::vector<ext::shared_ptr<CalibrationHelper> >&,
101
                OptimizationMethod& method,
102
                const EndCriteria& endCriteria,
103
                const Constraint& constraint = Constraint(),
104
                const std::vector<Real>& weights = std::vector<Real>(),
105
                const std::vector<bool>& fixParameters = std::vector<bool>());
106
107
        Real value(const Array& params,
108
                   const std::vector<ext::shared_ptr<CalibrationHelper> >&);
109
110
        const ext::shared_ptr<Constraint>& constraint() const;
111
112
        //! Returns end criteria result
113
0
        EndCriteria::Type endCriteria() const { return shortRateEndCriteria_; }
114
115
        //! Returns the problem values
116
0
        const Array& problemValues() const { return problemValues_; }
117
118
        //! Returns array of arguments on which calibration is done
119
        Array params() const;
120
121
        virtual void setParams(const Array& params);
122
0
        Integer functionEvaluation() const { return functionEvaluation_; }
123
124
      protected:
125
0
        virtual void generateArguments() {}
126
        std::vector<Parameter> arguments_;
127
        ext::shared_ptr<Constraint> constraint_;
128
        EndCriteria::Type shortRateEndCriteria_ = EndCriteria::None;
129
        Array problemValues_;
130
        Integer functionEvaluation_;
131
132
      private:
133
        //! Constraint imposed on arguments
134
        class PrivateConstraint;
135
        //! Calibration cost function class
136
        class CalibrationFunction;
137
    };
138
139
    //! Abstract short-rate model class
140
    /*! \ingroup shortrate */
141
    class ShortRateModel : public CalibratedModel {
142
      public:
143
        explicit ShortRateModel(Size nArguments);
144
        virtual ext::shared_ptr<Lattice> tree(const TimeGrid&) const = 0;
145
    };
146
147
148
    // inline definitions
149
150
151
    inline Real AffineModel::discountBondOption(Option::Type type,
152
                                                Real strike,
153
                                                Time maturity,
154
                                                Time,
155
0
                                                Time bondMaturity) const {
156
0
        return discountBondOption(type, strike, maturity, bondMaturity);
157
0
    }
158
159
    inline const ext::shared_ptr<Constraint>&
160
0
    CalibratedModel::constraint() const {
161
0
        return constraint_;
162
0
    }
163
164
    class CalibratedModel::PrivateConstraint : public Constraint {
165
      private:
166
        class Impl final : public Constraint::Impl {
167
          public:
168
            explicit Impl(const std::vector<Parameter>& arguments)
169
0
            : arguments_(arguments) {}
170
171
0
            bool test(const Array& params) const override {
172
0
                Size k=0;
173
0
                for (const auto& argument : arguments_) {
174
0
                    Size size = argument.size();
175
0
                    Array testParams(size);
176
0
                    for (Size j=0; j<size; j++, k++)
177
0
                        testParams[j] = params[k];
178
0
                    if (!argument.testParams(testParams))
179
0
                        return false;
180
0
                }
181
0
                return true;
182
0
            }
183
184
0
            Array upperBound(const Array& params) const override {
185
0
                Size k = 0, k2 = 0;
186
0
                Size totalSize = 0;
187
0
                for (const auto& argument : arguments_) {
188
0
                    totalSize += argument.size();
189
0
                }
190
0
                Array result(totalSize);
191
0
                for (const auto& argument : arguments_) {
192
0
                    Size size = argument.size();
193
0
                    Array partialParams(size);
194
0
                    for (Size j = 0; j < size; j++, k++)
195
0
                        partialParams[j] = params[k];
196
0
                    Array tmpBound = argument.constraint().upperBound(partialParams);
197
0
                    for (Size j = 0; j < size; j++, k2++)
198
0
                        result[k2] = tmpBound[j];
199
0
                }
200
0
                return result;
201
0
            }
202
203
0
            Array lowerBound(const Array& params) const override {
204
0
                Size k = 0, k2 = 0;
205
0
                Size totalSize = 0;
206
0
                for (const auto& argument : arguments_) {
207
0
                    totalSize += argument.size();
208
0
                }
209
0
                Array result(totalSize);
210
0
                for (const auto& argument : arguments_) {
211
0
                    Size size = argument.size();
212
0
                    Array partialParams(size);
213
0
                    for (Size j = 0; j < size; j++, k++)
214
0
                        partialParams[j] = params[k];
215
0
                    Array tmpBound = argument.constraint().lowerBound(partialParams);
216
0
                    for (Size j = 0; j < size; j++, k2++)
217
0
                        result[k2] = tmpBound[j];
218
0
                }
219
0
                return result;
220
0
            }
221
222
          private:
223
            const std::vector<Parameter>& arguments_;
224
        };
225
      public:
226
        explicit PrivateConstraint(const std::vector<Parameter>& arguments)
227
0
        : Constraint(ext::shared_ptr<Constraint::Impl>(
228
0
                                   new PrivateConstraint::Impl(arguments))) {}
229
    };
230
231
}
232
233
#endif