41#ifndef quantext_interpolated_survival_probability_curve_hpp
42#define quantext_interpolated_survival_probability_curve_hpp
44#include <ql/termstructures/credit/survivalprobabilitystructure.hpp>
45#include <ql/termstructures/interpolatedcurve.hpp>
53 template <
class Interpolator>
56 protected InterpolatedCurve<Interpolator> {
59 const std::vector<Date>&
dates,
60 const std::vector<Probability>& probabilities,
61 const DayCounter& dayCounter,
62 const Calendar& calendar = Calendar(),
63 const std::vector<Handle<Quote> >& jumps = std::vector<Handle<Quote> >(),
64 const std::vector<Date>& jumpDates = std::vector<Date>(),
65 const Interpolator& interpolator = Interpolator(),
66 const bool allowNegativeRates =
false);
73 const std::vector<Time>&
times()
const;
74 const std::vector<Date>&
dates()
const;
75 const std::vector<Real>&
data()
const;
77 std::vector<std::pair<Date, Real> >
nodes()
const;
82 const std::vector<Handle<Quote> >& jumps = std::vector<Handle<Quote> >(),
83 const std::vector<Date>& jumpDates = std::vector<Date>(),
84 const Interpolator& interpolator = Interpolator(),
85 const bool allowNegativeRates =
false);
87 const Date& referenceDate,
89 const std::vector<Handle<Quote> >& jumps = std::vector<Handle<Quote> >(),
90 const std::vector<Date>& jumpDates = std::vector<Date>(),
91 const Interpolator& interpolator = Interpolator(),
92 const bool allowNegativeRates =
false);
94 Natural settlementDays,
97 const std::vector<Handle<Quote> >& jumps = std::vector<Handle<Quote> >(),
98 const std::vector<Date>& jumpDates = std::vector<Date>(),
99 const Interpolator& interpolator = Interpolator(),
100 const bool allowNegativeRates =
false);
115 return dates_.back();
119 inline const std::vector<Time>&
125 inline const std::vector<Date>&
131 inline const std::vector<Real>&
137 inline const std::vector<Probability>&
143 inline std::vector<std::pair<Date,Real> >
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]);
159 if (t <= this->times_.back())
160 return this->interpolation_(t,
true);
163 Time tMax = this->times_.back();
164 Probability sMax = this->data_.back();
165 Rate hazardMax = - this->interpolation_.derivative(tMax) / sMax;
166 return sMax * std::exp(- hazardMax * (t-tMax));
172 if (t <= this->times_.back())
173 return -this->interpolation_.derivative(t,
true);
176 Time tMax = this->times_.back();
177 Probability sMax = this->data_.back();
178 Rate hazardMax = - this->interpolation_.derivative(tMax) / sMax;
179 return sMax * hazardMax * std::exp(- hazardMax * (t-tMax));
184 const DayCounter& dayCounter,
185 const std::vector<Handle<Quote> >& jumps,
186 const std::vector<Date>& jumpDates,
187 const T& interpolator,
188 const bool allowNegativeRates)
190 InterpolatedCurve<T>(interpolator), allowNegativeRates_(allowNegativeRates) {}
194 const Date& referenceDate,
195 const DayCounter& dayCounter,
196 const std::vector<Handle<Quote> >& jumps,
197 const std::vector<Date>& jumpDates,
198 const T& interpolator,
199 const bool allowNegativeRates)
201 InterpolatedCurve<T>(interpolator), allowNegativeRates_(allowNegativeRates) {}
205 Natural settlementDays,
206 const Calendar& calendar,
207 const DayCounter& dayCounter,
208 const std::vector<Handle<Quote> >& jumps,
209 const std::vector<Date>& jumpDates,
210 const T& interpolator,
211 const bool allowNegativeRates)
213 InterpolatedCurve<T>(interpolator), allowNegativeRates_(allowNegativeRates) {}
217 const std::vector<Date>& dates,
218 const std::vector<Probability>& probabilities,
219 const DayCounter& dayCounter,
220 const Calendar& calendar,
221 const std::vector<Handle<Quote> >& jumps,
222 const std::vector<Date>& jumpDates,
223 const T& interpolator,
224 const bool allowNegativeRates)
226 InterpolatedCurve<T>(std::vector<Time>(), probabilities, interpolator),
227 dates_(dates), allowNegativeRates_(allowNegativeRates)
229 QL_REQUIRE(
dates_.size() >= T::requiredPoints,
230 "not enough input dates given");
231 QL_REQUIRE(this->data_.size() ==
dates_.size(),
232 "dates/data count mismatch");
233 QL_REQUIRE(this->data_[0] == 1.0,
234 "the first probability must be == 1.0 "
235 "to flag the corresponding date as reference date");
237 this->times_.resize(
dates_.size());
238 this->times_[0] = 0.0;
239 for (Size i=1; i<
dates_.size(); ++i) {
241 "invalid date (" <<
dates_[i] <<
", vs "
243 this->times_[i] = dayCounter.yearFraction(
dates_[0],
dates_[i]);
244 QL_REQUIRE(!close(this->times_[i],this->times_[i-1]),
245 "two dates correspond to the same time "
246 "under this curve's day count convention");
247 QL_REQUIRE(this->data_[i] > 0.0,
"negative probability");
249 "negative hazard rate implied by the survival "
251 this->data_[i] <<
" at " <<
dates_[i] <<
252 " (t=" << this->times_[i] <<
") after the survival "
254 this->data_[i-1] <<
" at " <<
dates_[i-1] <<
255 " (t=" << this->times_[i-1] <<
")");
258 this->interpolation_ =
259 this->interpolator_.interpolate(this->times_.begin(),
261 this->data_.begin());
262 this->interpolation_.update();
DefaultProbabilityTermStructure based on interpolation of survival probabilities.
Real defaultDensityImpl(Time) const override
InterpolatedSurvivalProbabilityCurve(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< Probability > & survivalProbabilities() const
InterpolatedSurvivalProbabilityCurve(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)
std::vector< Date > dates_
const std::vector< Date > & dates() const
InterpolatedSurvivalProbabilityCurve(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
std::vector< std::pair< Date, Real > > nodes() const
const std::vector< Time > & times() const
Probability survivalProbabilityImpl(Time) const override
Date maxDate() const override
InterpolatedSurvivalProbabilityCurve(const std::vector< Date > &dates, const std::vector< Probability > &probabilities, const DayCounter &dayCounter, const Calendar &calendar=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)