QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
basket.hpp
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
25#ifndef quantlib_basket_hpp
26#define quantlib_basket_hpp
27
28#include <ql/instruments/claim.hpp>
29#include <ql/termstructures/defaulttermstructure.hpp>
30#include <ql/patterns/lazyobject.hpp>
31#include <ql/experimental/credit/defaultprobabilitykey.hpp>
32#include <ql/experimental/credit/issuer.hpp>
33#include <ql/experimental/credit/recoveryratemodel.hpp>
34#include <ql/experimental/credit/pool.hpp>
35#include <ql/experimental/credit/loss.hpp>
36
37namespace QuantLib {
38
39 class DefaultLossModel;
40
52 class Basket : public LazyObject {
53 public:
54 Basket() = default;
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 }
93 Size size() const;
95 const std::vector<std::string>& names() const {return pool_->names();}
97 const std::vector<Real>& notionals() const;
99 Real notional() const;
101 Real exposure(const std::string& name, const Date& = Date()) const;
103 const ext::shared_ptr<Pool>& pool() const;
105 std::vector<DefaultProbKey> defaultKeys() const;
110 const Date& refDate() const {return refDate_;}
126 ext::shared_ptr<Claim> claim() const {return claim_;}
130 std::vector<Probability> probabilities(const Date& d) const;
143 Real settledLoss() const;
144 Real settledLoss(const Date&) const;
154 Real cumulatedLoss() const;
155 Real cumulatedLoss(const Date&) const;
160 Real remainingNotional() const;
161 Real remainingNotional(const Date&) const;
165 const std::vector<Real>& remainingNotionals() const;
166 std::vector<Real> remainingNotionals(const Date&) const;
170 const std::vector<std::string>& remainingNames() const;
171 std::vector<std::string> remainingNames(const Date&) const;
174 const std::vector<DefaultProbKey>& remainingDefaultKeys() const;
175 std::vector<DefaultProbKey> remainingDefaultKeys(const Date&) const;
177 Size remainingSize() const;
178 Size remainingSize(const Date&) const;
182 std::vector<Probability> remainingProbabilities(const Date& d) const;
192 Real remainingAttachmentAmount(const Date& endDate) const;
193
203 Real remainingDetachmentAmount(const Date& endDate) const;
204
207 calculate();
209 }
213 Real remainingTrancheNotional(const Date& endDate) const {
214 calculate();
215 return remainingDetachmentAmount(endDate) -
217 }
219 const std::vector<Size>& liveList() const;
220 std::vector<Size> liveList(const Date&) const;//?? keep?
222 void setLossModel(
223 const ext::shared_ptr<DefaultLossModel>& lossModel);
230 Real expectedTrancheLoss(const Date& d) const;
235 Probability probOverLoss(const Date& d, Real lossFraction) const;
238 Real percentile(const Date& d, Probability prob) const;
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;
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;
259 std::vector<Probability> probsBeingNthEvent(
260 Size n, const Date& d) const;
264 Probability probAtLeastNEvents(Size n, const Date& d) const;
269 Real recoveryRate(const Date& d, Size iName) const;
271 private:
272 // LazyObject interface
273 void performCalculations() const override;
274
275 std::vector<Real> notionals_;
276 ext::shared_ptr<Pool> pool_;
278 const ext::shared_ptr<Claim> claim_;
279
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_;
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
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
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
Definition: any.hpp:35