| 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 | <http://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 = 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 = 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 = 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 = false) const; |
| 96 | //! probability of default between two given dates |
| 97 | Probability defaultProbability(const Date&, |
| 98 | const Date&, |
| 99 | bool = false) const; |
| 100 | //! probability of default between two given times |
| 101 | Probability defaultProbability(Time, |
| 102 | Time, |
| 103 | bool = 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 = false) const; |
| 115 | Real defaultDensity(Time t, |
| 116 | bool = 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 = false) const; |
| 132 | Rate hazardRate(Time t, |
| 133 | bool = 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 | overriden 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 | bool ) const { |
| 181 | return survivalProbability(t: timeFromReference(d), extrapolate); |
| 182 | } |
| 183 | |
| 184 | inline |
| 185 | Probability DefaultProbabilityTermStructure::defaultProbability( |
| 186 | const Date& d, |
| 187 | bool ) const { |
| 188 | return 1.0 - survivalProbability(d, extrapolate); |
| 189 | } |
| 190 | |
| 191 | inline |
| 192 | Probability DefaultProbabilityTermStructure::defaultProbability( |
| 193 | Time t, |
| 194 | bool ) const { |
| 195 | return 1.0 - survivalProbability(t, extrapolate); |
| 196 | } |
| 197 | |
| 198 | inline |
| 199 | Real DefaultProbabilityTermStructure::defaultDensity( |
| 200 | const Date& d, |
| 201 | bool ) const { |
| 202 | return defaultDensity(t: timeFromReference(d), extrapolate); |
| 203 | } |
| 204 | |
| 205 | inline |
| 206 | Real DefaultProbabilityTermStructure::defaultDensity( |
| 207 | Time t, |
| 208 | bool ) const { |
| 209 | checkRange(t, extrapolate); |
| 210 | return defaultDensityImpl(t); |
| 211 | } |
| 212 | |
| 213 | inline |
| 214 | Rate DefaultProbabilityTermStructure::hazardRate(const Date& d, |
| 215 | bool ) const { |
| 216 | return hazardRate(t: timeFromReference(d), extrapolate); |
| 217 | } |
| 218 | |
| 219 | inline |
| 220 | Rate DefaultProbabilityTermStructure::hazardRateImpl(Time t) const { |
| 221 | Probability S = survivalProbability(t, extrapolate: true); |
| 222 | return S == 0.0 ? Rate(0.0) : defaultDensity(t, extrapolate: true)/S; |
| 223 | } |
| 224 | |
| 225 | inline Rate DefaultProbabilityTermStructure::hazardRate(Time t, |
| 226 | bool ) const { |
| 227 | checkRange(t, extrapolate); |
| 228 | return hazardRateImpl(t); |
| 229 | } |
| 230 | |
| 231 | inline |
| 232 | const std::vector<Date>& |
| 233 | DefaultProbabilityTermStructure::jumpDates() const { |
| 234 | return this->jumpDates_; |
| 235 | } |
| 236 | |
| 237 | inline |
| 238 | const std::vector<Time>& |
| 239 | DefaultProbabilityTermStructure::jumpTimes() const { |
| 240 | return this->jumpTimes_; |
| 241 | } |
| 242 | |
| 243 | inline void DefaultProbabilityTermStructure::update() { |
| 244 | TermStructure::update(); |
| 245 | if (referenceDate() != latestReference_) |
| 246 | setJumps(); |
| 247 | } |
| 248 | |
| 249 | } |
| 250 | |
| 251 | #endif |
| 252 | |