/src/quantlib/ql/termstructures/defaulttermstructure.hpp
Line | Count | Source |
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 StatPro Italia srl |
7 | | Copyright (C) 2009 Ferdinando Ametrano |
8 | | |
9 | | This file is part of QuantLib, a free-software/open-source library |
10 | | for financial quantitative analysts and developers - http://quantlib.org/ |
11 | | |
12 | | QuantLib is free software: you can redistribute it and/or modify it |
13 | | under the terms of the QuantLib license. You should have received a |
14 | | copy of the license along with this program; if not, please email |
15 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
16 | | <https://www.quantlib.org/license.shtml>. |
17 | | |
18 | | This program is distributed in the hope that it will be useful, but WITHOUT |
19 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
20 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
21 | | */ |
22 | | |
23 | | /*! \file defaulttermstructure.hpp |
24 | | \brief default-probability term structure |
25 | | */ |
26 | | |
27 | | #ifndef quantlib_default_term_structure_hpp |
28 | | #define quantlib_default_term_structure_hpp |
29 | | |
30 | | #include <ql/termstructure.hpp> |
31 | | #include <ql/quote.hpp> |
32 | | |
33 | | namespace QuantLib { |
34 | | |
35 | | //! Default probability term structure |
36 | | /*! This abstract class defines the interface of concrete |
37 | | credit structures which will be derived from this one. |
38 | | |
39 | | \ingroup defaultprobabilitytermstructures |
40 | | */ |
41 | | class DefaultProbabilityTermStructure : public TermStructure { |
42 | | public: |
43 | | /*! \name Constructors |
44 | | See the TermStructure documentation for issues regarding |
45 | | constructors. |
46 | | */ |
47 | | //@{ |
48 | | DefaultProbabilityTermStructure( |
49 | | const DayCounter& dc = DayCounter(), |
50 | | std::vector<Handle<Quote> > jumps = {}, |
51 | | const std::vector<Date>& jumpDates = {}); |
52 | | DefaultProbabilityTermStructure( |
53 | | const Date& referenceDate, |
54 | | const Calendar& cal = Calendar(), |
55 | | const DayCounter& dc = DayCounter(), |
56 | | std::vector<Handle<Quote> > jumps = {}, |
57 | | const std::vector<Date>& jumpDates = {}); |
58 | | DefaultProbabilityTermStructure( |
59 | | Natural settlementDays, |
60 | | const Calendar& cal, |
61 | | const DayCounter& dc = DayCounter(), |
62 | | std::vector<Handle<Quote> > jumps = {}, |
63 | | const std::vector<Date>& jumpDates = {}); |
64 | | //@} |
65 | | |
66 | | /*! \name Survival probabilities |
67 | | |
68 | | These methods return the survival probability from the reference |
69 | | date until a given date or time. In the latter case, the time |
70 | | is calculated as a fraction of year from the reference date. |
71 | | */ |
72 | | //@{ |
73 | | Probability survivalProbability(const Date& d, |
74 | | bool extrapolate = false) const; |
75 | | /*! The same day-counting rule used by the term structure |
76 | | should be used for calculating the passed time t. |
77 | | */ |
78 | | Probability survivalProbability(Time t, |
79 | | bool extrapolate = false) const; |
80 | | //@} |
81 | | |
82 | | /*! \name Default probabilities |
83 | | |
84 | | These methods return the default probability from the reference |
85 | | date until a given date or time. In the latter case, the time |
86 | | is calculated as a fraction of year from the reference date. |
87 | | */ |
88 | | //@{ |
89 | | Probability defaultProbability(const Date& d, |
90 | | bool extrapolate = false) const; |
91 | | /*! The same day-counting rule used by the term structure |
92 | | should be used for calculating the passed time t. |
93 | | */ |
94 | | Probability defaultProbability(Time t, |
95 | | bool extrapolate = false) const; |
96 | | //! probability of default between two given dates |
97 | | Probability defaultProbability(const Date&, |
98 | | const Date&, |
99 | | bool extrapolate = false) const; |
100 | | //! probability of default between two given times |
101 | | Probability defaultProbability(Time, |
102 | | Time, |
103 | | bool extrapo = false) const; |
104 | | //@} |
105 | | |
106 | | /*! \name Default densities |
107 | | |
108 | | These methods return the default density at a given date or time. |
109 | | In the latter case, the time is calculated as a fraction of year |
110 | | from the reference date. |
111 | | */ |
112 | | //@{ |
113 | | Real defaultDensity(const Date& d, |
114 | | bool extrapolate = false) const; |
115 | | Real defaultDensity(Time t, |
116 | | bool extrapolate = false) const; |
117 | | //@} |
118 | | |
119 | | /*! \name Hazard rates |
120 | | |
121 | | These methods returns the hazard rate at a given date or time. |
122 | | In the latter case, the time is calculated as a fraction of year |
123 | | from the reference date. |
124 | | |
125 | | Hazard rates are defined with annual frequency and continuous |
126 | | compounding. |
127 | | */ |
128 | | |
129 | | //@{ |
130 | | Rate hazardRate(const Date& d, |
131 | | bool extrapolate = false) const; |
132 | | Rate hazardRate(Time t, |
133 | | bool extrapolate = false) const; |
134 | | //@} |
135 | | |
136 | | //! \name Jump inspectors |
137 | | //@{ |
138 | | const std::vector<Date>& jumpDates() const; |
139 | | const std::vector<Time>& jumpTimes() const; |
140 | | //@} |
141 | | |
142 | | //! \name Observer interface |
143 | | //@{ |
144 | | void update() override; |
145 | | //@} |
146 | | protected: |
147 | | /*! \name Calculations |
148 | | The first two methods must be implemented in derived classes to |
149 | | perform the actual calculations. When they are called, |
150 | | range check has already been performed; therefore, they |
151 | | must assume that extrapolation is required. |
152 | | The third method has a default implementation which can be |
153 | | overridden with a more efficient implementation in derived |
154 | | classes. |
155 | | */ |
156 | | //@{ |
157 | | //! survival probability calculation |
158 | | virtual Probability survivalProbabilityImpl(Time) const = 0; |
159 | | //! default density calculation |
160 | | virtual Real defaultDensityImpl(Time) const = 0; |
161 | | //! hazard rate calculation |
162 | | virtual Real hazardRateImpl(Time) const; |
163 | | //@} |
164 | | private: |
165 | | // methods |
166 | | void setJumps(); |
167 | | // data members |
168 | | std::vector<Handle<Quote> > jumps_; |
169 | | std::vector<Date> jumpDates_; |
170 | | std::vector<Time> jumpTimes_; |
171 | | Size nJumps_; |
172 | | Date latestReference_; |
173 | | }; |
174 | | |
175 | | // inline definitions |
176 | | |
177 | | inline |
178 | | Probability DefaultProbabilityTermStructure::survivalProbability( |
179 | | const Date& d, |
180 | 0 | bool extrapolate) const { |
181 | 0 | return survivalProbability(timeFromReference(d), extrapolate); |
182 | 0 | } |
183 | | |
184 | | inline |
185 | | Probability DefaultProbabilityTermStructure::defaultProbability( |
186 | | const Date& d, |
187 | 0 | bool extrapolate) const { |
188 | 0 | return 1.0 - survivalProbability(d, extrapolate); |
189 | 0 | } |
190 | | |
191 | | inline |
192 | | Probability DefaultProbabilityTermStructure::defaultProbability( |
193 | | Time t, |
194 | 0 | bool extrapolate) const { |
195 | 0 | return 1.0 - survivalProbability(t, extrapolate); |
196 | 0 | } |
197 | | |
198 | | inline |
199 | | Real DefaultProbabilityTermStructure::defaultDensity( |
200 | | const Date& d, |
201 | 0 | bool extrapolate) const { |
202 | 0 | return defaultDensity(timeFromReference(d), extrapolate); |
203 | 0 | } |
204 | | |
205 | | inline |
206 | | Real DefaultProbabilityTermStructure::defaultDensity( |
207 | | Time t, |
208 | 0 | bool extrapolate) const { |
209 | 0 | checkRange(t, extrapolate); |
210 | 0 | return defaultDensityImpl(t); |
211 | 0 | } |
212 | | |
213 | | inline |
214 | | Rate DefaultProbabilityTermStructure::hazardRate(const Date& d, |
215 | 0 | bool extrapolate) const { |
216 | 0 | return hazardRate(timeFromReference(d), extrapolate); |
217 | 0 | } |
218 | | |
219 | | inline |
220 | 0 | Rate DefaultProbabilityTermStructure::hazardRateImpl(Time t) const { |
221 | 0 | Probability S = survivalProbability(t, true); |
222 | 0 | return S == 0.0 ? Rate(0.0) : defaultDensity(t, true)/S; |
223 | 0 | } |
224 | | |
225 | | inline Rate DefaultProbabilityTermStructure::hazardRate(Time t, |
226 | 0 | bool extrapolate) const { |
227 | 0 | checkRange(t, extrapolate); |
228 | 0 | return hazardRateImpl(t); |
229 | 0 | } |
230 | | |
231 | | inline |
232 | | const std::vector<Date>& |
233 | 0 | DefaultProbabilityTermStructure::jumpDates() const { |
234 | 0 | return this->jumpDates_; |
235 | 0 | } |
236 | | |
237 | | inline |
238 | | const std::vector<Time>& |
239 | 0 | DefaultProbabilityTermStructure::jumpTimes() const { |
240 | 0 | return this->jumpTimes_; |
241 | 0 | } |
242 | | |
243 | 0 | inline void DefaultProbabilityTermStructure::update() { |
244 | 0 | TermStructure::update(); |
245 | 0 | if (referenceDate() != latestReference_) |
246 | 0 | setJumps(); |
247 | 0 | } |
248 | | |
249 | | } |
250 | | |
251 | | #endif |