| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2006, 2007, 2015 Ferdinando Ametrano |
| 5 | Copyright (C) 2006 Cristina Duminuco |
| 6 | Copyright (C) 2007 Giorgio Facchinetti |
| 7 | Copyright (C) 2015 Paolo Mazzocchi |
| 8 | |
| 9 | This file is part of QuantLib, a free-software/open-source library |
| 10 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 11 | |
| 12 | QuantLib is free software: you can redistribute it and/or modify it |
| 13 | under the terms of the QuantLib license. You should have received a |
| 14 | copy of the license along with this program; if not, please email |
| 15 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 16 | <http://quantlib.org/license.shtml>. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 19 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 20 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 21 | */ |
| 22 | |
| 23 | #ifndef quantlib_abcd_math_function_hpp |
| 24 | #define quantlib_abcd_math_function_hpp |
| 25 | |
| 26 | #include <ql/types.hpp> |
| 27 | #include <ql/errors.hpp> |
| 28 | #include <vector> |
| 29 | |
| 30 | namespace QuantLib { |
| 31 | |
| 32 | //! %Abcd functional form |
| 33 | /*! \f[ f(t) = [ a + b*t ] e^{-c*t} + d \f] |
| 34 | following Rebonato's notation. */ |
| 35 | class AbcdMathFunction { |
| 36 | |
| 37 | public: |
| 38 | /*! \deprecated Use `auto` or `decltype` instead. |
| 39 | Deprecated in version 1.29. |
| 40 | */ |
| 41 | QL_DEPRECATED |
| 42 | typedef Time argument_type; |
| 43 | |
| 44 | /*! \deprecated Use `auto` or `decltype` instead. |
| 45 | Deprecated in version 1.29. |
| 46 | */ |
| 47 | QL_DEPRECATED |
| 48 | typedef Real result_type; |
| 49 | |
| 50 | AbcdMathFunction(Real a = 0.002, |
| 51 | Real b = 0.001, |
| 52 | Real c = 0.16, |
| 53 | Real d = 0.0005); |
| 54 | AbcdMathFunction(std::vector<Real> abcd); |
| 55 | |
| 56 | //! function value at time t: \f[ f(t) \f] |
| 57 | Real operator()(Time t) const; |
| 58 | |
| 59 | //! time at which the function reaches maximum (if any) |
| 60 | Time maximumLocation() const; |
| 61 | |
| 62 | //! maximum value of the function |
| 63 | Real maximumValue() const; |
| 64 | |
| 65 | //! function value at time +inf: \f[ f(\inf) \f] |
| 66 | Real longTermValue() const { return d_; } |
| 67 | |
| 68 | /*! first derivative of the function at time t |
| 69 | \f[ f'(t) = [ (b-c*a) + (-c*b)*t) ] e^{-c*t} \f] */ |
| 70 | Real derivative(Time t) const; |
| 71 | |
| 72 | /*! indefinite integral of the function at time t |
| 73 | \f[ \int f(t)dt = [ (-a/c-b/c^2) + (-b/c)*t ] e^{-c*t} + d*t \f] */ |
| 74 | Real primitive(Time t) const; |
| 75 | |
| 76 | /*! definite integral of the function between t1 and t2 |
| 77 | \f[ \int_{t1}^{t2} f(t)dt \f] */ |
| 78 | Real definiteIntegral(Time t1, Time t2) const; |
| 79 | |
| 80 | /*! Inspectors */ |
| 81 | Real a() const { return a_; } |
| 82 | Real b() const { return b_; } |
| 83 | Real c() const { return c_; } |
| 84 | Real d() const { return d_; } |
| 85 | const std::vector<Real>& coefficients() { return abcd_; } |
| 86 | const std::vector<Real>& derivativeCoefficients() { return dabcd_; } |
| 87 | // the primitive is not abcd |
| 88 | |
| 89 | /*! coefficients of a AbcdMathFunction defined as definite |
| 90 | integral on a rolling window of length tau, with tau = t2-t */ |
| 91 | std::vector<Real> definiteIntegralCoefficients(Time t, |
| 92 | Time t2) const; |
| 93 | |
| 94 | /*! coefficients of a AbcdMathFunction defined as definite |
| 95 | derivative on a rolling window of length tau, with tau = t2-t */ |
| 96 | std::vector<Real> definiteDerivativeCoefficients(Time t, |
| 97 | Time t2) const; |
| 98 | |
| 99 | static void validate(Real a, |
| 100 | Real b, |
| 101 | Real c, |
| 102 | Real d); |
| 103 | protected: |
| 104 | Real a_, b_, c_, d_; |
| 105 | private: |
| 106 | void initialize_(); |
| 107 | std::vector<Real> abcd_; |
| 108 | std::vector<Real> dabcd_; |
| 109 | Real da_, db_; |
| 110 | Real pa_, pb_, K_; |
| 111 | |
| 112 | Real dibc_, diacplusbcc_; |
| 113 | }; |
| 114 | |
| 115 | // inline AbcdMathFunction |
| 116 | inline Real AbcdMathFunction::operator()(Time t) const { |
| 117 | //return (a_ + b_*t)*std::exp(-c_*t) + d_; |
| 118 | return t<0 ? 0.0 : Real((a_ + b_*t)*std::exp(x: -c_*t) + d_); |
| 119 | } |
| 120 | |
| 121 | inline Real AbcdMathFunction::derivative(Time t) const { |
| 122 | //return (da_ + db_*t)*std::exp(-c_*t); |
| 123 | return t<0 ? 0.0 : Real((da_ + db_*t)*std::exp(x: -c_*t)); |
| 124 | } |
| 125 | |
| 126 | inline Real AbcdMathFunction::primitive(Time t) const { |
| 127 | //return (pa_ + pb_*t)*std::exp(-c_*t) + d_*t + K_; |
| 128 | return t<0 ? 0.0 : Real((pa_ + pb_*t)*std::exp(x: -c_*t) + d_*t + K_); |
| 129 | } |
| 130 | |
| 131 | inline Real AbcdMathFunction::maximumValue() const { |
| 132 | if (b_==0.0 || a_<=0.0) |
| 133 | return d_; |
| 134 | return (*this)(maximumLocation()); |
| 135 | } |
| 136 | |
| 137 | } |
| 138 | |
| 139 | #endif |
| 140 | |