QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
interpolatedhazardratecurve.hpp
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 <http://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
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
34namespace QuantLib {
35
37
38 template <class Interpolator>
40 : public HazardRateStructure,
41 protected InterpolatedCurve<Interpolator> {
42 public:
44 const std::vector<Date>& dates,
45 const std::vector<Rate>& hazardRates,
47 const Calendar& cal = Calendar(),
48 const std::vector<Handle<Quote> >& jumps = {},
49 const std::vector<Date>& jumpDates = {},
50 const Interpolator& interpolator = {});
52 const std::vector<Date>& dates,
53 const std::vector<Rate>& hazardRates,
55 const Calendar& calendar,
56 const Interpolator& interpolator);
58 const std::vector<Date>& dates,
59 const std::vector<Rate>& hazardRates,
61 const Interpolator& interpolator);
63
64 Date maxDate() const override;
66
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;
74 protected:
76 const DayCounter&,
77 const std::vector<Handle<Quote> >& jumps = {},
78 const std::vector<Date>& jumpDates = {},
79 const Interpolator& interpolator = {});
81 const Date& referenceDate,
82 const DayCounter&,
83 const std::vector<Handle<Quote> >& jumps = {},
84 const std::vector<Date>& jumpDates = {},
85 const Interpolator& interpolator = {});
88 const Calendar&,
89 const DayCounter&,
90 const std::vector<Handle<Quote> >& jumps = {},
91 const std::vector<Date>& jumpDates = {},
92 const Interpolator& interpolator = {});
94
95 Real hazardRateImpl(Time) const override;
98 mutable std::vector<Date> dates_;
99 private:
100 void initialize();
101 };
102
103
104 // inline definitions
105
106 template <class T>
108 return dates_.back();
109 }
110
111 template <class T>
112 inline const std::vector<Time>&
114 return this->times_;
115 }
116
117 template <class T>
118 inline const std::vector<Date>&
120 return dates_;
121 }
122
123 template <class T>
124 inline const std::vector<Real>&
126 return this->data_;
127 }
128
129 template <class T>
130 inline const std::vector<Rate>&
132 return this->data_;
133 }
134
135 template <class T>
136 inline std::vector<std::pair<Date, Real> >
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>
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>
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>
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>
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>
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>
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>
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>
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>
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
calendar class
Definition: calendar.hpp:61
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
const std::vector< Date > & jumpDates() const
Shared handle to an observable.
Definition: handle.hpp:41
Hazard-rate term structure.
Helper class to build interpolated term structures.
DefaultProbabilityTermStructure based on interpolation of hazard rates.
InterpolatedHazardRateCurve(const DayCounter &, const std::vector< Handle< Quote > > &jumps={}, const std::vector< Date > &jumpDates={}, const Interpolator &interpolator={})
Real hazardRateImpl(Time) const override
hazard rate calculation
InterpolatedHazardRateCurve(const Date &referenceDate, const DayCounter &, const std::vector< Handle< Quote > > &jumps={}, const std::vector< Date > &jumpDates={}, const Interpolator &interpolator={})
const std::vector< Date > & dates() const
const std::vector< Real > & data() const
InterpolatedHazardRateCurve(const std::vector< Date > &dates, const std::vector< Rate > &hazardRates, const DayCounter &dayCounter, const Interpolator &interpolator)
std::vector< std::pair< Date, Real > > nodes() const
const std::vector< Time > & times() const
Probability survivalProbabilityImpl(Time) const override
InterpolatedHazardRateCurve(const std::vector< Date > &dates, const std::vector< Rate > &hazardRates, const DayCounter &dayCounter, const Calendar &cal=Calendar(), const std::vector< Handle< Quote > > &jumps={}, const std::vector< Date > &jumpDates={}, const Interpolator &interpolator={})
Date maxDate() const override
the latest date for which the curve can return values
const std::vector< Rate > & hazardRates() const
InterpolatedHazardRateCurve(Natural settlementDays, const Calendar &, const DayCounter &, const std::vector< Handle< Quote > > &jumps={}, const std::vector< Date > &jumpDates={}, const Interpolator &interpolator={})
InterpolatedHazardRateCurve(const std::vector< Date > &dates, const std::vector< Rate > &hazardRates, const DayCounter &dayCounter, const Calendar &calendar, const Interpolator &interpolator)
virtual Natural settlementDays() const
the settlementDays used for reference date calculation
virtual const Date & referenceDate() const
the date at which discount = 1.0 and/or variance = 0.0
virtual Calendar calendar() const
the calendar used for reference and/or option date calculation
virtual DayCounter dayCounter() const
the day counter used for date/time conversion
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
Real Probability
probability
Definition: types.hpp:82
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.