| 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> |
| 28 | #include <ql/math/optimization/constraint.hpp> |
| 29 | #include <ql/qldefines.hpp> |
| 30 | #include <utility> |
| 31 | #include <vector> |
| 32 | |
| 33 | namespace 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: |
| 48 | Parameter() |
| 49 | : constraint_(NoConstraint()) {} |
| 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(p: params); |
| 54 | } |
| 55 | Size size() const { return params_.size(); } |
| 56 | Real operator()(Time t) const { |
| 57 | return impl_->value(params: 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)) {} |
| 66 | Array params_; |
| 67 | Constraint constraint_; |
| 68 | }; |
| 69 | |
| 70 | //! Standard constant parameter \f$ a(t) = a \f$ |
| 71 | class ConstantParameter : public Parameter { |
| 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: |
| 78 | ConstantParameter(const Constraint& constraint) |
| 79 | : Parameter( |
| 80 | 1, |
| 81 | ext::shared_ptr<Parameter::Impl>(new ConstantParameter::Impl), |
| 82 | constraint) |
| 83 | {} |
| 84 | |
| 85 | ConstantParameter(Real value, |
| 86 | const Constraint& constraint) |
| 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 | |
| 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: |
| 106 | NullParameter() |
| 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 | */ |
| 119 | class PiecewiseConstantParameter : public Parameter { |
| 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)), |
| 144 | constraint) |
| 145 | {} |
| 146 | }; |
| 147 | |
| 148 | //! Deterministic time-dependent parameter used for yield-curve fitting |
| 149 | class TermStructureFittingParameter : public Parameter { |
| 150 | public: |
| 151 | class NumericalImpl : public Parameter::Impl { |
| 152 | public: |
| 153 | NumericalImpl(Handle<YieldTermStructure> termStructure) |
| 154 | : times_(0), values_(0), termStructure_(std::move(termStructure)) {} |
| 155 | |
| 156 | void set(Time t, Real x) { |
| 157 | times_.push_back(x: t); |
| 158 | values_.push_back(x: 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(first: times_.begin(), last: times_.end(), val: t); |
| 169 | QL_REQUIRE(result!=times_.end(), |
| 170 | "fitting parameter not set!" ); |
| 171 | return values_[result - times_.begin()]; |
| 172 | } |
| 173 | const Handle<YieldTermStructure>& termStructure() const { |
| 174 | return termStructure_; |
| 175 | } |
| 176 | private: |
| 177 | std::vector<Time> times_; |
| 178 | std::vector<Real> values_; |
| 179 | Handle<YieldTermStructure> termStructure_; |
| 180 | }; |
| 181 | |
| 182 | TermStructureFittingParameter( |
| 183 | const ext::shared_ptr<Parameter::Impl>& impl) |
| 184 | : Parameter(0, impl, NoConstraint()) {} |
| 185 | |
| 186 | TermStructureFittingParameter(const Handle<YieldTermStructure>& term) |
| 187 | : Parameter( |
| 188 | 0, |
| 189 | ext::shared_ptr<Parameter::Impl>(new NumericalImpl(term)), |
| 190 | NoConstraint()) |
| 191 | {} |
| 192 | }; |
| 193 | |
| 194 | } |
| 195 | |
| 196 | |
| 197 | #endif |
| 198 | |