QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
defaultevent.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2009 StatPro Italia srl
5 Copyright (C) 2009 Jose Aparicio
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21#include <ql/experimental/credit/defaultevent.hpp>
22#include <ql/experimental/credit/recoveryratequote.hpp>
23#include <ql/patterns/visitor.hpp>
24#include <ql/settings.hpp>
25#include <utility>
26
27namespace QuantLib {
28
30 return defaultDate_;
31 }
32
34 auto* v1 = dynamic_cast<Visitor<DefaultEvent>*>(&v);
35 if (v1 != nullptr)
36 v1->visit(*this);
37 else
39 }
40
41 // They will be sorted by settlement date
43 return settlementDate_;
44 }
45
47 auto* v1 = dynamic_cast<Visitor<DefaultEvent::DefaultSettlement>*>(&v);
48 if (v1 != nullptr)
49 v1->visit(*this);
50 else
52 }
53
55 const Date& date,
56 const std::map<Seniority, Real>& recoveryRates )
57 : settlementDate_(date), recoveryRates_(recoveryRates) {
58 QL_REQUIRE(recoveryRates.find(NoSeniority) ==
59 recoveryRates.end(),
60 "NoSeniority is not a valid realized seniority.");
61 }
62
64 const Date& date,
65 Seniority seniority,
66 const Real recoveryRate)
67 : settlementDate_(date), recoveryRates_(makeIsdaConvMap()) {
68 if (seniority == NoSeniority) {
69 for (auto& i : recoveryRates_) {
70 i.second = recoveryRate;
71 }
72 } else {
73 recoveryRates_[seniority] = recoveryRate;
74 }
75 }
76
78 Seniority sen) const {
79 // expensive require cause called often...... fix me
80 QL_REQUIRE(sen != NoSeniority,
81 "NoSeniority is not valid for recovery rate request.");
82 auto itmatch = recoveryRates_.find(sen);
83 if(itmatch != recoveryRates_.end()) {
84 return itmatch->second;
85 }else{
86 return Null<Real>();
87 }
88 }
89
90 DefaultEvent::DefaultEvent(const Date& creditEventDate,
91 const DefaultType& atomicEvType,
92 Currency curr,
93 Seniority bondsSen,
94 // Settlement information:
95 const Date& settleDate,
96 const std::map<Seniority, Real>& recoveryRates)
97 : bondsCurrency_(std::move(curr)), defaultDate_(creditEventDate), eventType_(atomicEvType),
98 bondsSeniority_(bondsSen),
99 defSettlement_(settleDate, recoveryRates.empty() ? makeIsdaConvMap() : recoveryRates) {
100 if(settleDate != Null<Date>()) {// has settled
101 QL_REQUIRE(settleDate >= creditEventDate,
102 "Settlement date should be after default date.");
103 QL_REQUIRE(recoveryRates.find(bondsSen) != recoveryRates.end(),
104 "Settled events must contain the seniority of the default");
105 }
106 }
107
108 DefaultEvent::DefaultEvent(const Date& creditEventDate,
109 const DefaultType& atomicEvType,
110 Currency curr,
111 Seniority bondsSen,
112 // Settlement information:
113 const Date& settleDate,
114 Real recoveryRate)
115 : bondsCurrency_(std::move(curr)), defaultDate_(creditEventDate), eventType_(atomicEvType),
116 bondsSeniority_(bondsSen), defSettlement_(settleDate, bondsSen, recoveryRate) {
117 if(settleDate != Null<Date>()) {
118 QL_REQUIRE(settleDate >= creditEventDate,
119 "Settlement date should be after default date.");
120 }
121 }
122
124 const DefaultProbKey& contractKey) const {
125 if(bondsCurrency_ != contractKey.currency()) return false;
126 // a contract with NoSeniority matches all events
127 if((bondsSeniority_ != contractKey.seniority())
128 && (contractKey.seniority() != NoSeniority))
129 return false;
130 // loop on all event types in the contract and chek if we match any,
131 // calls derived types
132 for(Size i=0; i<contractKey.size(); i++) {
133 if(this->matchesEventType(contractKey.eventTypes()[i])) return true;
134 }
135 return false;
136 }
137
138
139
140 bool operator==(const DefaultEvent& lhs, const DefaultEvent& rhs) {
141 return (lhs.currency() == rhs.currency()) &&
142 (lhs.defaultType() == rhs.defaultType()) &&
143 (lhs.date() == rhs.date()) &&
144 (lhs.eventSeniority() == rhs.eventSeniority());
145 }
146
147
149 const ext::shared_ptr<DefaultType>& contractEvType) const {
150 ext::shared_ptr<FailureToPay> eveType =
151 ext::dynamic_pointer_cast<FailureToPay>(contractEvType);
152 // this chekcs the atomic types, no need to call parents method
153 if(!eveType) return false;
154 if(defaultedAmount_ < eveType->amountRequired()) return false;
156 return this->hasOccurred(today - eveType->gracePeriod(), true);
157 }
158
159
160
162 const Currency& curr,
163 Seniority bondsSen,
164 Real defaultedAmount,
165 // Settlement information:
166 const Date& settleDate,
167 const std::map<Seniority, Real>&
168 recoveryRates)
169 : DefaultEvent(creditEventDate,
171 Restructuring::XR),
172 curr,
173 bondsSen,
174 settleDate,
175 recoveryRates),
176 defaultedAmount_(defaultedAmount) { }
177
179 const Currency& curr,
180 Seniority bondsSen,
181 Real defaultedAmount,
182 // Settlement information:
183 const Date& settleDate,
184 Real recoveryRates)
185 : DefaultEvent(creditEventDate,
187 Restructuring::XR),
188 curr,
189 bondsSen,
190 settleDate,
191 recoveryRates),
192 defaultedAmount_(defaultedAmount) { }
193
194
195
197 const Currency& curr,
198 Seniority bondsSen,
199 // Settlement information:
200 const Date& settleDate,
201 const std::map<Seniority, Real>&
202 recoveryRates)
203 : DefaultEvent(creditEventDate,
204 DefaultType(AtomicDefault::Bankruptcy,
205 Restructuring::XR),
206 curr,
207 bondsSen,
208 settleDate,
209 recoveryRates) {
210 if(hasSettled()) {
211 QL_REQUIRE(recoveryRates.size() == makeIsdaConvMap().size(),
212 "Bankruptcy event should have settled for all seniorities.");
213 }
214 }
215
217 const Currency& curr,
218 Seniority bondsSen,
219 // Settlement information:
220 const Date& settleDate,
221 // means same for all
222 Real recoveryRates)
223 : DefaultEvent(creditEventDate,
224 DefaultType(AtomicDefault::Bankruptcy,
225 Restructuring::XR),
226 curr,
227 bondsSen,
228 settleDate,
229 recoveryRates) { }
230
231}
degenerate base class for the Acyclic Visitor pattern
Definition: visitor.hpp:33
BankruptcyEvent(const Date &creditEventDate, const Currency &curr, Seniority bondsSen, const Date &settleDate, const std::map< Seniority, Real > &recoveryRates)
Currency specification
Definition: currency.hpp:36
Concrete date class.
Definition: date.hpp:125
DefaultSettlement(const Date &date, const std::map< Seniority, Real > &recoveryRates)
void accept(AcyclicVisitor &) override
Real recoveryRate(Seniority sen) const
std::map< Seniority, Real > recoveryRates_
Realized recovery rates.
Date date() const override
returns the date at which the event occurs
Credit event on a bond of a certain seniority(ies)/currency.
virtual Real recoveryRate(Seniority seniority) const
virtual bool matchesEventType(const ext::shared_ptr< DefaultType > &contractEvType) const
const Currency & currency() const
returns the currency of the bond this event refers to.
void accept(AcyclicVisitor &) override
DefaultEvent(const Date &creditEventDate, const DefaultType &atomicEvType, Currency curr, Seniority bondsSen, const Date &settleDate=Null< Date >(), const std::map< Seniority, Real > &recoveryRates=rate_map())
virtual bool matchesDefaultKey(const DefaultProbKey &contractKey) const
Date date() const override
returns the date at which the event occurs
DefaultSettlement defSettlement_
const DefaultType & defaultType() const
Seniority eventSeniority() const
returns the seniority of the bond that triggered the event.
const Currency & currency() const
const std::vector< ext::shared_ptr< DefaultType > > & eventTypes() const
Atomic credit-event type.
virtual void accept(AcyclicVisitor &)
Definition: event.cpp:41
virtual bool hasOccurred(const Date &refDate=Date(), ext::optional< bool > includeRefDate=ext::nullopt) const
returns true if an event has already occurred before a date
Definition: event.cpp:28
bool matchesEventType(const ext::shared_ptr< DefaultType > &contractEvType) const override
FailureToPayEvent(const Date &creditEventDate, const Currency &curr, Seniority bondsSen, Real defaultedAmount, const Date &settleDate, const std::map< Seniority, Real > &recoveryRates)
Failure to Pay atomic event type.
template class providing a null value for a given type.
Definition: null.hpp:76
DateProxy & evaluationDate()
the date at which pricing is to be performed.
Definition: settings.hpp:147
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
Visitor for a specific class
Definition: visitor.hpp:40
virtual void visit(T &)=0
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
bool operator==(const Currency &c1, const Currency &c2)
Definition: currency.hpp:191
std::map< Seniority, Real > makeIsdaConvMap()
Helper function for conventional recoveries. Returns the ISDA.
Seniority
Seniority of a bond.
Definition: defaulttype.hpp:37
STL namespace.
Atomic (single contractual event) default events.
Definition: defaulttype.hpp:60
Restructuring type.
Definition: defaulttype.hpp:84