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 <http://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
36namespace 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 : termStructure_(std::move(termStructure)) {}
77 const Handle<YieldTermStructure>& termStructure() const {
78 return termStructure_;
79 }
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 void update() override {
91 generateArguments();
92 notifyObservers();
93 }
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 EndCriteria::Type endCriteria() const { return shortRateEndCriteria_; }
114
115 //! Returns the problem values
116 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 Integer functionEvaluation() const { return functionEvaluation_; }
123
124 protected:
125 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 Time bondMaturity) const {
156 return discountBondOption(type, strike, maturity, bondMaturity);
157 }
158
159 inline const ext::shared_ptr<Constraint>&
160 CalibratedModel::constraint() const {
161 return constraint_;
162 }
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 : arguments_(arguments) {}
170
171 bool test(const Array& params) const override {
172 Size k=0;
173 for (const auto& argument : arguments_) {
174 Size size = argument.size();
175 Array testParams(size);
176 for (Size j=0; j<size; j++, k++)
177 testParams[j] = params[k];
178 if (!argument.testParams(params: testParams))
179 return false;
180 }
181 return true;
182 }
183
184 Array upperBound(const Array& params) const override {
185 Size k = 0, k2 = 0;
186 Size totalSize = 0;
187 for (const auto& argument : arguments_) {
188 totalSize += argument.size();
189 }
190 Array result(totalSize);
191 for (const auto& argument : arguments_) {
192 Size size = argument.size();
193 Array partialParams(size);
194 for (Size j = 0; j < size; j++, k++)
195 partialParams[j] = params[k];
196 Array tmpBound = argument.constraint().upperBound(params: partialParams);
197 for (Size j = 0; j < size; j++, k2++)
198 result[k2] = tmpBound[j];
199 }
200 return result;
201 }
202
203 Array lowerBound(const Array& params) const override {
204 Size k = 0, k2 = 0;
205 Size totalSize = 0;
206 for (const auto& argument : arguments_) {
207 totalSize += argument.size();
208 }
209 Array result(totalSize);
210 for (const auto& argument : arguments_) {
211 Size size = argument.size();
212 Array partialParams(size);
213 for (Size j = 0; j < size; j++, k++)
214 partialParams[j] = params[k];
215 Array tmpBound = argument.constraint().lowerBound(params: partialParams);
216 for (Size j = 0; j < size; j++, k2++)
217 result[k2] = tmpBound[j];
218 }
219 return result;
220 }
221
222 private:
223 const std::vector<Parameter>& arguments_;
224 };
225 public:
226 explicit PrivateConstraint(const std::vector<Parameter>& arguments)
227 : Constraint(ext::shared_ptr<Constraint::Impl>(
228 new PrivateConstraint::Impl(arguments))) {}
229 };
230
231}
232
233#endif
234

source code of quantlib/ql/models/model.hpp

Morty Proxy This is a proxified and sanitized view of the page, visit original site.