QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
basket.hpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 Roland Lichters
5 Copyright (C) 2009, 2014 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/*! \file basket.hpp
22 \brief basket of issuers and related notionals
23*/
24
25#ifndef quantlib_basket_hpp
26#define quantlib_basket_hpp
27
36
37namespace QuantLib {
38
39 class DefaultLossModel;
40
41 /*! Credit Basket.\par
42 A basket is a collection of credit names, represented by a
43 unique identifier (a text string), associated notional
44 amounts, a pool and tranche information. The pool is a map of
45 "names" to issuers. The Basket structure is motivated by CDO
46 squared instruments containing various underlying inner CDOs
47 which can be represented by respective baskets including their
48 tranche structure. The role of the Pool is providing a unique
49 list of relevant issuers while names may appear multiple times
50 across different baskets (overlap).
51 */
52 class Basket : public LazyObject {
53 public:
54 Basket() = default;
55 /*! Constructs a basket of simple collection of constant notional
56 positions subject to default risk only.
57
58 The refDate parameter is the basket inception date, that is,
59 the date at which defaultable events are relevant. (There
60 are no constraints on forward baskets but models assigned
61 should be consistent.)
62 */
63 Basket(const Date& refDate,
64 const std::vector<std::string>& names,
65 std::vector<Real> notionals,
66 ext::shared_ptr<Pool> pool,
69 ext::shared_ptr<Claim> claim = ext::shared_ptr<Claim>(new FaceValueClaim()));
70 void update() override {
73 }
74 void computeBasket() const {
76 /* update cache values at the calculation date (work as arguments
77 to the Loss Models)
78 \to do: IMPORTANT: notice that defaults added to Issuers dont get
79 notify as the codes stnds today. Issuers need to be observables.
80 */
81 //this one must remain on top since there are dependencies
91 }
92 //! Basket inception number of counterparties.
93 Size size() const;
94 //! Basket counterparties names at inception.
95 const std::vector<std::string>& names() const {return pool_->names();}
96 //! Basket counterparties notionals at inception.
97 const std::vector<Real>& notionals() const;
98 //! Basket total notional at inception.
99 Real notional() const;
100 //! Returns the total expected exposures for that name.
101 Real exposure(const std::string& name, const Date& = Date()) const;
102 //! Underlying pool
103 const ext::shared_ptr<Pool>& pool() const;
104 //! The keys each counterparty enters the basket with (sensitive to)
105 std::vector<DefaultProbKey> defaultKeys() const;
106 /*! Loss Given Default for all issuers/notionals based on
107 expected recovery rates for the respective issuers.
108 */
109 //! Basket inception date.
110 const Date& refDate() const {return refDate_;}
111 /*! Attachment point expressed as a fraction of the total inception
112 notional.
113 */
115 //! Detachment point expressed as a fraction of the total pool notional
117 //! Original basket notional ignoring any losses.
119 //! Original tranche notional ignoring any realized losses.
121 //! Attachment amount = attachmentRatio() * basketNotional()
123 //! Detachment amount = detachmentRatio() * basketNotional()
125 //! default claim, same for all positions and counterparties
126 ext::shared_ptr<Claim> claim() const {return claim_;}
127 /*! Vector of cumulative default probability to date d for all
128 issuers in the basket.
129 */
130 std::vector<Probability> probabilities(const Date& d) const;
131 /*! Realized basket losses between the reference date and the
132 calculation date, taking the actual recovery rates of loss events
133 into account.
134 Only default events that have settled (have a realized RR) are
135 accounted for. For contingent losses after a default you need
136 to compute the losses through a DefaultLossModel
137
138 Optionally one can pass a date in the future and that will collect
139 events stored in the issuers list. This shows the effect of
140 'programmed' (after today's) events on top of past ones. The
141 intention is to be used in risk analysis (jump to default, etc).
142 */
143 Real settledLoss() const;
144 Real settledLoss(const Date&) const;
145 /*! Actual basket losses between the reference date and the calculation
146 date, taking the actual recovery rates of loss events into account.
147 If the event has not settled yet a model driven recovery is used.
148
149 Returns the realized losses in this portfolio since the portfolio
150 default reference date.
151 This method relies on an implementation of the loss given default
152 since the events have not necessarily settled.
153 */
154 Real cumulatedLoss() const;
155 Real cumulatedLoss(const Date&) const;
156 /*! Remaining full basket (untranched) notional after settled losses
157 between the reference date and the given date. The full notional
158 for defaulted names is subracted, recovery ignored.
159 */
160 Real remainingNotional() const;
161 Real remainingNotional(const Date&) const;
162 /*! Vector of surviving notionals after settled losses between the
163 reference date and the given date, recovery ignored.
164 */
165 const std::vector<Real>& remainingNotionals() const;
166 std::vector<Real> remainingNotionals(const Date&) const;
167 /*! Vector of surviving issuers after defaults between the reference
168 basket date and the given (or evaluation) date.
169 */
170 const std::vector<std::string>& remainingNames() const;
171 std::vector<std::string> remainingNames(const Date&) const;
172 /*! Default keys of non defaulted counterparties
173 */
174 const std::vector<DefaultProbKey>& remainingDefaultKeys() const;
175 std::vector<DefaultProbKey> remainingDefaultKeys(const Date&) const;
176 //! Number of counterparties alive on the requested date.
177 Size remainingSize() const;
178 Size remainingSize(const Date&) const;
179 /*! Vector of cumulative default probability to date d for all
180 issuers still (at the evaluation date) alive in the basket.
181 */
182 std::vector<Probability> remainingProbabilities(const Date& d) const;
183 /*!
184 Attachment amount of the equivalent (after defaults) remaining basket
185 The remaining attachment amount is
186 RAA = max (0, attachmentAmount - cumulatedLoss())
187
188 The remaining attachment ratio is then
189 RAR = RAA / remainingNotional()
190 */
192 Real remainingAttachmentAmount(const Date& endDate) const;
193
194 /*!
195 Detachment amount of the equivalent remaining basket.
196 The remaining detachment amount is
197 RDA = max (0, detachmentAmount - cumulatedLoss())
198
199 The remaining detachment ratio is then
200 RDR = RDA / remainingNotional()
201 */
203 Real remainingDetachmentAmount(const Date& endDate) const;
204
205 //! Remaining basket tranched notional on calculation date
207 calculate();
209 }
210 /*! Expected basket tranched notional on the requested date
211 according to the basket model. Model should have been assigned.
212 */
213 Real remainingTrancheNotional(const Date& endDate) const {
214 calculate();
215 return remainingDetachmentAmount(endDate) -
217 }
218 //!Indexes of remaining names. Notice these are names and not positions.
219 const std::vector<Size>& liveList() const;
220 std::vector<Size> liveList(const Date&) const;//?? keep?
221 //! Assigns the default loss model to this basket. Resets calculations.
222 void setLossModel(
223 const ext::shared_ptr<DefaultLossModel>& lossModel);
224 /*! \name Basket Loss Statistics
225 Methods providing statistical metrics on the loss or value
226 distribution of the basket. Most calculations rely on the pressence
227 of a model assigned to the basket.
228 */
229 //@{
230 Real expectedTrancheLoss(const Date& d) const;
231 /*! The lossFraction is the fraction of losses expressed in
232 inception (no losses) tranche units (e.g. 'attach level'=0%,
233 'detach level'=100%)
234 */
235 Probability probOverLoss(const Date& d, Real lossFraction) const;
236 /*!
237 */
238 Real percentile(const Date& d, Probability prob) const;
239 /*! ESF
240 */
241 Real expectedShortfall(const Date& d, Probability prob) const;
242 /* Split a portfolio loss along counterparties. Typically loss
243 corresponds to some percentile.*/
244 std::vector<Real> splitVaRLevel(const Date& date, Real loss) const;
245 /*! Full loss distribution
246 */
247 std::map<Real, Probability> lossDistribution(const Date&) const;
248 Real densityTrancheLoss(const Date& d, Real lossFraction) const;
249 Real defaultCorrelation(const Date& d, Size iName, Size jName) const;
250 /*! Probability vector that each of the remaining live names (at eval
251 date) is the n-th default by date d.
252
253 The n parameter is the internal index to the name; it should
254 be alive at the evaluation date.
255
256 ---------TO DO: Implement with a string passed----------------------
257 ---------TO DO: Perform check the name is alive---------------------
258 */
259 std::vector<Probability> probsBeingNthEvent(
260 Size n, const Date& d) const;
261 /*! Returns the probaility of having a given or larger number of
262 defaults in the basket portfolio at a given time.
263 */
264 Probability probAtLeastNEvents(Size n, const Date& d) const;
265 /*! Expected recovery rate of the underlying position as a fraction of
266 its exposure value at date d _given_ it has defaulted _on_ that date.
267 NOTICE THE ARG IS THE CTPTY....SHOULDNT IT BE THE POSITION/INSTRUMENT?????<<<<<<<<<<<<<<<<<<<<<<<
268 */
269 Real recoveryRate(const Date& d, Size iName) const;
270 //@}
271 private:
272 // LazyObject interface
273 void performCalculations() const override;
274
275 std::vector<Real> notionals_;
276 ext::shared_ptr<Pool> pool_;
277 //! The claim is the same for all names
278 const ext::shared_ptr<Claim> claim_;
279
283 //! basket tranched inception attachment amount:
285 //! basket tranched inception detachment amount:
287 //! basket tranched notional amount:
289 /* Caches. Most of the times one wants statistics on the distribution of
290 futures losses at arbitrary dates but some problems (e.g. derivatives
291 pricing) work with todays (evalDate) magnitudes which do not require a
292 loss model and would be too expensive to recompute on every call.
293 */
298 mutable std::vector<Size> evalDateLiveList_;
299 mutable std::vector<Real> evalDateLiveNotionals_;
300 mutable std::vector<std::string> evalDateLiveNames_;
301 mutable std::vector<DefaultProbKey> evalDateLiveKeys_;
302 //! Basket inception date.
304 /* It is the basket responsibility to ensure that the model assigned it
305 is properly initialized to the basket current data.
306 This might not be the case for various reasons: the basket data might
307 have been updated, the evaluation date has changed or the model has
308 received another request from another basket pointing to it. For
309 this last reason we can never be sure between calls that this is the
310 case (and that is true in a single thread environment only).
311 */
312 ext::shared_ptr<DefaultLossModel> lossModel_;
313 };
314
315 // ------------ Inlines -------------------------------------------------
316
317 inline Size Basket::size() const {
318 return pool_->size();
319 }
320
321 inline const std::vector<Real>& Basket::notionals() const {
322 return notionals_;
323 }
324
325 inline std::vector<DefaultProbKey> Basket::defaultKeys() const {
326 return pool_->defaultKeys();
327 }
328
329 inline const ext::shared_ptr<Pool>& Basket::pool() const {
330 return pool_;
331 }
332
333 inline const std::vector<Size>& Basket::liveList() const {
334 return evalDateLiveList_;
335 }
336
339 }
340
343 }
344
345 inline const std::vector<std::string>& Basket::remainingNames() const {
346 return evalDateLiveNames_;
347 }
348
349 inline const std::vector<Real>& Basket::remainingNotionals() const {
351 }
352
354 return this->evalDateSettledLoss_;
355 }
356
357 inline Real Basket::settledLoss() const {
359 }
360
361 inline const std::vector<DefaultProbKey>&
363 {
364 return evalDateLiveKeys_;
365 }
366
367}
368
369
370#endif
Classes for default-event claims.
std::vector< Probability > probabilities(const Date &d) const
Definition: basket.cpp:110
Real recoveryRate(const Date &d, Size iName) const
Definition: basket.cpp:369
Real evalDateAttachAmount_
Definition: basket.hpp:296
void performCalculations() const override
Definition: basket.cpp:88
const ext::shared_ptr< Claim > claim_
The claim is the same for all names.
Definition: basket.hpp:278
Real defaultCorrelation(const Date &d, Size iName, Size jName) const
Definition: basket.cpp:354
Real detachmentAmount_
basket tranched inception detachment amount:
Definition: basket.hpp:286
Real remainingAttachmentAmount() const
Definition: basket.hpp:341
ext::shared_ptr< Claim > claim() const
default claim, same for all positions and counterparties
Definition: basket.hpp:126
Real detachmentAmount() const
Detachment amount = detachmentRatio() * basketNotional()
Definition: basket.hpp:124
const std::vector< DefaultProbKey > & remainingDefaultKeys() const
Definition: basket.hpp:362
Real evalDateSettledLoss_
Definition: basket.hpp:294
std::vector< DefaultProbKey > evalDateLiveKeys_
Definition: basket.hpp:301
std::vector< Real > evalDateLiveNotionals_
Definition: basket.hpp:299
ext::shared_ptr< Pool > pool_
Definition: basket.hpp:276
const std::vector< Real > & notionals() const
Basket counterparties notionals at inception.
Definition: basket.hpp:321
Real remainingTrancheNotional() const
Remaining basket tranched notional on calculation date.
Definition: basket.hpp:206
std::vector< Size > evalDateLiveList_
Definition: basket.hpp:298
Real attachmentAmount_
basket tranched inception attachment amount:
Definition: basket.hpp:284
void update() override
Definition: basket.hpp:70
Real detachmentRatio() const
Detachment point expressed as a fraction of the total pool notional.
Definition: basket.hpp:116
Real attachmentRatio() const
Definition: basket.hpp:114
std::vector< DefaultProbKey > defaultKeys() const
The keys each counterparty enters the basket with (sensitive to)
Definition: basket.hpp:325
Basket()=default
Real attachmentAmount() const
Attachment amount = attachmentRatio() * basketNotional()
Definition: basket.hpp:122
void computeBasket() const
Definition: basket.hpp:74
Real detachmentRatio_
Definition: basket.hpp:281
const Date refDate_
Basket inception date.
Definition: basket.hpp:303
Probability probOverLoss(const Date &d, Real lossFraction) const
Definition: basket.cpp:299
std::vector< Real > notionals_
Definition: basket.hpp:275
Size remainingSize() const
Number of counterparties alive on the requested date.
Definition: basket.cpp:273
const ext::shared_ptr< Pool > & pool() const
Underlying pool.
Definition: basket.hpp:329
Real remainingTrancheNotional(const Date &endDate) const
Definition: basket.hpp:213
Real exposure(const std::string &name, const Date &=Date()) const
Returns the total expected exposures for that name.
Definition: basket.cpp:224
Real densityTrancheLoss(const Date &d, Real lossFraction) const
Real trancheNotional_
basket tranched notional amount:
Definition: basket.hpp:288
Probability probAtLeastNEvents(Size n, const Date &d) const
Definition: basket.cpp:363
Real evalDateRemainingNot_
Definition: basket.hpp:295
const std::vector< Size > & liveList() const
Indexes of remaining names. Notice these are names and not positions.
Definition: basket.hpp:333
const std::vector< std::string > & names() const
Basket counterparties names at inception.
Definition: basket.hpp:95
const Date & refDate() const
Basket inception date.
Definition: basket.hpp:110
Real percentile(const Date &d, Probability prob) const
Definition: basket.cpp:318
ext::shared_ptr< DefaultLossModel > lossModel_
Definition: basket.hpp:312
Real expectedTrancheLoss(const Date &d) const
Definition: basket.cpp:323
std::vector< std::string > evalDateLiveNames_
Definition: basket.hpp:300
void setLossModel(const ext::shared_ptr< DefaultLossModel > &lossModel)
Assigns the default loss model to this basket. Resets calculations.
Definition: basket.cpp:74
Real notional() const
Basket total notional at inception.
Definition: basket.cpp:106
Real attachmentRatio_
Definition: basket.hpp:280
std::vector< Probability > probsBeingNthEvent(Size n, const Date &d) const
Definition: basket.cpp:344
Real basketNotional() const
Original basket notional ignoring any losses.
Definition: basket.hpp:118
Real trancheNotional() const
Original tranche notional ignoring any realized losses.
Definition: basket.hpp:120
Real remainingDetachmentAmount() const
Definition: basket.hpp:337
Size size() const
Basket inception number of counterparties.
Definition: basket.hpp:317
const std::vector< std::string > & remainingNames() const
Definition: basket.hpp:345
Real expectedShortfall(const Date &d, Probability prob) const
Definition: basket.cpp:333
std::vector< Real > splitVaRLevel(const Date &date, Real loss) const
Definition: basket.cpp:328
Real cumulatedLoss() const
Definition: basket.hpp:353
const std::vector< Real > & remainingNotionals() const
Definition: basket.hpp:349
Real evalDateDetachAmmount_
Definition: basket.hpp:297
std::vector< Probability > remainingProbabilities(const Date &d) const
Definition: basket.cpp:210
Real basketNotional_
Definition: basket.hpp:282
Real remainingNotional() const
Definition: basket.cpp:168
Real settledLoss() const
Definition: basket.hpp:357
std::map< Real, Probability > lossDistribution(const Date &) const
Definition: basket.cpp:338
Concrete date class.
Definition: date.hpp:125
Claim on a notional.
Definition: claim.hpp:43
Framework for calculation on demand and result caching.
Definition: lazyobject.hpp:35
virtual void calculate() const
Definition: lazyobject.hpp:253
void update() override
Definition: lazyobject.hpp:188
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
Classes for default-event description.
default-probability term structure
Date d
QL_REAL Real
real number
Definition: types.hpp:50
Real Probability
probability
Definition: types.hpp:82
std::size_t Size
size of a container
Definition: types.hpp:58
Classes for credit-name handling.
framework for calculation on demand and result caching
Pair of loss time and amount, sortable by loss time.
Definition: any.hpp:35
pool of issuers