Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
interpolatedhazardratecurve.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*
20 Copyright (C) 2008 Jose Aparicio
21 Copyright (C) 2008 Chris Kenyon
22 Copyright (C) 2008 Roland Lichters
23 Copyright (C) 2008, 2009 StatPro Italia srl
24
25 This file is part of QuantLib, a free-software/open-source library
26 for financial quantitative analysts and developers - http://quantlib.org/
27
28 QuantLib is free software: you can redistribute it and/or modify it
29 under the terms of the QuantLib license. You should have received a
30 copy of the license along with this program; if not, please email
31 <quantlib-dev@lists.sf.net>. The license is also available online at
32 <http://quantlib.org/license.shtml>.
33
34 This program is distributed in the hope that it will be useful, but WITHOUT
35 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
36 FOR A PARTICULAR PURPOSE. See the license for more details.
37*/
38
39/*! \file interpolatedhazardratecurve.hpp
40 \brief interpolated hazard-rate term structure (with the option to disable
41 the negative rates check)
42*/
43
44#ifndef quantext_interpolated_hazard_rate_curve_hpp
45#define quantext_interpolated_hazard_rate_curve_hpp
46
47#include <ql/termstructures/credit/hazardratestructure.hpp>
48#include <ql/termstructures/interpolatedcurve.hpp>
49#include <utility>
50
51
52namespace QuantExt {
53using namespace QuantLib;
54
55 //! DefaultProbabilityTermStructure based on interpolation of hazard rates
56 /*! This is an exact copy of the QuantLib class, but with the option to
57 disabled the check for non negative input hazard rates */
58 /*! \ingroup defaultprobabilitytermstructures */
59 template <class Interpolator>
61 : public HazardRateStructure,
62 protected InterpolatedCurve<Interpolator> {
63 public:
65 const std::vector<Date>& dates,
66 const std::vector<Rate>& hazardRates,
67 const DayCounter& dayCounter,
68 const Calendar& cal = Calendar(),
69 const std::vector<Handle<Quote> >& jumps =
70 std::vector<Handle<Quote> >(),
71 const std::vector<Date>& jumpDates = std::vector<Date>(),
72 const Interpolator& interpolator = Interpolator(),
73 const bool allowNegativeRates = false);
75 const std::vector<Date>& dates,
76 const std::vector<Rate>& hazardRates,
77 const DayCounter& dayCounter,
78 const Calendar& calendar,
79 const Interpolator& interpolator,
80 const bool allowNegativeRates = false);
82 const std::vector<Date>& dates,
83 const std::vector<Rate>& hazardRates,
84 const DayCounter& dayCounter,
85 const Interpolator& interpolator,
86 const bool allowNegativeRates = false);
87 //! \name TermStructure interface
88 //@{
89 Date maxDate() const override;
90 //@}
91 //! \name other inspectors
92 //@{
93 const std::vector<Time>& times() const;
94 const std::vector<Date>& dates() const;
95 const std::vector<Real>& data() const;
96 const std::vector<Rate>& hazardRates() const;
97 std::vector<std::pair<Date, Real> > nodes() const;
98 //@}
99 protected:
101 const DayCounter&,
102 const std::vector<Handle<Quote> >& jumps = std::vector<Handle<Quote> >(),
103 const std::vector<Date>& jumpDates = std::vector<Date>(),
104 const Interpolator& interpolator = Interpolator(),
105 const bool allowNegativeRates = false);
107 const Date& referenceDate,
108 const DayCounter&,
109 const std::vector<Handle<Quote> >& jumps = std::vector<Handle<Quote> >(),
110 const std::vector<Date>& jumpDates = std::vector<Date>(),
111 const Interpolator& interpolator = Interpolator(),
112 const bool allowNegativeRates = false);
114 Natural settlementDays,
115 const Calendar&,
116 const DayCounter&,
117 const std::vector<Handle<Quote> >& jumps = std::vector<Handle<Quote> >(),
118 const std::vector<Date>& jumpDates = std::vector<Date>(),
119 const Interpolator& interpolator = Interpolator(),
120 const bool allowNegativeRates = false);
121 //! \name DefaultProbabilityTermStructure implementation
122 //@{
123 Real hazardRateImpl(Time) const override;
124 Probability survivalProbabilityImpl(Time) const override;
125 //@}
126 mutable std::vector<Date> dates_;
127 private:
128 void initialize();
130 };
131
132
133 // inline definitions
134
135 template <class T>
137 return dates_.back();
138 }
139
140 template <class T>
141 inline const std::vector<Time>&
143 return this->times_;
144 }
145
146 template <class T>
147 inline const std::vector<Date>&
149 return dates_;
150 }
151
152 template <class T>
153 inline const std::vector<Real>&
155 return this->data_;
156 }
157
158 template <class T>
159 inline const std::vector<Rate>&
161 return this->data_;
162 }
163
164 template <class T>
165 inline std::vector<std::pair<Date, Real> >
167 std::vector<std::pair<Date, Real> > results(dates_.size());
168 for (Size i=0; i<dates_.size(); ++i)
169 results[i] = std::make_pair(dates_[i], this->data_[i]);
170 return results;
171 }
172
173 #ifndef __DOXYGEN__
174
175 // template definitions
176
177 template <class T>
179 if (t <= this->times_.back())
180 return this->interpolation_(t, true);
181
182 // flat hazard rate extrapolation
183 return this->data_.back();
184 }
185
186 template <class T>
187 Probability
189 if (t == 0.0)
190 return 1.0;
191
192 Real integral;
193 if (t <= this->times_.back()) {
194 integral = this->interpolation_.primitive(t, true);
195 } else {
196 // flat hazard rate extrapolation
197 integral = this->interpolation_.primitive(this->times_.back(), true)
198 + this->data_.back()*(t - this->times_.back());
199 }
200 return std::exp(-integral);
201 }
202
203 template <class T>
205 const DayCounter& dayCounter,
206 const std::vector<Handle<Quote> >& jumps,
207 const std::vector<Date>& jumpDates,
208 const T& interpolator,
209 const bool allowNegativeRates)
210 : HazardRateStructure(dayCounter, jumps, jumpDates),
211 InterpolatedCurve<T>(interpolator), allowNegativeRates_(allowNegativeRates) {}
212
213 template <class T>
215 const Date& referenceDate,
216 const DayCounter& dayCounter,
217 const std::vector<Handle<Quote> >& jumps,
218 const std::vector<Date>& jumpDates,
219 const T& interpolator,
220 const bool allowNegativeRates)
221 : HazardRateStructure(referenceDate, Calendar(), dayCounter, jumps, jumpDates),
222 InterpolatedCurve<T>(interpolator), allowNegativeRates_(allowNegativeRates) {}
223
224 template <class T>
226 Natural settlementDays,
227 const Calendar& calendar,
228 const DayCounter& dayCounter,
229 const std::vector<Handle<Quote> >& jumps,
230 const std::vector<Date>& jumpDates,
231 const T& interpolator,
232 const bool allowNegativeRates)
233 : HazardRateStructure(settlementDays, calendar, dayCounter, jumps, jumpDates),
234 InterpolatedCurve<T>(interpolator), allowNegativeRates_(allowNegativeRates) {}
235
236 template <class T>
238 const std::vector<Date>& dates,
239 const std::vector<Rate>& hazardRates,
240 const DayCounter& dayCounter,
241 const Calendar& calendar,
242 const std::vector<Handle<Quote> >& jumps,
243 const std::vector<Date>& jumpDates,
244 const T& interpolator,
245 const bool allowNegativeRates)
246 : HazardRateStructure(dates.at(0), calendar, dayCounter, jumps, jumpDates),
247 InterpolatedCurve<T>(std::vector<Time>(), hazardRates, interpolator),
248 dates_(dates), allowNegativeRates_(allowNegativeRates)
249 {
250 initialize();
251 }
252
253 template <class T>
255 const std::vector<Date>& dates,
256 const std::vector<Rate>& hazardRates,
257 const DayCounter& dayCounter,
258 const Calendar& calendar,
259 const T& interpolator,
260 const bool allowNegativeRates)
261 : HazardRateStructure(dates.at(0), calendar, dayCounter),
262 InterpolatedCurve<T>(std::vector<Time>(), hazardRates, interpolator),
263 dates_(dates), allowNegativeRates_(allowNegativeRates)
264 {
265 initialize();
266 }
267
268 template <class T>
270 const std::vector<Date>& dates,
271 const std::vector<Rate>& hazardRates,
272 const DayCounter& dayCounter,
273 const T& interpolator,
274 const bool allowNegativeRates)
275 : HazardRateStructure(dates.at(0), Calendar(), dayCounter),
276 InterpolatedCurve<T>(std::vector<Time>(), hazardRates, interpolator),
277 dates_(dates), allowNegativeRates_(allowNegativeRates)
278 {
279 initialize();
280 }
281
282 #endif
283
284 template <class T>
286 {
287 QL_REQUIRE(dates_.size() >= T::requiredPoints,
288 "not enough input dates given");
289 QL_REQUIRE(this->data_.size() == dates_.size(),
290 "dates/data count mismatch");
291
292 this->times_.resize(dates_.size());
293 this->times_[0] = 0.0;
294 for (Size i=1; i<dates_.size(); ++i) {
295 QL_REQUIRE(dates_[i] > dates_[i-1],
296 "invalid date (" << dates_[i] << ", vs "
297 << dates_[i-1] << ")");
298 this->times_[i] = dayCounter().yearFraction(dates_[0], dates_[i]);
299 QL_REQUIRE(!close(this->times_[i], this->times_[i-1]),
300 "two dates correspond to the same time "
301 "under this curve's day count convention");
302 QL_REQUIRE(allowNegativeRates_ || this->data_[i] >= 0.0, "negative hazard rate");
303 }
304
305 this->interpolation_ =
306 this->interpolator_.interpolate(this->times_.begin(),
307 this->times_.end(),
308 this->data_.begin());
309 this->interpolation_.update();
310 }
311
312}
313
314#endif
DefaultProbabilityTermStructure based on interpolation of hazard rates.
Real hazardRateImpl(Time) const override
InterpolatedHazardRateCurve(const Date &referenceDate, const DayCounter &, const std::vector< Handle< Quote > > &jumps=std::vector< Handle< Quote > >(), const std::vector< Date > &jumpDates=std::vector< Date >(), const Interpolator &interpolator=Interpolator(), const bool allowNegativeRates=false)
const std::vector< Date > & dates() const
InterpolatedHazardRateCurve(Natural settlementDays, const Calendar &, const DayCounter &, const std::vector< Handle< Quote > > &jumps=std::vector< Handle< Quote > >(), const std::vector< Date > &jumpDates=std::vector< Date >(), const Interpolator &interpolator=Interpolator(), const bool allowNegativeRates=false)
const std::vector< Real > & data() const
InterpolatedHazardRateCurve(const std::vector< Date > &dates, const std::vector< Rate > &hazardRates, const DayCounter &dayCounter, const Calendar &cal=Calendar(), const std::vector< Handle< Quote > > &jumps=std::vector< Handle< Quote > >(), const std::vector< Date > &jumpDates=std::vector< Date >(), const Interpolator &interpolator=Interpolator(), const bool allowNegativeRates=false)
std::vector< std::pair< Date, Real > > nodes() const
InterpolatedHazardRateCurve(const std::vector< Date > &dates, const std::vector< Rate > &hazardRates, const DayCounter &dayCounter, const Interpolator &interpolator, const bool allowNegativeRates=false)
const std::vector< Time > & times() const
Probability survivalProbabilityImpl(Time) const override
const std::vector< Rate > & hazardRates() const
InterpolatedHazardRateCurve(const DayCounter &, const std::vector< Handle< Quote > > &jumps=std::vector< Handle< Quote > >(), const std::vector< Date > &jumpDates=std::vector< Date >(), const Interpolator &interpolator=Interpolator(), const bool allowNegativeRates=false)
InterpolatedHazardRateCurve(const std::vector< Date > &dates, const std::vector< Rate > &hazardRates, const DayCounter &dayCounter, const Calendar &calendar, const Interpolator &interpolator, const bool allowNegativeRates=false)
Real integral(const CrossAssetModel &model, const E &e, const Real a, const Real b)