Coverage Report

Created: 2026-03-31 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/termstructures/credit/interpolatedhazardratecurve.hpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2008 Jose Aparicio
5
 Copyright (C) 2008 Chris Kenyon
6
 Copyright (C) 2008 Roland Lichters
7
 Copyright (C) 2008, 2009 StatPro Italia srl
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
/*! \file interpolatedhazardratecurve.hpp
24
    \brief interpolated hazard-rate term structure
25
*/
26
27
#ifndef quantlib_interpolated_hazard_rate_curve_hpp
28
#define quantlib_interpolated_hazard_rate_curve_hpp
29
30
#include <ql/termstructures/credit/hazardratestructure.hpp>
31
#include <ql/termstructures/interpolatedcurve.hpp>
32
#include <utility>
33
34
namespace QuantLib {
35
36
    //! DefaultProbabilityTermStructure based on interpolation of hazard rates
37
    /*! \ingroup defaultprobabilitytermstructures */
38
    template <class Interpolator>
39
    class InterpolatedHazardRateCurve
40
        : public HazardRateStructure,
41
          protected InterpolatedCurve<Interpolator> {
42
      public:
43
        InterpolatedHazardRateCurve(
44
            const std::vector<Date>& dates,
45
            const std::vector<Rate>& hazardRates,
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
        InterpolatedHazardRateCurve(
52
            const std::vector<Date>& dates,
53
            const std::vector<Rate>& hazardRates,
54
            const DayCounter& dayCounter,
55
            const Calendar& calendar,
56
            const Interpolator& interpolator);
57
        InterpolatedHazardRateCurve(
58
            const std::vector<Date>& dates,
59
            const std::vector<Rate>& hazardRates,
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>& hazardRates() const;
72
        std::vector<std::pair<Date, Real> > nodes() const;
73
        //@}
74
      protected:
75
        InterpolatedHazardRateCurve(
76
            const DayCounter&,
77
            const std::vector<Handle<Quote> >& jumps = {},
78
            const std::vector<Date>& jumpDates = {},
79
            const Interpolator& interpolator = {});
80
        InterpolatedHazardRateCurve(
81
            const Date& referenceDate,
82
            const DayCounter&,
83
            const std::vector<Handle<Quote> >& jumps = {},
84
            const std::vector<Date>& jumpDates = {},
85
            const Interpolator& interpolator = {});
86
        InterpolatedHazardRateCurve(
87
            Natural settlementDays,
88
            const Calendar&,
89
            const DayCounter&,
90
            const std::vector<Handle<Quote> >& jumps = {},
91
            const std::vector<Date>& jumpDates = {},
92
            const Interpolator& interpolator = {});
93
        //! \name DefaultProbabilityTermStructure implementation
94
        //@{
95
        Real hazardRateImpl(Time) const override;
96
        Probability survivalProbabilityImpl(Time) const override;
97
        //@}
98
        mutable std::vector<Date> dates_;
99
      private:
100
        void initialize();
101
    };
102
103
104
    // inline definitions
105
106
    template <class T>
107
    inline Date InterpolatedHazardRateCurve<T>::maxDate() const {
108
        return dates_.back();
109
    }
110
111
    template <class T>
112
    inline const std::vector<Time>&
113
    InterpolatedHazardRateCurve<T>::times() const {
114
        return this->times_;
115
    }
116
117
    template <class T>
118
    inline const std::vector<Date>&
119
0
    InterpolatedHazardRateCurve<T>::dates() const {
120
0
        return dates_;
121
0
    }
122
123
    template <class T>
124
    inline const std::vector<Real>&
125
    InterpolatedHazardRateCurve<T>::data() const {
126
        return this->data_;
127
    }
128
129
    template <class T>
130
    inline const std::vector<Rate>&
131
    InterpolatedHazardRateCurve<T>::hazardRates() const {
132
        return this->data_;
133
    }
134
135
    template <class T>
136
    inline std::vector<std::pair<Date, Real> >
137
    InterpolatedHazardRateCurve<T>::nodes() const {
138
        std::vector<std::pair<Date, Real> > results(dates_.size());
139
        for (Size i=0; i<dates_.size(); ++i)
140
            results[i] = std::make_pair(dates_[i], this->data_[i]);
141
        return results;
142
    }
143
144
    #ifndef __DOXYGEN__
145
146
    // template definitions
147
148
    template <class T>
149
    Real InterpolatedHazardRateCurve<T>::hazardRateImpl(Time t) const {
150
        if (t <= this->times_.back())
151
            return this->interpolation_(t, true);
152
153
        // flat hazard rate extrapolation
154
        return this->data_.back();
155
    }
156
157
    template <class T>
158
    Probability
159
    InterpolatedHazardRateCurve<T>::survivalProbabilityImpl(Time t) const {
160
        if (t == 0.0)
161
            return 1.0;
162
163
        Real integral;
164
        if (t <= this->times_.back()) {
165
            integral = this->interpolation_.primitive(t, true);
166
        } else {
167
            // flat hazard rate extrapolation
168
            integral = this->interpolation_.primitive(this->times_.back(), true)
169
                     + this->data_.back()*(t - this->times_.back());
170
        }
171
        return std::exp(-integral);
172
    }
173
174
    template <class T>
175
    InterpolatedHazardRateCurve<T>::InterpolatedHazardRateCurve(
176
                                    const DayCounter& dayCounter,
177
                                    const std::vector<Handle<Quote> >& jumps,
178
                                    const std::vector<Date>& jumpDates,
179
                                    const T& interpolator)
180
    : HazardRateStructure(dayCounter, jumps, jumpDates),
181
      InterpolatedCurve<T>(interpolator) {}
182
183
    template <class T>
184
    InterpolatedHazardRateCurve<T>::InterpolatedHazardRateCurve(
185
                                    const Date& referenceDate,
186
                                    const DayCounter& dayCounter,
187
                                    const std::vector<Handle<Quote> >& jumps,
188
                                    const std::vector<Date>& jumpDates,
189
                                    const T& interpolator)
190
    : HazardRateStructure(referenceDate, Calendar(), dayCounter, jumps, jumpDates),
191
      InterpolatedCurve<T>(interpolator) {}
192
193
    template <class T>
194
    InterpolatedHazardRateCurve<T>::InterpolatedHazardRateCurve(
195
                                    Natural settlementDays,
196
                                    const Calendar& calendar,
197
                                    const DayCounter& dayCounter,
198
                                    const std::vector<Handle<Quote> >& jumps,
199
                                    const std::vector<Date>& jumpDates,
200
                                    const T& interpolator)
201
    : HazardRateStructure(settlementDays, calendar, dayCounter, jumps, jumpDates),
202
      InterpolatedCurve<T>(interpolator) {}
203
204
    template <class T>
205
    InterpolatedHazardRateCurve<T>::InterpolatedHazardRateCurve(
206
                                    const std::vector<Date>& dates,
207
                                    const std::vector<Rate>& hazardRates,
208
                                    const DayCounter& dayCounter,
209
                                    const Calendar& calendar,
210
                                    const std::vector<Handle<Quote> >& jumps,
211
                                    const std::vector<Date>& jumpDates,
212
                                    const T& interpolator)
213
    : HazardRateStructure(dates.at(0), calendar, dayCounter, jumps, jumpDates),
214
      InterpolatedCurve<T>(std::vector<Time>(), hazardRates, interpolator),
215
      dates_(dates)
216
    {
217
        initialize();
218
    }
219
220
    template <class T>
221
    InterpolatedHazardRateCurve<T>::InterpolatedHazardRateCurve(
222
            const std::vector<Date>& dates,
223
            const std::vector<Rate>& hazardRates,
224
            const DayCounter& dayCounter,
225
            const Calendar& calendar,
226
            const T& interpolator)
227
    : HazardRateStructure(dates.at(0), calendar, dayCounter),
228
      InterpolatedCurve<T>(std::vector<Time>(), hazardRates, interpolator),
229
      dates_(dates)
230
    {
231
        initialize();
232
    }
233
234
    template <class T>
235
    InterpolatedHazardRateCurve<T>::InterpolatedHazardRateCurve(
236
            const std::vector<Date>& dates,
237
            const std::vector<Rate>& hazardRates,
238
            const DayCounter& dayCounter,
239
            const T& interpolator)
240
    : HazardRateStructure(dates.at(0), Calendar(), dayCounter),
241
      InterpolatedCurve<T>(std::vector<Time>(), hazardRates, interpolator),
242
      dates_(dates)
243
    {
244
        initialize();
245
    }
246
247
    #endif
248
249
    template <class T>
250
    void InterpolatedHazardRateCurve<T>::initialize()
251
    {
252
        QL_REQUIRE(dates_.size() >= T::requiredPoints,
253
                   "not enough input dates given");
254
        QL_REQUIRE(this->data_.size() == dates_.size(),
255
                   "dates/data count mismatch");
256
257
        for (Size i=0; i<dates_.size(); ++i) {
258
            QL_REQUIRE(this->data_[i] >= 0.0, "negative hazard rate");
259
        }
260
261
        this->setupTimes(dates_, dates_[0], dayCounter());
262
        this->setupInterpolation();
263
        this->interpolation_.update();
264
    }
265
266
}
267
268
#endif