QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
model.hpp
Go to the documentation of this file.
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
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
55 Real strike,
56 Time maturity,
57 Time bondMaturity) const = 0;
58
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 */
74 public:
78 return termStructure_;
79 }
80 private:
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 {
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,
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
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);
123
124 protected:
125 virtual void generateArguments() {}
126 std::vector<Parameter> arguments_;
127 ext::shared_ptr<Constraint> constraint_;
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 */
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
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>&
161 return constraint_;
162 }
163
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(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(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(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
Calibration helper class.
Affine model class.
Definition: model.hpp:45
virtual Real discountBondOption(Option::Type type, Real strike, Time maturity, Time bondMaturity) const =0
virtual DiscountFactor discount(Time t) const =0
Implied discount curve.
virtual Real discountBond(Time now, Time maturity, Array factors) const =0
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
bool test(const Array &params) const override
Tests if params satisfy the constraint.
Definition: model.hpp:171
Array lowerBound(const Array &params) const override
Returns lower bound for given parameters.
Definition: model.hpp:203
Array upperBound(const Array &params) const override
Returns upper bound for given parameters.
Definition: model.hpp:184
Impl(const std::vector< Parameter > &arguments)
Definition: model.hpp:168
const std::vector< Parameter > & arguments_
Definition: model.hpp:223
PrivateConstraint(const std::vector< Parameter > &arguments)
Definition: model.hpp:226
Calibrated model class.
Definition: model.hpp:86
const Array & problemValues() const
Returns the problem values.
Definition: model.hpp:116
virtual void generateArguments()
Definition: model.hpp:125
void update() override
Definition: model.hpp:90
Array params() const
Returns array of arguments on which calibration is done.
Definition: model.cpp:126
EndCriteria::Type endCriteria() const
Returns end criteria result.
Definition: model.hpp:113
Real value(const Array &params, const std::vector< ext::shared_ptr< CalibrationHelper > > &)
Definition: model.cpp:117
Integer functionEvaluation() const
Definition: model.hpp:122
EndCriteria::Type shortRateEndCriteria_
Definition: model.hpp:128
ext::shared_ptr< Constraint > constraint_
Definition: model.hpp:127
std::vector< Parameter > arguments_
Definition: model.hpp:126
const ext::shared_ptr< Constraint > & constraint() const
Definition: model.hpp:160
virtual void calibrate(const std::vector< ext::shared_ptr< CalibrationHelper > > &, OptimizationMethod &method, const EndCriteria &endCriteria, const Constraint &constraint=Constraint(), const std::vector< Real > &weights=std::vector< Real >(), const std::vector< bool > &fixParameters=std::vector< bool >())
Calibrate to a set of market instruments (usually caps/swaptions)
Definition: model.cpp:75
virtual void setParams(const Array &params)
Definition: model.cpp:138
Base class for constraint implementations.
Definition: constraint.hpp:38
Base constraint class.
Definition: constraint.hpp:35
Criteria to end optimization process:
Definition: endcriteria.hpp:40
Shared handle to an observable.
Definition: handle.hpp:41
Object that notifies its changes to a set of observers.
Definition: observable.hpp:62
Object that gets notified when a given observable changes.
Definition: observable.hpp:116
Abstract class for constrained optimization method.
Definition: method.hpp:36
Abstract short-rate model class.
Definition: model.hpp:141
virtual ext::shared_ptr< Lattice > tree(const TimeGrid &) const =0
Term-structure consistent model class.
Definition: model.hpp:73
TermStructureConsistentModel(Handle< YieldTermStructure > termStructure)
Definition: model.hpp:75
const Handle< YieldTermStructure > & termStructure() const
Definition: model.hpp:77
Handle< YieldTermStructure > termStructure_
Definition: model.hpp:81
time grid class
Definition: timegrid.hpp:43
const DefaultType & t
Optimization criteria class.
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
QL_INTEGER Integer
integer number
Definition: types.hpp:35
std::size_t Size
size of a container
Definition: types.hpp:58
Tree-based lattice-method class.
Definition: any.hpp:35
STL namespace.
Base option class.
Model parameter classes.