Coverage Report

Created: 2026-10-31 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/termstructures/yield/forwardcurve.hpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2005, 2006, 2007, 2008, 2009 StatPro Italia srl
5
 Copyright (C) 2009, 2015 Ferdinando Ametrano
6
 Copyright (C) 2015 Paolo Mazzocchi
7
8
 This file is part of QuantLib, a free-software/open-source library
9
 for financial quantitative analysts and developers - http://quantlib.org/
10
11
 QuantLib is free software: you can redistribute it and/or modify it
12
 under the terms of the QuantLib license.  You should have received a
13
 copy of the license along with this program; if not, please email
14
 <quantlib-dev@lists.sf.net>. The license is also available online at
15
 <https://www.quantlib.org/license.shtml>.
16
17
 This program is distributed in the hope that it will be useful, but WITHOUT
18
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 FOR A PARTICULAR PURPOSE.  See the license for more details.
20
*/
21
22
/*! \file forwardcurve.hpp
23
    \brief interpolated forward-rate structure
24
*/
25
26
#ifndef quantlib_forward_curve_hpp
27
#define quantlib_forward_curve_hpp
28
29
#include <ql/termstructures/yield/zeroyieldstructure.hpp>
30
#include <ql/termstructures/interpolatedcurve.hpp>
31
#include <ql/math/interpolations/backwardflatinterpolation.hpp>
32
#include <utility>
33
34
namespace QuantLib {
35
36
    //! YieldTermStructure based on interpolation of forward rates
37
    /*! \ingroup yieldtermstructures */
38
    template <class Interpolator>
39
    class InterpolatedForwardCurve : public ZeroYieldStructure,
40
                                     protected InterpolatedCurve<Interpolator>,
41
                                     public InterpolatedNodes {
42
      public:
43
        // constructor
44
        InterpolatedForwardCurve(
45
            const std::vector<Date>& dates,
46
            const std::vector<Rate>& forwards,
47
            const DayCounter& dayCounter,
48
            const Calendar& cal = Calendar(),
49
            const std::vector<Handle<Quote> >& jumps = {},
50
            const std::vector<Date>& jumpDates = {},
51
            const Interpolator& interpolator = {});
52
        InterpolatedForwardCurve(
53
            const std::vector<Date>& dates,
54
            const std::vector<Rate>& forwards,
55
            const DayCounter& dayCounter,
56
            const Calendar& calendar,
57
            const Interpolator& interpolator);
58
        InterpolatedForwardCurve(
59
            const std::vector<Date>& dates,
60
            const std::vector<Rate>& forwards,
61
            const DayCounter& dayCounter,
62
            const Interpolator& interpolator);
63
        //! \name TermStructure interface
64
        //@{
65
        Date maxDate() const override;
66
        //@}
67
        //! \name other inspectors
68
        //@{
69
        const std::vector<Time>& times() const override;
70
        const std::vector<Date>& dates() const override;
71
        const std::vector<Real>& data() const override;
72
        const std::vector<Rate>& forwards() const;
73
        //@}
74
75
      protected:
76
        explicit InterpolatedForwardCurve(
77
            const DayCounter&,
78
            const Interpolator& interpolator = {});
79
        InterpolatedForwardCurve(
80
            const Date& referenceDate,
81
            const DayCounter&,
82
            const std::vector<Handle<Quote> >& jumps = {},
83
            const std::vector<Date>& jumpDates = {},
84
            const Interpolator& interpolator = {});
85
        InterpolatedForwardCurve(
86
            Natural settlementDays,
87
            const Calendar&,
88
            const DayCounter&,
89
            const std::vector<Handle<Quote> >& jumps = {},
90
            const std::vector<Date>& jumpDates = {},
91
            const Interpolator& interpolator = {});
92
93
        //! \name ZeroYieldStructure implementation
94
        //@{
95
        Rate zeroYieldImpl(Time t) const override;
96
        //@}
97
        mutable std::vector<Date> dates_;
98
      private:
99
        void initialize();
100
    };
101
102
    //! Term structure based on flat interpolation of forward rates
103
    /*! \ingroup yieldtermstructures */
104
105
    typedef InterpolatedForwardCurve<BackwardFlat> ForwardCurve;
106
107
108
    // inline definitions
109
110
    template <class T>
111
    inline Date InterpolatedForwardCurve<T>::maxDate() const {
112
        if (this->maxDate_ != Date())
113
           return this->maxDate_;
114
        return dates_.back();
115
    }
116
117
    template <class T>
118
    inline const std::vector<Time>&
119
    InterpolatedForwardCurve<T>::times() const {
120
        return this->times_;
121
    }
122
123
    template <class T>
124
    inline const std::vector<Date>&
125
0
    InterpolatedForwardCurve<T>::dates() const {
126
0
        return dates_;
127
0
    }
Unexecuted instantiation: QuantLib::InterpolatedForwardCurve<QuantLib::BackwardFlat>::dates() const
Unexecuted instantiation: QuantLib::InterpolatedForwardCurve<QuantLib::ForwardFlat>::dates() const
128
129
    template <class T>
130
    inline const std::vector<Real>&
131
    InterpolatedForwardCurve<T>::data() const {
132
        return this->data_;
133
    }
134
135
    template <class T>
136
    inline const std::vector<Rate>&
137
    InterpolatedForwardCurve<T>::forwards() const {
138
        return this->data_;
139
    }
140
141
    #ifndef __DOXYGEN__
142
143
    // template definitions
144
145
    template <class T>
146
    Rate InterpolatedForwardCurve<T>::zeroYieldImpl(Time t) const {
147
        if (t == 0.0)
148
            return this->interpolation_(t, true);
149
150
        const Time maxTime = this->times_.back();
151
        Real integral;
152
        if (t <= maxTime) {
153
            integral = this->interpolation_.primitive(t, true);
154
        } else {
155
            // flat fwd extrapolation
156
            integral = this->interpolation_.primitive(maxTime, true)
157
                     + this->data_.back()*(t - maxTime);
158
        }
159
        return integral/t;
160
    }
161
162
    template <class T>
163
    InterpolatedForwardCurve<T>::InterpolatedForwardCurve(
164
                                    const DayCounter& dayCounter,
165
                                    const T& interpolator)
166
    : ZeroYieldStructure(dayCounter), InterpolatedCurve<T>(interpolator) {}
167
168
    template <class T>
169
    InterpolatedForwardCurve<T>::InterpolatedForwardCurve(
170
                                    const Date& referenceDate,
171
                                    const DayCounter& dayCounter,
172
                                    const std::vector<Handle<Quote> >& jumps,
173
                                    const std::vector<Date>& jumpDates,
174
                                    const T& interpolator)
175
    : ZeroYieldStructure(referenceDate, Calendar(), dayCounter, jumps, jumpDates),
176
      InterpolatedCurve<T>(interpolator) {}
177
178
    template <class T>
179
    InterpolatedForwardCurve<T>::InterpolatedForwardCurve(
180
                                    Natural settlementDays,
181
                                    const Calendar& calendar,
182
                                    const DayCounter& dayCounter,
183
                                    const std::vector<Handle<Quote> >& jumps,
184
                                    const std::vector<Date>& jumpDates,
185
                                    const T& interpolator)
186
    : ZeroYieldStructure(settlementDays, calendar, dayCounter, jumps, jumpDates),
187
      InterpolatedCurve<T>(interpolator) {}
188
189
    template <class T>
190
    InterpolatedForwardCurve<T>::InterpolatedForwardCurve(
191
                                    const std::vector<Date>& dates,
192
                                    const std::vector<Rate>& forwards,
193
                                    const DayCounter& dayCounter,
194
                                    const Calendar& calendar,
195
                                    const std::vector<Handle<Quote> >& jumps,
196
                                    const std::vector<Date>& jumpDates,
197
                                    const T& interpolator)
198
    : ZeroYieldStructure(dates.at(0), calendar, dayCounter, jumps, jumpDates),
199
      InterpolatedCurve<T>(std::vector<Time>(), forwards, interpolator),
200
      dates_(dates)
201
    {
202
        initialize();
203
    }
204
205
    template <class T>
206
    InterpolatedForwardCurve<T>::InterpolatedForwardCurve(
207
            const std::vector<Date>& dates,
208
            const std::vector<Rate>& forwards,
209
            const DayCounter& dayCounter,
210
            const Calendar& calendar,
211
            const T& interpolator)
212
    : ZeroYieldStructure(dates.at(0), calendar, dayCounter),
213
      InterpolatedCurve<T>(std::vector<Time>(), forwards, interpolator),
214
      dates_(dates)
215
    {
216
        initialize();
217
    }
218
219
    template <class T>
220
    InterpolatedForwardCurve<T>::InterpolatedForwardCurve(
221
            const std::vector<Date>& dates,
222
            const std::vector<Rate>& forwards,
223
            const DayCounter& dayCounter,
224
            const T& interpolator)
225
    : ZeroYieldStructure(dates.at(0), Calendar(), dayCounter),
226
      InterpolatedCurve<T>(std::vector<Time>(), forwards, interpolator),
227
      dates_(dates)
228
    {
229
        initialize();
230
    }
231
232
    #endif
233
234
    template <class T>
235
    void InterpolatedForwardCurve<T>::initialize()
236
    {
237
        QL_REQUIRE(dates_.size() >= T::requiredPoints,
238
                   "not enough input dates given");
239
        QL_REQUIRE(this->data_.size() == dates_.size(),
240
                   "dates/data count mismatch");
241
242
        this->setupTimes(dates_, dates_[0], dayCounter());
243
        this->setupInterpolation();
244
    }
245
246
}
247
248
#endif