QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
model.cpp
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) 2013, 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 <http://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
24#include <ql/models/model.hpp>
26#include <utility>
27
28using std::vector;
29
30namespace QuantLib {
31
33 : arguments_(nArguments), constraint_(new PrivateConstraint(arguments_)) {}
34
35 class CalibratedModel::CalibrationFunction : public CostFunction {
36 public:
37 CalibrationFunction(CalibratedModel* model,
38 const vector<ext::shared_ptr<CalibrationHelper> >& h,
39 vector<Real> weights,
40 const Projection& projection)
41 : model_(model, null_deleter()), instruments_(h), weights_(std::move(weights)),
42 projection_(projection) {}
43
44 ~CalibrationFunction() override = default;
45
46 Real value(const Array& params) const override {
47 model_->setParams(projection_.include(params));
48 Real value = 0.0;
49 for (Size i=0; i<instruments_.size(); i++) {
50 Real diff = instruments_[i]->calibrationError();
51 value += diff*diff*weights_[i];
52 }
53 return std::sqrt(value);
54 }
55
56 Array values(const Array& params) const override {
57 model_->setParams(projection_.include(params));
58 Array values(instruments_.size());
59 for (Size i=0; i<instruments_.size(); i++) {
60 values[i] = instruments_[i]->calibrationError()
61 *std::sqrt(weights_[i]);
62 }
63 return values;
64 }
65
66 Real finiteDifferenceEpsilon() const override { return 1e-6; }
67
68 private:
69 ext::shared_ptr<CalibratedModel> model_;
70 const vector<ext::shared_ptr<CalibrationHelper> >& instruments_;
71 vector<Real> weights_;
72 const Projection projection_;
73 };
74
76 const vector<ext::shared_ptr<CalibrationHelper> >& instruments,
77 OptimizationMethod& method,
78 const EndCriteria& endCriteria,
79 const Constraint& additionalConstraint,
80 const vector<Real>& weights,
81 const vector<bool>& fixParameters) {
82
83 QL_REQUIRE(!instruments.empty(), "no instruments provided");
84
85 Constraint c;
86 if (additionalConstraint.empty())
87 c = *constraint_;
88 else
89 c = CompositeConstraint(*constraint_,additionalConstraint);
90
91 QL_REQUIRE(weights.empty() || weights.size() == instruments.size(),
92 "mismatch between number of instruments (" <<
93 instruments.size() << ") and weights (" <<
94 weights.size() << ")");
95 vector<Real> w =
96 weights.empty() ? vector<Real>(instruments.size(), 1.0): weights;
97
98 Array prms = params();
99 QL_REQUIRE(fixParameters.empty() || fixParameters.size() == prms.size(),
100 "mismatch between number of parameters (" <<
101 prms.size() << ") and fixed-parameter specs (" <<
102 fixParameters.size() << ")");
103 vector<bool> all(prms.size(), false);
104 Projection proj(prms, !fixParameters.empty() ? fixParameters : all);
105 CalibrationFunction f(this,instruments,w,proj);
106 ProjectedConstraint pc(c,proj);
107 Problem prob(f, pc, proj.project(prms));
109 Array result(prob.currentValue());
110 setParams(proj.include(result));
111 problemValues_ = prob.values(result);
113
115 }
116
118 const Array& params,
119 const vector<ext::shared_ptr<CalibrationHelper> >& instruments) {
120 vector<Real> w = vector<Real>(instruments.size(), 1.0);
122 CalibrationFunction f(this, instruments, w, p);
123 return f.value(params);
124 }
125
127 Size size=0;
128 for (const auto& argument : arguments_)
129 size += argument.size();
130 Array params(size);
131 for (Size i=0, k=0; i<arguments_.size(); ++i) {
132 for (Size j=0; j<arguments_[i].size(); ++j, ++k)
133 params[k] = arguments_[i].params()[j];
134 }
135 return params;
136 }
137
138 void CalibratedModel::setParams(const Array& params) {
140 for (auto& argument : arguments_) {
141 for (Size j = 0; j < argument.size(); ++j, ++p) {
142 QL_REQUIRE(p!=params.end(),"parameter array too small");
143 argument.setParam(j, *p);
144 }
145 }
146 QL_REQUIRE(p==params.end(),"parameter array too big!");
149 }
150
152 : CalibratedModel(nArguments) {}
153
154}
1-D array used in linear algebra.
Definition: array.hpp:52
const Real * const_iterator
Definition: array.hpp:124
bool empty() const
whether the array is empty
Definition: array.hpp:499
const_iterator end() const
Definition: array.hpp:511
Size size() const
dimension of the array
Definition: array.hpp:495
const_iterator begin() const
Definition: array.hpp:503
Calibrated model class.
Definition: model.hpp:86
virtual void generateArguments()
Definition: model.hpp:125
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
EndCriteria::Type shortRateEndCriteria_
Definition: model.hpp:128
ext::shared_ptr< Constraint > constraint_
Definition: model.hpp:127
std::vector< Parameter > arguments_
Definition: model.hpp:126
CalibratedModel(Size nArguments)
Definition: model.cpp:32
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
Constraint enforcing both given sub-constraints
Definition: constraint.hpp:140
Base constraint class.
Definition: constraint.hpp:35
bool empty() const
Definition: constraint.hpp:56
Cost function abstract class for optimization problem.
Criteria to end optimization process:
Definition: endcriteria.hpp:40
Abstract class for constrained optimization method.
Definition: method.hpp:36
virtual EndCriteria::Type minimize(Problem &P, const EndCriteria &endCriteria)=0
minimize the optimization problem P
Constrained optimization problem.
Definition: problem.hpp:42
const Array & currentValue() const
current value of the local minimum
Definition: problem.hpp:81
Integer functionEvaluation() const
number of evaluation of cost function
Definition: problem.hpp:97
Array values(const Array &x)
call cost values computation and increment evaluation counter
Definition: problem.hpp:121
virtual Array include(const Array &projectedParameters) const
returns whole set of parameters corresponding to the set
Definition: projection.cpp:67
virtual Array project(const Array &parameters) const
returns the subset of free parameters corresponding
Definition: projection.cpp:54
ShortRateModel(Size nArguments)
Definition: model.cpp:151
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Abstract interest rate model class.
Definition: any.hpp:35
STL namespace.
empty deleter for shared_ptr
Abstract optimization problem class.
Projected constraint.
Parameter projection.