Coverage Report

Created: 2026-10-31 06:47

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