Coverage Report

Created: 2025-08-28 06:30

/src/quantlib/ql/termstructures/yield/forwardcurve.hpp
Line
Count
Source (jump to first uncovered line)
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
 <http://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/forwardstructure.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 ForwardRateStructure,
40
                                     protected InterpolatedCurve<Interpolator> {
41
      public:
42
        // constructor
43
        InterpolatedForwardCurve(
44
            const std::vector<Date>& dates,
45
            const std::vector<Rate>& forwards,
46
            const DayCounter& dayCounter,
47
            const Calendar& cal = Calendar(),
48
            const std::vector<Handle<Quote> >& jumps = {},
49
            const std::vector<Date>& jumpDates = {},
50
            const Interpolator& interpolator = {});
51
        InterpolatedForwardCurve(
52
            const std::vector<Date>& dates,
53
            const std::vector<Rate>& forwards,
54
            const DayCounter& dayCounter,
55
            const Calendar& calendar,
56
            const Interpolator& interpolator);
57
        InterpolatedForwardCurve(
58
            const std::vector<Date>& dates,
59
            const std::vector<Rate>& forwards,
60
            const DayCounter& dayCounter,
61
            const Interpolator& interpolator);
62
        //! \name TermStructure interface
63
        //@{
64
        Date maxDate() const override;
65
        //@}
66
        //! \name other inspectors
67
        //@{
68
        const std::vector<Time>& times() const;
69
        const std::vector<Date>& dates() const;
70
        const std::vector<Real>& data() const;
71
        const std::vector<Rate>& forwards() const;
72
        std::vector<std::pair<Date, Real> > nodes() 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 ForwardRateStructure implementation
94
        //@{
95
        Rate forwardImpl(Time t) const override;
96
        Rate zeroYieldImpl(Time t) const override;
97
        //@}
98
        mutable std::vector<Date> dates_;
99
      private:
100
        void initialize();
101
    };
102
103
    //! Term structure based on flat interpolation of forward rates
104
    /*! \ingroup yieldtermstructures */
105
106
    typedef InterpolatedForwardCurve<BackwardFlat> ForwardCurve;
107
108
109
    // inline definitions
110
111
    template <class T>
112
    inline Date InterpolatedForwardCurve<T>::maxDate() const {
113
        if (this->maxDate_ != Date())
114
           return this->maxDate_;
115
        return dates_.back();
116
    }
117
118
    template <class T>
119
    inline const std::vector<Time>&
120
    InterpolatedForwardCurve<T>::times() const {
121
        return this->times_;
122
    }
123
124
    template <class T>
125
    inline const std::vector<Date>&
126
0
    InterpolatedForwardCurve<T>::dates() const {
127
0
        return dates_;
128
0
    }
Unexecuted instantiation: QuantLib::InterpolatedForwardCurve<QuantLib::BackwardFlat>::dates() const
Unexecuted instantiation: QuantLib::InterpolatedForwardCurve<QuantLib::ForwardFlat>::dates() const
129
130
    template <class T>
131
    inline const std::vector<Real>&
132
    InterpolatedForwardCurve<T>::data() const {
133
        return this->data_;
134
    }
135
136
    template <class T>
137
    inline const std::vector<Rate>&
138
    InterpolatedForwardCurve<T>::forwards() const {
139
        return this->data_;
140
    }
141
142
    template <class T>
143
    inline std::vector<std::pair<Date, Real> >
144
    InterpolatedForwardCurve<T>::nodes() const {
145
        std::vector<std::pair<Date, Real> > results(dates_.size());
146
        for (Size i=0; i<dates_.size(); ++i)
147
            results[i] = std::make_pair(dates_[i], this->data_[i]);
148
        return results;
149
    }
150
151
    #ifndef __DOXYGEN__
152
153
    // template definitions
154
155
    template <class T>
156
    Rate InterpolatedForwardCurve<T>::forwardImpl(Time t) const {
157
        if (t <= this->times_.back())
158
            return this->interpolation_(t, true);
159
160
        // flat fwd extrapolation
161
        return this->data_.back();
162
    }
163
164
    template <class T>
