QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
parameter.hpp
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
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
24#ifndef quantlib_interest_rate_modelling_parameter_hpp
25#define quantlib_interest_rate_modelling_parameter_hpp
26
27#include <ql/handle.hpp>
28#include <ql/math/optimization/constraint.hpp>
29#include <ql/qldefines.hpp>
30#include <utility>
31#include <vector>
32
33namespace QuantLib {
34
35 class YieldTermStructure;
36
38 class Parameter {
39 protected:
41 class Impl {
42 public:
43 virtual ~Impl() = default;
44 virtual Real value(const Array& params, Time t) const = 0;
45 };
46 ext::shared_ptr<Impl> impl_;
47 public:
50 const Array& params() const { return params_; }
51 void setParam(Size i, Real x) { params_[i] = x; }
52 bool testParams(const Array& params) const {
53 return constraint_.test(params);
54 }
55 Size size() const { return params_.size(); }
56 Real operator()(Time t) const {
57 return impl_->value(params_, t);
58 }
59 const ext::shared_ptr<Impl>& implementation() const {
60 return impl_;
61 }
62 const Constraint& constraint() const { return constraint_; }
63 protected:
64 Parameter(Size size, ext::shared_ptr<Impl> impl, Constraint constraint)
65 : impl_(std::move(impl)), params_(size), constraint_(std::move(constraint)) {}
68 };
69
72 private:
73 class Impl final : public Parameter::Impl {
74 public:
75 Real value(const Array& params, Time) const override { return params[0]; }
76 };
77 public:
79 : Parameter(
80 1,
81 ext::shared_ptr<Parameter::Impl>(new ConstantParameter::Impl),
83 {}
84
87 : Parameter(
88 1,
89 ext::shared_ptr<Parameter::Impl>(new ConstantParameter::Impl),
90 constraint) {
91 params_[0] = value;
92 QL_REQUIRE(testParams(params_),
93 value << ": invalid value");
94 }
95
96 };
97
99 class NullParameter : public Parameter {
100 private:
101 class Impl final : public Parameter::Impl {
102 public:
103 Real value(const Array&, Time) const override { return 0.0; }
104 };
105 public:
107 : Parameter(
108 0,
109 ext::shared_ptr<Parameter::Impl>(new NullParameter::Impl),
110 NoConstraint())
111 {}
112 };
113
115
120 private:
121 class Impl final : public Parameter::Impl {
122 public:
123 explicit Impl(std::vector<Time> times) : times_(std::move(times)) {}
124
125 Real value(const Array& params, Time t) const override {
126 Size size = times_.size();
127 for (Size i=0; i<size; i++) {
128 if (t<times_[i])
129 return params[i];
130 }
131 return params[size];
132 }
133
134 private:
135 std::vector<Time> times_;
136 };
137 public:
138 PiecewiseConstantParameter(const std::vector<Time>& times,
139 const Constraint& constraint =
140 NoConstraint())
141 : Parameter(times.size()+1,
142 ext::shared_ptr<Parameter::Impl>(
143 new PiecewiseConstantParameter::Impl(times)),
145 {}
146 };
147
150 public:
152 public:
154 : times_(0), values_(0), termStructure_(std::move(termStructure)) {}
155
156 void set(Time t, Real x) {
157 times_.push_back(t);
158 values_.push_back(x);
159 }
160 void change(Real x) {
161 values_.back() = x;
162 }
163 void reset() {
164 times_.clear();
165 values_.clear();
166 }
167 Real value(const Array&, Time t) const override {
168 auto result = std::find(times_.begin(), times_.end(), t);
169 QL_REQUIRE(result!=times_.end(),
170 "fitting parameter not set!");
171 return values_[result - times_.begin()];
172 }
174 return termStructure_;
175 }
176 private:
177 std::vector<Time> times_;
178 std::vector<Real> values_;
180 };
181
183 const ext::shared_ptr<Parameter::Impl>& impl)
184 : Parameter(0, impl, NoConstraint()) {}
185
187 : Parameter(
188 0,
189 ext::shared_ptr<Parameter::Impl>(new NumericalImpl(term)),
190 NoConstraint())
191 {}
192 };
193
194}
195
196
197#endif
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
Real value(const Array &params, Time) const override
Definition: parameter.hpp:75
Standard constant parameter .
Definition: parameter.hpp:71
ConstantParameter(const Constraint &constraint)
Definition: parameter.hpp:78
ConstantParameter(Real value, const Constraint &constraint)
Definition: parameter.hpp:85
Base constraint class.
Definition: constraint.hpp:35
bool test(const Array &p) const
Definition: constraint.hpp:57
Shared handle to an observable.
Definition: handle.hpp:41
No constraint.
Definition: constraint.hpp:79
Real value(const Array &, Time) const override
Definition: parameter.hpp:103
Parameter which is always zero
Definition: parameter.hpp:99
Base class for model parameter implementation.
Definition: parameter.hpp:41
virtual ~Impl()=default
virtual Real value(const Array &params, Time t) const =0
Base class for model arguments.
Definition: parameter.hpp:38
const Constraint & constraint() const
Definition: parameter.hpp:62
void setParam(Size i, Real x)
Definition: parameter.hpp:51
Parameter(Size size, ext::shared_ptr< Impl > impl, Constraint constraint)
Definition: parameter.hpp:64
const ext::shared_ptr< Impl > & implementation() const
Definition: parameter.hpp:59
Constraint constraint_
Definition: parameter.hpp:67
const Array & params() const
Definition: parameter.hpp:50
bool testParams(const Array &params) const
Definition: parameter.hpp:52
ext::shared_ptr< Impl > impl_
Definition: parameter.hpp:46
Size size() const
Definition: parameter.hpp:55
Real operator()(Time t) const
Definition: parameter.hpp:56
Real value(const Array &params, Time t) const override
Definition: parameter.hpp:125
Piecewise-constant parameter.
Definition: parameter.hpp:119
PiecewiseConstantParameter(const std::vector< Time > &times, const Constraint &constraint=NoConstraint())
Definition: parameter.hpp:138
const Handle< YieldTermStructure > & termStructure() const
Definition: parameter.hpp:173
Real value(const Array &, Time t) const override
Definition: parameter.hpp:167
NumericalImpl(Handle< YieldTermStructure > termStructure)
Definition: parameter.hpp:153
Deterministic time-dependent parameter used for yield-curve fitting.
Definition: parameter.hpp:149
TermStructureFittingParameter(const ext::shared_ptr< Parameter::Impl > &impl)
Definition: parameter.hpp:182
TermStructureFittingParameter(const Handle< YieldTermStructure > &term)
Definition: parameter.hpp:186
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.