This example shows the calculation of correlated defaults.
#if !defined(BOOST_ALL_NO_LIB) && defined(BOOST_MSVC)
#endif
#include <string>
#include <iostream>
#include <iomanip>
int main(int, char* []) {
try {
std::cout << std::endl;
Date todaysDate(19, March, 2014);
todaysDate = calendar.
adjust(todaysDate);
Settings::instance().evaluationDate() = todaysDate;
std::vector<Real> hazardRates(3, -std::log(1.-0.01));
std::vector<std::string> names;
for(
Size i=0; i<hazardRates.size(); i++)
names.push_back(std::string("Acme") + std::to_string(i));
std::vector<Handle<DefaultProbabilityTermStructure>> defTS;
defTS.reserve(hazardRates.size());
for (
Real& hazardRate : hazardRates)
defTS.emplace_back(
std::vector<Issuer> issuers;
for(
Size i=0; i<hazardRates.size(); i++) {
std::vector<QuantLib::Issuer::key_curve_pair> curves(1,
), defTS[i]));
issuers.emplace_back(curves);
}
auto thePool = ext::make_shared<Pool>();
for(
Size i=0; i<hazardRates.size(); i++)
std::vector<DefaultProbKey> defaultKeys(hazardRates.size(),
std::vector<ext::shared_ptr<RecoveryRateModel>> rrModels(
hazardRates.size(), ext::make_shared<ConstantRecoveryModel>(
auto theBskt = ext::make_shared<Basket>(
todaysDate, names, std::vector<Real>(hazardRates.size(), 100.),
thePool);
std::vector<std::vector<Real>> fctrsWeights(hazardRates.size(),
std::vector<Real>(1, std::sqrt(0.1)));
#ifndef QL_PATCH_SOLARIS
auto lmG = ext::make_shared<GaussianDefProbLM>(fctrsWeights,
LatentModelIntegrationType::GaussianQuadrature,
);
#endif
std::vector<Integer> ordersT(2, 3);
auto lmT = ext::make_shared<TDefProbLM>(fctrsWeights,
LatentModelIntegrationType::Trapezoid, iniT);
Size numSimulations = 100000;
#ifndef QL_PATCH_SOLARIS
auto rdlmG = ext::make_shared<RandomDefaultLM<GaussianCopulaPolicy>>(lmG,
std::vector<Real>(), numSimulations, 1.e-6, 2863311530UL);
#endif
auto rdlmT = ext::make_shared<RandomDefaultLM<TCopulaPolicy>>(lmT,
std::vector<Real>(), numSimulations, 1.e-6, 2863311530UL);
Date calcDate(
TARGET().advance(Settings::instance().evaluationDate(),
std::vector<Probability> probEventsTLatent, probEventsTRandLoss;
#ifndef QL_PATCH_SOLARIS
std::vector<Probability> probEventsGLatent, probEventsGRandLoss;
#endif
lmT->resetBasket(theBskt);
for(
Size numEvts=0; numEvts <=theBskt->size(); numEvts++) {
probEventsTLatent.push_back(lmT->probAtLeastNEvents(numEvts,
calcDate));
}
theBskt->setLossModel(rdlmT);
for(
Size numEvts=0; numEvts <=theBskt->size(); numEvts++) {
probEventsTRandLoss.push_back(theBskt->probAtLeastNEvents(numEvts,
calcDate));
}
#ifndef QL_PATCH_SOLARIS
lmG->resetBasket(theBskt);
for(
Size numEvts=0; numEvts <=theBskt->size(); numEvts++) {
probEventsGLatent.push_back(lmG->probAtLeastNEvents(numEvts,
calcDate));
}
theBskt->setLossModel(rdlmG);
for(
Size numEvts=0; numEvts <=theBskt->size(); numEvts++) {
probEventsGRandLoss.push_back(theBskt->probAtLeastNEvents(numEvts,
calcDate));
}
#endif
Settings::instance().evaluationDate(),
Period(12, Months));
std::vector<std::vector<Real>> correlsGlm, correlsTlm, correlsGrand,
correlsTrand;
lmT->resetBasket(theBskt);
for(
Size iName1=0; iName1 <theBskt->size(); iName1++) {
std::vector<Real> tmp;
for(
Size iName2=0; iName2 <theBskt->size(); iName2++)
tmp.push_back(lmT->defaultCorrelation(correlDate,
iName1, iName2));
correlsTlm.push_back(tmp);
}
theBskt->setLossModel(rdlmT);
for(
Size iName1=0; iName1 <theBskt->size(); iName1++) {
std::vector<Real> tmp;
for(
Size iName2=0; iName2 <theBskt->size(); iName2++)
tmp.push_back(theBskt->defaultCorrelation(correlDate,
iName1, iName2));
correlsTrand.push_back(tmp);
}
#ifndef QL_PATCH_SOLARIS
lmG->resetBasket(theBskt);
for(
Size iName1=0; iName1 <theBskt->size(); iName1++) {
std::vector<Real> tmp;
for(
Size iName2=0; iName2 <theBskt->size(); iName2++)
tmp.push_back(lmG->defaultCorrelation(correlDate,
iName1, iName2));
correlsGlm.push_back(tmp);
}
theBskt->setLossModel(rdlmG);
for(
Size iName1=0; iName1 <theBskt->size(); iName1++) {
std::vector<Real> tmp;
for(
Size iName2=0; iName2 <theBskt->size(); iName2++)
tmp.push_back(theBskt->defaultCorrelation(correlDate,
iName1, iName2));
correlsGrand.push_back(tmp);
}
#endif
std::cout <<
" Gaussian versus T prob of extreme event (random and integrable)-"
<< std::endl;
for(
Size numEvts=0; numEvts <=theBskt->size(); numEvts++) {
std::cout << "-Prob of " << numEvts << " events... " <<
#ifndef QL_PATCH_SOLARIS
probEventsGLatent[numEvts] << " ** " <<
#else
"n/a" << " ** " <<
#endif
probEventsTLatent[numEvts] << " ** " <<
#ifndef QL_PATCH_SOLARIS
probEventsGRandLoss[numEvts]<< " ** " <<
#else
"n/a" << " ** " <<
#endif
probEventsTRandLoss[numEvts]
<< std::endl;
}
cout << endl;
cout << "-- Default correlations G,T,GRand,TRand--" << endl;
cout << "-----------------------------------------" << endl;
for(
Size iName1=0; iName1 <theBskt->size(); iName1++) {
for(
Size iName2=0; iName2 <theBskt->size(); iName2++)
cout <<
correlsGlm[iName1][iName2] << " , ";
#else
"n/a" << " , ";
#endif
cout << endl;
}
cout << endl;
for(
Size iName1=0; iName1 <theBskt->size(); iName1++) {
for(
Size iName2=0; iName2 <theBskt->size(); iName2++)
cout <<
correlsTlm[iName1][iName2] << " , ";
;
cout << endl;
}
cout << endl;
for(
Size iName1=0; iName1 <theBskt->size(); iName1++) {
for(
Size iName2=0; iName2 <theBskt->size(); iName2++)
cout <<
correlsGrand[iName1][iName2] << " , ";
#else
"n/a" << " , ";
#endif
cout << endl;
}
cout << endl;
for(
Size iName1=0; iName1 <theBskt->size(); iName1++) {
for(
Size iName2=0; iName2 <theBskt->size(); iName2++)
cout <<
correlsTrand[iName1][iName2] << " , ";
;
cout << endl;
}
return 0;
} catch (exception& e) {
cerr << e.what() << endl;
return 1;
} catch (...) {
cerr << "unknown error" << endl;
return 1;
}
}
Actual/365 (Fixed) day counter.
Actual/365 (Fixed) day count convention.
Date adjust(const Date &, BusinessDayConvention convention=Following) const
Date advance(const Date &, Integer n, TimeUnit unit, BusinessDayConvention convention=Following, bool endOfMonth=false) const
ISDA standard default contractual key for corporate US debt.
flat hazard-rate term structure
std::size_t Size
size of a container
Global definitions and compiler switches.
std::vector< Integer > tOrders