19#include <ql/math/optimization/problem.hpp>
20#include <ql/math/optimization/projectedconstraint.hpp>
21#include <ql/math/optimization/projection.hpp>
22#include <ql/utilities/null_deleter.hpp>
25using QuantLib::ext::shared_ptr;
33class LinkableCalibratedModel::CalibrationFunction :
public CostFunction {
36 const vector<Real>& weights,
const Projection& projection)
37 : model_(model, null_deleter()), instruments_(h), weights_(weights), projection_(projection) {}
39 virtual ~CalibrationFunction() {}
41 virtual Real value(
const Array&
params)
const override {
42 model_->setParams(projection_.include(
params));
44 for (Size i = 0; i < instruments_.size(); i++) {
45 Real diff = instruments_[i]->calibrationError();
46 value += diff * diff * weights_[i];
48 return std::sqrt(value);
51 virtual Array values(
const Array&
params)
const override {
52 model_->setParams(projection_.include(
params));
53 Array values(instruments_.size());
54 for (Size i = 0; i < instruments_.size(); i++) {
55 values[i] = instruments_[i]->calibrationError() * std::sqrt(weights_[i]);
60 virtual Real finiteDifferenceEpsilon()
const override {
return 1e-6; }
63 shared_ptr<LinkableCalibratedModel> model_;
64 const vector<shared_ptr<CalibrationHelper> >& instruments_;
65 vector<Real> weights_;
66 const Projection projection_;
71 const Constraint& additionalConstraint,
const vector<Real>& weights,
72 const vector<bool>& fixParameters) {
73 vector<QuantLib::ext::shared_ptr<CalibrationHelper> > tmp(instruments.size());
74 for (Size i = 0; i < instruments.size(); ++i)
75 tmp[i] = ext::static_pointer_cast<CalibrationHelper>(instruments[i]);
81 const Constraint& additionalConstraint,
const vector<Real>& weights,
82 const vector<bool>& fixParameters) {
84 QL_REQUIRE(weights.empty() || weights.size() == instruments.size(),
"mismatch between number of instruments ("
85 << instruments.size() <<
") and weights("
86 << weights.size() <<
")");
89 if (additionalConstraint.empty())
92 c = CompositeConstraint(*
constraint_, additionalConstraint);
93 vector<Real> w = weights.empty() ? vector<Real>(instruments.size(), 1.0) : weights;
96 vector<bool> all(prms.size(),
false);
97 Projection proj(prms, fixParameters.size() > 0 ? fixParameters : all);
99 ProjectedConstraint pc(c, proj);
100 Problem prob(f, pc, proj.project(prms));
102 Array result(prob.currentValue());
110 const vector<QuantLib::ext::shared_ptr<BlackCalibrationHelper> >& instruments) {
111 vector<ext::shared_ptr<CalibrationHelper> > tmp(instruments.size());
112 for (Size i = 0; i < instruments.size(); ++i)
113 tmp[i] = ext::static_pointer_cast<CalibrationHelper>(instruments[i]);
118 vector<Real> w = vector<Real>(instruments.size(), 1.0);
120 CalibrationFunction f(
this, instruments, w, p);
131 for (Size j = 0; j <
arguments_[i]->size(); j++, k++) {
139 Array::const_iterator p =
params.begin();
140 for (Size i = 0; i <
arguments_.size(); ++i) {
141 for (Size j = 0; j <
arguments_[i]->size(); ++j, ++p) {
142 QL_REQUIRE(p !=
params.end(),
"parameter array too small");
146 QL_REQUIRE(p ==
params.end(),
"parameter array too big!");
152 int tmp =
static_cast<int>(idx);
153 for (Size i = 0; i <
arguments_.size(); ++i) {
154 for (Size j = 0; j <
arguments_[i]->size(); ++j) {
Linkable Calibrated Model.
Calibrated model class with linkable parameters.
virtual void generateArguments()
Array params() const
Returns array of arguments on which calibration is done.
EndCriteria::Type endCriteria() const
Returns end criteria result.
friend class CalibrationFunction
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_
LinkableCalibratedModel()
EndCriteria::Type endCriteria_
Real value(const Array ¶ms, const std::vector< QuantLib::ext::shared_ptr< CalibrationHelper > > &)
virtual void setParams(const Array ¶ms)
calibrated model class with linkable parameters
Swap::arguments * arguments_