165
    Rate InterpolatedForwardCurve<T>::zeroYieldImpl(Time t) const {
166
        if (t == 0.0)
167
            return forwardImpl(0.0);
168
169
        Real integral;
170
        if (t <= this->times_.back()) {
171
            integral = this->interpolation_.primitive(t, true);
172
        } else {
173
            // flat fwd extrapolation
174
            integral = this->interpolation_.primitive(this->times_.back(), true)
175
                     + this->data_.back()*(t - this->times_.back());
176
        }
177
        return integral/t;
178
    }
179
180
    template <class T>
181
    InterpolatedForwardCurve<T>::InterpolatedForwardCurve(
182
                                    const DayCounter& dayCounter,
183
                                    const T& interpolator)
184
    : ForwardRateStructure(dayCounter), InterpolatedCurve<T>(interpolator) {}
185
186
    template <class T>
187
    InterpolatedForwardCurve<T>::InterpolatedForwardCurve(
188
                                    const Date& referenceDate,
189
                                    const DayCounter& dayCounter,
190
                                    const std::vector<Handle<Quote> >& jumps,
191
                                    const std::vector<Date>& jumpDates,
192
                                    const T& interpolator)
193
    : ForwardRateStructure(referenceDate, Calendar(), dayCounter, jumps, jumpDates),
194
      InterpolatedCurve<T>(interpolator) {}
195
196
    template <class T>
197
    InterpolatedForwardCurve<T>::InterpolatedForwardCurve(
198
                                    Natural settlementDays,
199
                                    const Calendar& calendar,
200
                                    const DayCounter& dayCounter,
201
                                    const std::vector<Handle<Quote> >& jumps,
202
                                    const std::vector<Date>& jumpDates,
203
                                    const T& interpolator)
204
    : ForwardRateStructure(settlementDays, calendar, dayCounter, jumps, jumpDates),
205
      InterpolatedCurve<T>(interpolator) {}
206
207
    template <class T>
208
    InterpolatedForwardCurve<T>::InterpolatedForwardCurve(
209
                                    const std::vector<Date>& dates,
210
                                    const std::vector<Rate>& forwards,
211
                                    const DayCounter& dayCounter,
212
                                    const Calendar& calendar,
213
                                    const std::vector<Handle<Quote> >& jumps,
214
                                    const std::vector<Date>& jumpDates,
215
                                    const T& interpolator)
216
    : ForwardRateStructure(dates.at(0), calendar, dayCounter, jumps, jumpDates),
217
      InterpolatedCurve<T>(std::vector<Time>(), forwards, interpolator),
218
      dates_(dates)
219
    {
220
        initialize();
221
    }
222
223
    template <class T>
224
    InterpolatedForwardCurve<T>::InterpolatedForwardCurve(
225
            const std::vector<Date>& dates,
226
            const std::vector<Rate>& forwards,
227
            const DayCounter& dayCounter,
228
            const Calendar& calendar,
229
            const T& interpolator)
230
    : ForwardRateStructure(dates.at(0), calendar, dayCounter),
231
      InterpolatedCurve<T>(std::vector<Time>(), forwards, interpolator),
232
      dates_(dates)
233
    {
234
        initialize();
235
    }
236
237
    template <class T>
238
    InterpolatedForwardCurve<T>::InterpolatedForwardCurve(
239
            const std::vector<Date>& dates,
240
            const std::vector<Rate>& forwards,
241
            const DayCounter& dayCounter,
242
            const T& interpolator)
243
    : ForwardRateStructure(dates.at(0), Calendar(), dayCounter),
244
      InterpolatedCurve<T>(std::vector<Time>(), forwards, interpolator),
245
      dates_(dates)
246
    {
247
        initialize();
248
    }
249
250
    #endif
251
252
    template <class T>
253
    void InterpolatedForwardCurve<T>::initialize()
254
    {
255
        QL_REQUIRE(dates_.size() >= T::requiredPoints,
256
                   "not enough input dates given");
257
        QL_REQUIRE(this->data_.size() == dates_.size(),
258
                   "dates/data count mismatch");
259
260
        this->setupTimes(dates_, dates_[0], dayCounter());
261
        this->setupInterpolation();
262
        this->interpolation_.update();
263
    }
264
265
}
266
267
#endif