QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
interpolateddefaultdensitycurve.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 Chris Kenyon
5 Copyright (C) 2008 Roland Lichters
6 Copyright (C) 2008, 2009 StatPro Italia srl
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
26#ifndef quantlib_interpolated_default_density_curve_hpp
27#define quantlib_interpolated_default_density_curve_hpp
28
29#include <ql/termstructures/credit/defaultdensitystructure.hpp>
30#include <ql/termstructures/interpolatedcurve.hpp>
31#include <utility>
32
33namespace QuantLib {
34
36
37 template <class Interpolator>
40 protected InterpolatedCurve<Interpolator> {
41 public:
43 const std::vector<Date>& dates,
44 const std::vector<Real>& densities,
46 const Calendar& calendar = Calendar(),
47 const std::vector<Handle<Quote> >& jumps = {},
48 const std::vector<Date>& jumpDates = {},
49 const Interpolator& interpolator = {});
51 const std::vector<Date>& dates,
52 const std::vector<Real>& densities,
54 const Calendar& calendar,
55 const Interpolator& interpolator);
57 const std::vector<Date>& dates,
58 const std::vector<Real>& densities,
60 const Interpolator& interpolator);
62
63 Date maxDate() const override;
65
67 const std::vector<Time>& times() const;
68 const std::vector<Date>& dates() const;
69 const std::vector<Real>& data() const;
70 const std::vector<Real>& defaultDensities() const;
71 std::vector<std::pair<Date, Real> > nodes() const;
73 protected:
75 const DayCounter&,
76 const std::vector<Handle<Quote> >& jumps = {},
77 const std::vector<Date>& jumpDates = {},
78 const Interpolator& interpolator = {});
80 const Date& referenceDate,
81 const DayCounter&,
82 const std::vector<Handle<Quote> >& jumps = {},
83 const std::vector<Date>& jumpDates = {},
84 const Interpolator& interpolator = {});
87 const Calendar&,
88 const DayCounter&,
89 const std::vector<Handle<Quote> >& jumps = {},
90 const std::vector<Date>& jumpDates = {},
91 const Interpolator& interpolator = {});
93
94 Real defaultDensityImpl(Time) const override;
97 mutable std::vector<Date> dates_;
98 private:
99 void initialize(const DayCounter& dayCounter);
100 };
101
102 // inline definitions
103
104 template <class T>
106 return dates_.back();
107 }
108
109 template <class T>
110 inline const std::vector<Time>&
112 return this->times_;
113 }
114
115 template <class T>
116 inline const std::vector<Date>&
118 return dates_;
119 }
120
121 template <class T>
122 inline const std::vector<Real>&
124 return this->data_;
125 }
126
127 template <class T>
128 inline const std::vector<Real>&
130 return this->data_;
131 }
132
133 template <class T>
134 inline std::vector<std::pair<Date, Real> >
136 std::vector<std::pair<Date, Real> > results(dates_.size());
137 for (Size i=0; i<dates_.size(); ++i)
138 results[i] = std::make_pair(dates_[i], this->data_[i]);
139 return results;
140 }
141
142 #ifndef __DOXYGEN__
143
144 // template definitions
145
146 template <class T>
148 if (t <= this->times_.back())
149 return this->interpolation_(t, true);
150
151 // flat default density extrapolation
152 return this->data_.back();
153 }
154
155 template <class T>
158 if (t == 0.0)
159 return 1.0;
160
161 Real integral = 0.0;
162 if (t <= this->times_.back()) {
163 integral = this->interpolation_.primitive(t, true);
164 } else {
165 // flat default density extrapolation
166 integral = this->interpolation_.primitive(this->times_.back(), true)
167 + this->data_.back()*(t - this->times_.back());
168 }
169 Probability P = 1.0 - integral;
170 // QL_ENSURE(P >= 0.0, "negative survival probability");
171 return std::max<Real>(P, 0.0);
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 : DefaultDensityStructure(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 : DefaultDensityStructure(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 : DefaultDensityStructure(settlementDays, calendar, dayCounter, jumps, jumpDates),
202 InterpolatedCurve<T>(interpolator) {}
203
204 template <class T>
206 const std::vector<Date>& dates,
207 const std::vector<Real>& densities,
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 : DefaultDensityStructure(dates.at(0), calendar, dayCounter, jumps, jumpDates),
214 InterpolatedCurve<T>(std::vector<Time>(), densities, interpolator),
215 dates_(dates)
216 {
218 }
219
220 template <class T>
222 const std::vector<Date>& dates,
223 const std::vector<Real>& densities,
224 const DayCounter& dayCounter,
225 const Calendar& calendar,
226 const T& interpolator)
227 : DefaultDensityStructure(dates.at(0), calendar, dayCounter),
228 InterpolatedCurve<T>(std::vector<Time>(), densities, interpolator),
229 dates_(dates)
230 {
232 }
233
234 template <class T>
236 const std::vector<Date>& dates,
237 const std::vector<Real>& densities,
238 const DayCounter& dayCounter,
239 const T& interpolator)
240 : DefaultDensityStructure(dates.at(0), Calendar(), dayCounter),
241 InterpolatedCurve<T>(std::vector<Time>(), densities, interpolator),
242 dates_(dates)
243 {
245 }
246
247
248 #endif
249
250
251 template <class T>
253 QL_REQUIRE(dates_.size() >= T::requiredPoints,
254 "not enough input dates given");
255 QL_REQUIRE(this->data_.size() == dates_.size(),
256 "dates/data count mismatch");
257
258 for (Size i=0; i<dates_.size(); ++i) {
259 QL_REQUIRE(this->data_[i] >= 0.0, "negative default density");
260 }
261
262 this->setupTimes(dates_, dates_[0], dayCounter);
263 this->setupInterpolation();
264 this->interpolation_.update();
265 }
266
267}
268
269#endif
calendar class
Definition: calendar.hpp:61
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Default-density term structure.
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 default densities.
Real defaultDensityImpl(Time) const override
default density calculation
InterpolatedDefaultDensityCurve(const Date &referenceDate, const DayCounter &, const std::vector< Handle< Quote > > &jumps={}, const std::vector< Date > &jumpDates={}, const Interpolator &interpolator={})
InterpolatedDefaultDensityCurve(const std::vector< Date > &dates, const std::vector< Real > &densities, const DayCounter &dayCounter, const Calendar &calendar=Calendar(), const std::vector< Handle< Quote > > &jumps={}, const std::vector< Date > &jumpDates={}, const Interpolator &interpolator={})
std::vector< std::pair< Date, Real > > nodes() const
InterpolatedDefaultDensityCurve(Natural settlementDays, const Calendar &, const DayCounter &, const std::vector< Handle< Quote > > &jumps={}, const std::vector< Date > &jumpDates={}, const Interpolator &interpolator={})
Probability survivalProbabilityImpl(Time) const override
survival probability calculation
Date maxDate() const override
the latest date for which the curve can return values
InterpolatedDefaultDensityCurve(const std::vector< Date > &dates, const std::vector< Real > &densities, const DayCounter &dayCounter, const Calendar &calendar, const Interpolator &interpolator)
InterpolatedDefaultDensityCurve(const std::vector< Date > &dates, const std::vector< Real > &densities, const DayCounter &dayCounter, const Interpolator &interpolator)
InterpolatedDefaultDensityCurve(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
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.