Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
linkablecalibratedmodel.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file linkablecalibratedmodel.hpp
20 \brief calibrated model class with linkable parameters
21 \ingroup models
22*/
23
24#ifndef quantext_calibrated_model_hpp
25#define quantext_calibrated_model_hpp
26
27#include <ql/math/optimization/endcriteria.hpp>
28#include <ql/math/optimization/method.hpp>
29#include <ql/models/calibrationhelper.hpp>
30#include <ql/models/parameter.hpp>
31#include <ql/option.hpp>
32#include <ql/patterns/observable.hpp>
33
34namespace QuantExt {
35using namespace QuantLib;
36
37//! Calibrated model class with linkable parameters
38/*! \ingroup models
39 */
40class LinkableCalibratedModel : public virtual Observer, public virtual Observable {
41public:
43
44 void update() override {
46 notifyObservers();
47 }
48
49 //! Calibrate to a set of market instruments (usually caps/swaptions)
50 /*! An additional constraint can be passed which must be
51 satisfied in addition to the constraints of the model.
52 */
53 virtual void calibrate(const std::vector<QuantLib::ext::shared_ptr<CalibrationHelper> >&, OptimizationMethod& method,
54 const EndCriteria& endCriteria, const Constraint& constraint = Constraint(),
55 const std::vector<Real>& weights = std::vector<Real>(),
56 const std::vector<bool>& fixParameters = std::vector<bool>());
57
58 //! for backward compatibility
59 virtual void calibrate(const std::vector<QuantLib::ext::shared_ptr<BlackCalibrationHelper> >&, OptimizationMethod& method,
60 const EndCriteria& endCriteria, const Constraint& constraint = Constraint(),
61 const std::vector<Real>& weights = std::vector<Real>(),
62 const std::vector<bool>& fixParameters = std::vector<bool>());
63
64 Real value(const Array& params, const std::vector<QuantLib::ext::shared_ptr<CalibrationHelper> >&);
65
66 //! for backward compatibility
67 Real value(const Array& params, const std::vector<QuantLib::ext::shared_ptr<BlackCalibrationHelper> >&);
68
69 const QuantLib::ext::shared_ptr<Constraint>& constraint() const;
70
71 //! Returns end criteria result
72 EndCriteria::Type endCriteria() const { return endCriteria_; }
73
74 //! Returns the problem values
75 const Array& problemValues() const { return problemValues_; }
76
77 //! Returns array of arguments on which calibration is done
78 Array params() const;
79
80 virtual void setParams(const Array& params);
81 virtual void setParam(Size idx, const Real value);
82
83protected:
84 virtual void generateArguments() {}
85 std::vector<QuantLib::ext::shared_ptr<Parameter> > arguments_;
86 QuantLib::ext::shared_ptr<Constraint> constraint_;
87 EndCriteria::Type endCriteria_;
89
90private:
91 //! Constraint imposed on arguments
93 //! Calibration cost function class
95 friend class CalibrationFunction;
96};
97
98//! Linkable Calibrated Model
99/*! \ingroup models
100 */
102private:
103 class Impl : public Constraint::Impl {
104 public:
105 Impl(const std::vector<QuantLib::ext::shared_ptr<Parameter> >& arguments) : arguments_(arguments) {}
106
107 bool test(const Array& params) const override {
108 Size k = 0;
109 for (Size i = 0; i < arguments_.size(); i++) {
110 Size size = arguments_[i]->size();
111 Array testParams(size);
112 for (Size j = 0; j < size; j++, k++)
113 testParams[j] = params[k];
114 if (!arguments_[i]->testParams(testParams))
115 return false;
116 }
117 return true;
118 }
119
120 Array upperBound(const Array& params) const override {
121 Size k = 0, k2 = 0;
122 Size totalSize = 0;
123 for (Size i = 0; i < arguments_.size(); i++) {
124 totalSize += arguments_[i]->size();
125 }
126 Array result(totalSize);
127 for (Size i = 0; i < arguments_.size(); i++) {
128 Size size = arguments_[i]->size();
129 Array partialParams(size);
130 for (Size j = 0; j < size; j++, k++)
131 partialParams[j] = params[k];
132 Array tmpBound = arguments_[i]->constraint().upperBound(partialParams);
133 for (Size j = 0; j < size; j++, k2++)
134 result[k2] = tmpBound[j];
135 }
136 return result;
137 }
138
139 Array lowerBound(const Array& params) const override {
140 Size k = 0, k2 = 0;
141 Size totalSize = 0;
142 for (Size i = 0; i < arguments_.size(); i++) {
143 totalSize += arguments_[i]->size();
144 }
145 Array result(totalSize);
146 for (Size i = 0; i < arguments_.size(); i++) {
147 Size size = arguments_[i]->size();
148 Array partialParams(size);
149 for (Size j = 0; j < size; j++, k++)
150 partialParams[j] = params[k];
151 Array tmpBound = arguments_[i]->constraint().lowerBound(partialParams);
152 for (Size j = 0; j < size; j++, k2++)
153 result[k2] = tmpBound[j];
154 }
155 return result;
156 }
157
158 private:
159 const std::vector<QuantLib::ext::shared_ptr<Parameter> >& arguments_;
160 };
161
162public:
163 PrivateConstraint(const std::vector<QuantLib::ext::shared_ptr<Parameter> >& arguments)
164 : Constraint(QuantLib::ext::shared_ptr<Constraint::Impl>(new PrivateConstraint::Impl(arguments))) {}
165};
166
167} // namespace QuantExt
168
169#endif
Impl(const std::vector< QuantLib::ext::shared_ptr< Parameter > > &arguments)
const std::vector< QuantLib::ext::shared_ptr< Parameter > > & arguments_
PrivateConstraint(const std::vector< QuantLib::ext::shared_ptr< Parameter > > &arguments)
Calibrated model class with linkable parameters.
const Array & problemValues() const
Returns the problem values.
virtual void calibrate(const std::vector< QuantLib::ext::shared_ptr< BlackCalibrationHelper > > &, 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 >())
for backward compatibility
const QuantLib::ext::shared_ptr< Constraint > & constraint() const
Array params() const
Returns array of arguments on which calibration is done.
EndCriteria::Type endCriteria() const
Returns end criteria result.
virtual void setParam(Size idx, const Real value)
virtual void calibrate(const std::vector< QuantLib::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)
std::vector< QuantLib::ext::shared_ptr< Parameter > > arguments_
QuantLib::ext::shared_ptr< Constraint > constraint_
Real value(const Array &params, const std::vector< QuantLib::ext::shared_ptr< CalibrationHelper > > &)
virtual void setParams(const Array &params)