Coverage Report

Created: 2025-10-14 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/math/abcdmathfunction.hpp
Line
Count
Source
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
 <https://www.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
        AbcdMathFunction(Real a = 0.002,
39
                         Real b = 0.001, 
40
                         Real c = 0.16,
41
                         Real d = 0.0005);
42
        AbcdMathFunction(std::vector<Real> abcd);
43
44
        //! function value at time t: \f[ f(t) \f]
45
        Real operator()(Time t) const;
46
47
        //! time at which the function reaches maximum (if any)
48
        Time maximumLocation() const;
49
50
        //! maximum value of the function
51
        Real maximumValue() const;
52
53
        //! function value at time +inf: \f[ f(\inf) \f]
54
0
        Real longTermValue() const { return d_; }
55
56
        /*! first derivative of the function at time t
57
            \f[ f'(t) = [ (b-c*a) + (-c*b)*t) ] e^{-c*t} \f] */
58
        Real derivative(Time t) const;
59
        
60
        /*! indefinite integral of the function at time t
61
            \f[ \int f(t)dt = [ (-a/c-b/c^2) + (-b/c)*t ] e^{-c*t} + d*t \f] */
62
        Real primitive(Time t) const;
63
        
64
        /*! definite integral of the function between t1 and t2
65
            \f[ \int_{t1}^{t2} f(t)dt \f] */
66
        Real definiteIntegral(Time t1, Time t2) const;
67
68
        /*! Inspectors */
69
0
        Real a() const { return a_; }
70
0
        Real b() const { return b_; }
71
0
        Real c() const { return c_; }
72
0
        Real d() const { return d_; }
73
0
        const std::vector<Real>& coefficients() { return abcd_; }
74
0
        const std::vector<Real>& derivativeCoefficients() { return dabcd_; }
75
        // the primitive is not abcd
76
77
        /*! coefficients of a AbcdMathFunction defined as definite
78
            integral on a rolling window of length tau, with tau = t2-t */
79
        std::vector<Real> definiteIntegralCoefficients(Time t,
80
                                                       Time t2) const;
81
82
        /*! coefficients of a AbcdMathFunction defined as definite
83
            derivative on a rolling window of length tau, with tau = t2-t */
84
        std::vector<Real> definiteDerivativeCoefficients(Time t,
85
                                                         Time t2) const;
86
87
        static void validate(Real a,
88
                             Real b,
89
                             Real c,
90
                             Real d);
91
      protected:
92
        Real a_, b_, c_, d_;
93
      private:
94
        void initialize_();
95
        std::vector<Real> abcd_;
96
        std::vector<Real> dabcd_;
97
        Real da_, db_;
98
        Real pa_, pb_, K_;
99
100
        Real dibc_, diacplusbcc_;
101
    };
102
103
    // inline AbcdMathFunction
104
0
    inline Real AbcdMathFunction::operator()(Time t) const {
105
        //return (a_ + b_*t)*std::exp(-c_*t) + d_;
106
0
        return t<0 ? 0.0 : Real((a_ + b_*t)*std::exp(-c_*t) + d_);
107
0
    }
108
109
0
    inline Real AbcdMathFunction::derivative(Time t) const {
110
0
        //return (da_ + db_*t)*std::exp(-c_*t);
111
0
        return t<0 ? 0.0 : Real((da_ + db_*t)*std::exp(-c_*t));
112
0
    }
113
114
0
    inline Real AbcdMathFunction::primitive(Time t) const {
115
        //return (pa_ + pb_*t)*std::exp(-c_*t) + d_*t + K_;
116
0
        return t<0 ? 0.0 : Real((pa_ + pb_*t)*std::exp(-c_*t) + d_*t + K_);
117
0
    }
118
119
0
    inline Real AbcdMathFunction::maximumValue() const {
120
0
        if (b_==0.0 || a_<=0.0)
121
0
            return d_;
122
0
        return (*this)(maximumLocation());
123
0
    }
124
125
}
126
127
#endif