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
21#include <ql/math/optimization/problem.hpp>
22#include <ql/math/optimization/projectedconstraint.hpp>
23#include <ql/math/optimization/projection.hpp>
24#include <ql/models/model.hpp>
25#include <ql/utilities/null_deleter.hpp>
26#include <utility>
27
28using std::vector;
29
30namespace QuantLib {
31
32 CalibratedModel::CalibratedModel(Size nArguments)
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(projectedParameters: 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(x: value);
54 }
55
56 Array values(const Array& params) const override {
57 model_->setParams(projection_.include(projectedParameters: params));
58 Array values(instruments_.size());
59 for (Size i=0; i<instruments_.size(); i++) {
60 values[i] = instruments_[i]->calibrationError()
61 *std::sqrt(x: 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
75 void CalibratedModel::calibrate(
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(parameters: prms));
108 shortRateEndCriteria_ = method.minimize(P&: prob, endCriteria);
109 Array result(prob.currentValue());
110 setParams(proj.include(projectedParameters: result));
111 problemValues_ = prob.values(x: result);
112 functionEvaluation_ = prob.functionEvaluation();
113
114 notifyObservers();
115 }
116
117 Real CalibratedModel::value(
118 const Array& params,
119 const vector<ext::shared_ptr<CalibrationHelper> >& instruments) {
120 vector<Real> w = vector<Real>(instruments.size(), 1.0);
121 Projection p(params);
122 CalibrationFunction f(this, instruments, w, p);
123 return f.value(params);
124 }
125
126 Array CalibratedModel::params() const {
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) {
139 Array::const_iterator p = params.begin();
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(i: j, x: *p);
144 }
145 }
146 QL_REQUIRE(p==params.end(),"parameter array too big!");
147 generateArguments();
148 notifyObservers();
149 }
150
151 ShortRateModel::ShortRateModel(Size nArguments)
152 : CalibratedModel(nArguments) {}
153
154}
155

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