QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
interpolatedsurvivalprobabilitycurve.hpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2009 Ferdinando Ametrano
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20/*! \file interpolatedsurvivalprobabilitycurve.hpp
21 \brief interpolated survival-probability term structure
22*/
23
24#ifndef quantlib_interpolated_survival_probability_curve_hpp
25#define quantlib_interpolated_survival_probability_curve_hpp
26
29#include <utility>
30
31namespace QuantLib {
32
33 //! DefaultProbabilityTermStructure based on interpolation of survival probabilities
34 /*! \ingroup defaultprobabilitytermstructures */
35 template <class Interpolator>
38 protected InterpolatedCurve<Interpolator> {
39 public:
41 const std::vector<Date>& dates,
42 const std::vector<Probability>& probabilities,
44 const Calendar& calendar = Calendar(),
45 const std::vector<Handle<Quote> >& jumps = {},
46 const std::vector<Date>& jumpDates = {},
47 const Interpolator& interpolator = {});
49 const std::vector<Date>& dates,
50 const std::vector<Probability>& probabilities,
52 const Calendar& calendar,
53 const Interpolator& interpolator);
55 const std::vector<Date>& dates,
56 const std::vector<Probability>& probabilities,
58 const Interpolator& interpolator);
59 //! \name TermStructure interface
60 //@{
61 Date maxDate() const override;
62 //@}
63 //! \name other inspectors
64 //@{
65 const std::vector<Time>& times() const;
66 const std::vector<Date>& dates() const;
67 const std::vector<Real>& data() const;
68 const std::vector<Probability>& survivalProbabilities() const;
69 std::vector<std::pair<Date, Real> > nodes() const;
70 //@}
71 protected:
73 const DayCounter&,
74 const std::vector<Handle<Quote> >& jumps = {},
75 const std::vector<Date>& jumpDates = {},
76 const Interpolator& interpolator = {});
78 const Date& referenceDate,
79 const DayCounter&,
80 const std::vector<Handle<Quote> >& jumps = {},
81 const std::vector<Date>& jumpDates = {},
82 const Interpolator& interpolator = {});
85 const Calendar&,
86 const DayCounter&,
87 const std::vector<Handle<Quote> >& jumps = {},
88 const std::vector<Date>& jumpDates = {},
89 const Interpolator& interpolator = {});
90 //! \name DefaultProbabilityTermStructure implementation
91 //@{
93 Real defaultDensityImpl(Time) const override;
94 //@}
95 mutable std::vector<Date> dates_;
96 private:
97 void initialize();
98 };
99
100 // inline definitions
101
102 template <class T>
104 return dates_.back();
105 }
106
107 template <class T>
108 inline const std::vector<Time>&
110 return this->times_;
111 }
112
113 template <class T>
114 inline const std::vector<Date>&
116 return dates_;
117 }
118
119 template <class T>
120 inline const std::vector<Real>&
122 return this->data_;
123 }
124
125 template <class T>
126 inline const std::vector<Probability>&
128 return this->data_;
129 }
130
131 template <class T>
132 inline std::vector<std::pair<Date,Real> >
134 std::vector<std::pair<Date,Real> > results(dates_.size());
135 for (Size i=0; i<dates_.size(); ++i)
136 results[i] = std::make_pair(dates_[i],this->data_[i]);
137 return results;
138 }
139
140 #ifndef __DOXYGEN__
141
142 // template definitions
143
144 template <class T>
147 const {
148 if (t <= this->times_.back())
149 return this->interpolation_(t, true);
150
151 // flat hazard rate extrapolation
152 Time tMax = this->times_.back();
153 Probability sMax = this->data_.back();
154 Rate hazardMax = - this->interpolation_.derivative(tMax) / sMax;
155 return sMax * std::exp(- hazardMax * (t-tMax));
156 }
157
158 template <class T>
159 Real
161 if (t <= this->times_.back())
162 return -this->interpolation_.derivative(t, true);
163
164 // flat hazard rate extrapolation
165 Time tMax = this->times_.back();
166 Probability sMax = this->data_.back();
167 Rate hazardMax = - this->interpolation_.derivative(tMax) / sMax;
168 return sMax * hazardMax * std::exp(- hazardMax * (t-tMax));
169 }
170
171 template <class T>
173 const DayCounter& dayCounter,
174 const std::vector<Handle<Quote> >& jumps,
175 const std::vector<Date>& jumpDates,
176 const T& interpolator)
177 : SurvivalProbabilityStructure(dayCounter, jumps, jumpDates),
178 InterpolatedCurve<T>(interpolator) {}
179
180 template <class T>
182 const Date& referenceDate,
183 const DayCounter& dayCounter,
184 const std::vector<Handle<Quote> >& jumps,
185 const std::vector<Date>& jumpDates,
186 const T& interpolator)
187 : SurvivalProbabilityStructure(referenceDate, Calendar(), dayCounter, jumps, jumpDates),
188 InterpolatedCurve<T>(interpolator) {}
189
190 template <class T>
192 Natural settlementDays,
193 const Calendar& calendar,
194 const DayCounter& dayCounter,
195 const std::vector<Handle<Quote> >& jumps,
196 const std::vector<Date>& jumpDates,
197 const T& interpolator)
198 : SurvivalProbabilityStructure(settlementDays, calendar, dayCounter, jumps, jumpDates),
199 InterpolatedCurve<T>(interpolator) {}
200
201 template <class T>
203 const std::vector<Date>& dates,
204 const std::vector<Probability>& probabilities,
205 const DayCounter& dayCounter,
206 const Calendar& calendar,
207 const std::vector<Handle<Quote> >& jumps,
208 const std::vector<Date>& jumpDates,
209 const T& interpolator)
210 : SurvivalProbabilityStructure(dates.at(0), calendar, dayCounter, jumps, jumpDates),
211 InterpolatedCurve<T>(std::vector<Time>(), probabilities, interpolator),
212 dates_(dates)
213 {
214 initialize();
215 }
216
217 template <class T>
219 const std::vector<Date>& dates,
220 const std::vector<Probability>& probabilities,
221 const DayCounter& dayCounter,
222 const Calendar& calendar,
223 const T& interpolator)
224 : SurvivalProbabilityStructure(dates.at(0), calendar, dayCounter),
225 InterpolatedCurve<T>(std::vector<Time>(), probabilities, interpolator),
226 dates_(dates)
227 {
228 initialize();
229 }
230
231 template <class T>
233 const std::vector<Date>& dates,
234 const std::vector<Probability>& probabilities,
235 const DayCounter& dayCounter,
236 const T& interpolator)
237 : SurvivalProbabilityStructure(dates.at(0), Calendar(), dayCounter),
238 InterpolatedCurve<T>(std::vector<Time>(), probabilities, interpolator),
239 dates_(dates)
240 {
241 initialize();
242 }
243
244
245 #endif
246
247
248 template <class T>
250 {
251 QL_REQUIRE(dates_.size() >= T::requiredPoints,
252 "not enough input dates given");
253 QL_REQUIRE(this->data_.size() == dates_.size(),
254 "dates/data count mismatch");
255 QL_REQUIRE(this->data_[0] == 1.0,
256 "the first probability must be == 1.0 "
257 "to flag the corresponding date as reference date");
258
259 this->setupTimes(dates_, dates_[0], dayCounter());
260
261 for (Size i=1; i<dates_.size(); ++i) {
262 QL_REQUIRE(this->data_[i] > 0.0, "negative probability");
263 QL_REQUIRE(this->data_[i] <= this->data_[i-1],
264 "negative hazard rate implied by the survival "
265 "probability " <<
266 this->data_[i] << " at " << dates_[i] <<
267 " (t=" << this->times_[i] << ") after the survival "
268 "probability " <<
269 this->data_[i-1] << " at " << dates_[i-1] <<
270 " (t=" << this->times_[i-1] << ")");
271 }
272
273 this->setupInterpolation();
274 this->interpolation_.update();
275 }
276
277}
278
279#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
Helper class to build interpolated term structures.
DefaultProbabilityTermStructure based on interpolation of survival probabilities.
Real defaultDensityImpl(Time) const override
default density calculation
const std::vector< Probability > & survivalProbabilities() const
InterpolatedSurvivalProbabilityCurve(const DayCounter &, const std::vector< Handle< Quote > > &jumps={}, const std::vector< Date > &jumpDates={}, const Interpolator &interpolator={})
InterpolatedSurvivalProbabilityCurve(const std::vector< Date > &dates, const std::vector< Probability > &probabilities, const DayCounter &dayCounter, const Calendar &calendar, const Interpolator &interpolator)
Probability survivalProbabilityImpl(Time) const override
survival probability calculation
InterpolatedSurvivalProbabilityCurve(Natural settlementDays, const Calendar &, const DayCounter &, 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
InterpolatedSurvivalProbabilityCurve(const std::vector< Date > &dates, const std::vector< Probability > &probabilities, const DayCounter &dayCounter, const Calendar &calendar=Calendar(), const std::vector< Handle< Quote > > &jumps={}, const std::vector< Date > &jumpDates={}, const Interpolator &interpolator={})
InterpolatedSurvivalProbabilityCurve(const std::vector< Date > &dates, const std::vector< Probability > &probabilities, const DayCounter &dayCounter, const Interpolator &interpolator)
InterpolatedSurvivalProbabilityCurve(const Date &referenceDate, const DayCounter &, const std::vector< Handle< Quote > > &jumps={}, const std::vector< Date > &jumpDates={}, 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
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
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
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Helper class to build interpolated term structures.
Definition: any.hpp:35
STL namespace.
survival-probability term structure