21#include <ql/experimental/credit/loss.hpp>
22#include <ql/time/daycounters/actualactual.hpp>
24#include <boost/make_shared.hpp>
31Basket::Basket(
const Date& refDate,
const vector<string>& names,
const vector<Real>& notionals,
32 const QuantLib::ext::shared_ptr<Pool> pool, Real attachment, Real detachment,
33 const QuantLib::ext::shared_ptr<Claim>& claim)
34 : notionals_(notionals), pool_(pool), claim_(claim), attachmentRatio_(attachment), detachmentRatio_(detachment),
35 basketNotional_(0.0), attachmentAmount_(0.0), detachmentAmount_(0.0), trancheNotional_(0.0), refDate_(refDate) {
36 QL_REQUIRE(!
notionals_.empty(),
"notionals empty");
38 "invalid attachment/detachment ratio");
39 QL_REQUIRE(
pool_,
"Empty pool pointer.");
40 QL_REQUIRE(
notionals_.size() ==
pool_->size(),
"unmatched data entry sizes in basket, "
46 registerWith(Settings::instance().evaluationDate());
77 QL_REQUIRE(
lossModel_,
"Basket has no default loss model assigned.");
93 vector<Real> prob(
size());
95 for (Size j = 0; j <
size(); j++)
96 prob[j] =
pool_->get(
pool_->names()[j]).defaultProbability(defKeys[j])->defaultProbability(d);
102 QL_REQUIRE(endDate >=
refDate_,
"Target date lies before basket inception");
104 for (Size i = 0; i <
size(); i++) {
105 QuantLib::ext::shared_ptr<DefaultEvent> credEvent =
112 if (credEvent->hasSettled())
113 loss +=
claim_->amount(credEvent->date(),
116 credEvent->settlement().recoveryRate(
pool_->defaultKeys()[i].seniority()));
124 QL_REQUIRE(endDate >=
refDate_,
"Target date lies before basket inception");
127 for (Size i = 0; i <
size(); i++) {
128 QuantLib::ext::shared_ptr<DefaultEvent> credEvent =
131 if (credEvent->hasSettled()) {
132 loss +=
claim_->amount(credEvent->date(),
138 credEvent->settlement().recoveryRate(
pool_->defaultKeys()[i].seniority()));
152 std::vector<Size> calcBufferLiveList;
153 for (Size i = 0; i <
size(); i++)
155 calcBufferLiveList.push_back(i);
157 return calcBufferLiveList;
164 for (Size i = 0; i <
size(); i++) {
173 QL_REQUIRE(endDate >=
refDate_,
"Target date lies before basket inception");
175 std::vector<Real> calcBufferNotionals;
176 const std::vector<Size>& alive =
liveList(endDate);
177 for (Size i = 0; i < alive.size(); i++)
178 calcBufferNotionals.push_back(
exposure(
pool_->names()[i], endDate));
179 return calcBufferNotionals;
184 QL_REQUIRE(d >=
refDate_,
"Target date lies before basket inception");
186 const std::vector<Size>& alive =
liveList();
188 for (Size i = 0; i < alive.size(); i++)
190 pool_->get(
pool_->names()[i]).defaultProbability(
pool_->defaultKeys()[i])->defaultProbability(d,
true));
199 std::vector<std::string>::const_iterator match = std::find(
pool_->names().begin(),
pool_->names().end(), name);
200 QL_REQUIRE(match !=
pool_->names().end(),
"Name not in basket.");
201 Real totalNotional = 0.;
208 match = std::find(match,
pool_->names().end(), name);
209 }
while (match !=
pool_->names().end());
211 return totalNotional;
222 QL_REQUIRE(endDate >=
refDate_,
"Target date lies before basket inception");
224 const std::vector<Size>& alive =
liveList(endDate);
225 std::vector<std::string> calcBufferNames;
226 for (Size i = 0; i < alive.size(); i++)
227 calcBufferNames.push_back(
pool_->names()[alive[i]]);
228 return calcBufferNames;
233 QL_REQUIRE(endDate >=
refDate_,
"Target date lies before basket inception");
235 const std::vector<Size>& alive =
liveList(endDate);
236 vector<DefaultProbKey> defKeys;
237 for (Size i = 0; i < alive.size(); i++)
238 defKeys.push_back(
pool_->defaultKeys()[alive[i]]);
258 QL_REQUIRE(endDate >=
refDate_,
"Target date lies before basket inception");
305 return lossModel_->expectedShortfall(d, prob);
315 if (alreadyDefaulted >= n)
319 return lossModel_->probsBeingNthEvent(n - alreadyDefaulted, d);
324 return lossModel_->defaultCorrelation(d, iName, jName);
337 return lossModel_->expectedRecovery(d, iName,
pool_->defaultKeys()[iName]);
basket of issuers and related notionals
std::vector< Probability > probabilities(const Date &d) const
Real recoveryRate(const Date &d, Size iName) const
Real evalDateAttachAmount_
void performCalculations() const override
Real defaultCorrelation(const Date &d, Size iName, Size jName) const
Real detachmentAmount_
basket tranched inception detachment amount:
Real remainingAttachmentAmount() const
const std::vector< DefaultProbKey > & remainingDefaultKeys() const
std::vector< Size > evalDateLiveList_
Real attachmentAmount_
basket tranched inception attachment amount:
QuantLib::ext::shared_ptr< Pool > pool_
const QuantLib::ext::shared_ptr< Claim > claim_
The claim is the same for all names.
std::vector< DefaultProbKey > defaultKeys() const
The keys each counterparty enters the basket with (sensitive to)
QuantLib::Real correlation() const
void computeBasket() const
const Date refDate_
Basket inception date.
Probability probOverLoss(const Date &d, Real lossFraction) const
std::vector< Real > notionals_
Size remainingSize() const
Number of counterparties alive on the requested date.
Real exposure(const std::string &name, const Date &=Date()) const
Returns the total expected exposures for that name.
Real trancheNotional_
basket tranched notional amount:
Probability probAtLeastNEvents(Size n, const Date &d) const
Real evalDateRemainingNot_
const std::vector< Size > & liveList() const
Indexes of remaining names. Notice these are names and not positions.
Real percentile(const Date &d, Probability prob) const
Real expectedTrancheLoss(const Date &d, Real recoveryRate=Null< Real >()) const
QuantLib::ext::shared_ptr< QuantExt::DefaultLossModel > lossModel_
std::vector< Probability > probsBeingNthEvent(Size n, const Date &d) const
Real remainingDetachmentAmount() const
Size size() const
Basket inception number of counterparties.
const std::vector< std::string > & remainingNames() const
Real expectedShortfall(const Date &d, Probability prob) const
std::vector< Real > splitVaRLevel(const Date &date, Real loss) const
Real cumulatedLoss() const
void setLossModel(const QuantLib::ext::shared_ptr< QuantExt::DefaultLossModel > &lossModel)
Assigns the default loss model to this basket. Resets calculations.
const std::vector< Real > & remainingNotionals() const
std::vector< Probability > remainingProbabilities(const Date &d) const
Real remainingNotional() const
std::map< Real, Probability > lossDistribution(const Date &) const