24#ifndef quantext_survival_probability_curve_hpp
25#define quantext_survival_probability_curve_hpp
27#include <boost/make_shared.hpp>
28#include <ql/patterns/lazyobject.hpp>
29#include <ql/termstructures/credit/survivalprobabilitystructure.hpp>
30#include <ql/termstructures/interpolatedcurve.hpp>
37template <
class Interpolator>
39 protected InterpolatedCurve<Interpolator>,
44 const DayCounter& dayCounter,
const Calendar& calendar = Calendar(),
45 const std::vector<Handle<Quote>>& jumps = std::vector<Handle<Quote>>(),
46 const std::vector<Date>& jumpDates = std::vector<Date>(),
47 const Interpolator& interpolator = Interpolator(),
55 const std::vector<Time>&
times()
const;
56 const std::vector<Date>&
dates()
const;
57 const std::vector<Real>&
data()
const;
59 const std::vector<Handle<Quote>>&
quotes()
const;
60 std::vector<std::pair<Date, Real>>
nodes()
const;
98 std::vector<std::pair<Date, Real>> results(dates_.size());
99 for (Size i = 0; i < dates_.size(); ++i)
100 results[i] = std::make_pair(dates_[i], this->data_[i]);
108 if (t <= this->times_.back())
109 return this->interpolation_(t,
true);
112 Time tMax = this->times_.back();
113 Probability sMax = this->data_.back();
114 if (this->extrapolation_ == Extrapolation::flatFwd) {
115 Rate hazardMax = -this->interpolation_.derivative(tMax) / sMax;
116 return sMax * std::exp(-hazardMax * (t - tMax));
118 return std::pow(sMax, t / tMax);
124 if (t <= this->times_.back())
125 return -this->interpolation_.derivative(t,
true);
128 Time tMax = this->times_.back();
129 Probability sMax = this->data_.back();
130 if (this->extrapolation_ == Extrapolation::flatFwd) {
131 Rate hazardMax = -this->interpolation_.derivative(tMax) / sMax;
132 return sMax * hazardMax * std::exp(-hazardMax * (t - tMax));
134 return -std::log(sMax) / tMax * std::pow(sMax, t / tMax);
140 const std::vector<Handle<Quote>>& quotes,
141 const DayCounter& dayCounter,
const Calendar& calendar,
142 const std::vector<Handle<Quote>>& jumps,
143 const std::vector<Date>& jumpDates,
const T& interpolator,
146 InterpolatedCurve<T>(std::vector<Time>(), std::vector<Real>(), interpolator), dates_(dates), quotes_(quotes),
147 extrapolation_(extrapolation) {
148 QL_REQUIRE(
dates_.size() >= T::requiredPoints,
"not enough input dates given");
149 QL_REQUIRE(
quotes_.size() ==
dates_.size(),
"dates/data count mismatch");
150 for (Size i = 0; i <
quotes_.size(); i++)
156 LazyObject::update();
157 DefaultProbabilityTermStructure::update();
161 for (Size i = 0; i < dates_.size(); ++i)
162 this->data_[i] = quotes_[i]->value();
164 this->interpolation_ =
165 this->interpolator_.interpolate(this->times_.begin(), this->times_.end(), this->data_.begin());
166 this->interpolation_.update();
170 QL_REQUIRE(dates_.size() >= T::requiredPoints,
"not enough input dates given");
171 QL_REQUIRE(quotes_.size() == dates_.size(),
"dates/data count mismatch");
173 this->times_.resize(dates_.size());
174 this->data_.resize(dates_.size());
175 this->times_[0] = 0.0;
176 for (Size i = 1; i < dates_.size(); ++i) {
177 QL_REQUIRE(dates_[i] > dates_[i - 1],
"invalid date (" << dates_[i] <<
", vs " << dates_[i - 1] <<
")");
178 this->times_[i] = dayCounter().yearFraction(dates_[0], dates_[i]);
179 QL_REQUIRE(!close(this->times_[i], this->times_[i - 1]),
"two dates correspond to the same time "
180 "under this curve's day count convention");
DefaultProbabilityTermStructure based on interpolation of survival probability quotes.
void performCalculations() const override
const std::vector< Probability > & survivalProbabilities() const
std::vector< Date > dates_
const std::vector< Date > & dates() const
const std::vector< Real > & data() const
Extrapolation extrapolation_
std::vector< std::pair< Date, Real > > nodes() const
Real defaultDensityImpl(Time t) const override
const std::vector< Time > & times() const
const std::vector< Handle< Quote > > & quotes() const
Probability survivalProbabilityImpl(Time) const override
Date maxDate() const override
std::vector< Handle< Quote > > quotes_
SurvivalProbabilityCurve(const std::vector< Date > &dates, const std::vector< Handle< Quote > > "es, 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 Extrapolation extrpolation=Extrapolation::flatFwd)