QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
parameter.hpp
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
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
20/*! \file parameter.hpp
21 \brief Model parameter classes
22*/
23
24#ifndef quantlib_interest_rate_modelling_parameter_hpp
25#define quantlib_interest_rate_modelling_parameter_hpp
26
27#include <ql/handle.hpp>
29#include <ql/qldefines.hpp>
30#include <utility>
31#include <vector>
32
33namespace QuantLib {
34
35 class YieldTermStructure;
36
37 //! Base class for model arguments
38 class Parameter {
39 protected:
40 //! Base class for model parameter implementation
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(); }
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
70 //! Standard constant parameter \f$ a(t) = a \f$
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;
93 value << ": invalid value");
94 }
95
96 };
97
98 //! %Parameter which is always zero \f$ a(t) = 0 \f$
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
114 //! Piecewise-constant parameter
115 /*! \f$ a(t) = a_i if t_{i-1} \geq t < t_i \f$.
116 This kind of parameter is usually used to enhance the fitting of a
117 model
118 */
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 i = std::upper_bound(times_.begin(), times_.end(), t) - times_.begin();
127 return params[i];
128 }
129
130 private:
131 std::vector<Time> times_;
132 };
133 public:
134 PiecewiseConstantParameter(const std::vector<Time>& times,
135 const Constraint& constraint =
136 NoConstraint())
137 : Parameter(times.size()+1,
138 ext::shared_ptr<Parameter::Impl>(
139 new PiecewiseConstantParameter::Impl(times)),
141 {}
142 };
143
144 //! Deterministic time-dependent parameter used for yield-curve fitting
146 public:
148 public:
150 : times_(0), values_(0), termStructure_(std::move(termStructure)) {}
151
152 void set(Time t, Real x) {
153 times_.push_back(t);
154 values_.push_back(x);
155 }
156 void change(Real x) {
157 values_.back() = x;
158 }
159 void reset() {
160 times_.clear();
161 values_.clear();
162 }
163 Real value(const Array&, Time t) const override {
164 auto result = std::find(times_.begin(), times_.end(), t);
165 QL_REQUIRE(result!=times_.end(),
166 "fitting parameter not set!");
167 return values_[result - times_.begin()];
168 }
170 return termStructure_;
171 }
172 private:
173 std::vector<Time> times_;
174 std::vector<Real> values_;
176 };
177
179 const ext::shared_ptr<Parameter::Impl>& impl)
180 : Parameter(0, impl, NoConstraint()) {}
181
183 : Parameter(
184 0,
185 ext::shared_ptr<Parameter::Impl>(new NumericalImpl(term)),
186 NoConstraint())
187 {}
188 };
189
190}
191
192
193#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:134
const Handle< YieldTermStructure > & termStructure() const
Definition: parameter.hpp:169
Real value(const Array &, Time t) const override
Definition: parameter.hpp:163
NumericalImpl(Handle< YieldTermStructure > termStructure)
Definition: parameter.hpp:149
Deterministic time-dependent parameter used for yield-curve fitting.
Definition: parameter.hpp:145
TermStructureFittingParameter(const ext::shared_ptr< Parameter::Impl > &impl)
Definition: parameter.hpp:178
TermStructureFittingParameter(const Handle< YieldTermStructure > &term)
Definition: parameter.hpp:182
Abstract constraint class.
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
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
Globally accessible relinkable pointer.
Definition: any.hpp:35
STL namespace.
Global definitions and compiler switches.