This example shows the calculation of correlated defaults.
#include <ql/qldefines.hpp>
#ifdef BOOST_MSVC
# include <ql/auto_link.hpp>
#endif
#include <ql/experimental/credit/randomdefaultlatentmodel.hpp>
#include <ql/termstructures/credit/flathazardrate.hpp>
#include <ql/currencies/europe.hpp>
#include <ql/time/calendars/target.hpp>
#include <ql/time/daycounters/actual365fixed.hpp>
#include <iostream>
#include <iomanip>
using namespace std;
#if defined(QL_ENABLE_SESSIONS)
ThreadKey sessionId() { return 0; }
}
#endif
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") +
boost::lexical_cast<std::string>(i));
std::vector<Handle<DefaultProbabilityTermStructure> > defTS;
for(
Size i=0; i<hazardRates.size(); i++)
ext::make_shared<FlatHazardRate>(0,
TARGET(), hazardRates[i],
std::vector<Issuer> issuers;
for(
Size i=0; i<hazardRates.size(); i++) {
std::vector<QuantLib::Issuer::key_curve_pair> curves(1,
), defTS[i]));
issuers.push_back(Issuer(curves));
}
ext::shared_ptr<Pool> 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>(
ext::shared_ptr<Basket> 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
ext::shared_ptr<GaussianDefProbLM> lmG(new
LatentModelIntegrationType::GaussianQuadrature,
GaussianCopulaPolicy::initTraits()
));
#endif
std::vector<Integer> ordersT(2, 3);
ext::shared_ptr<TDefProbLM> lmT(
new TDefProbLM(fctrsWeights,
LatentModelIntegrationType::Trapezoid,
iniT));
Size numSimulations = 100000;
#ifndef QL_PATCH_SOLARIS
ext::shared_ptr<DefaultLossModel> rdlmG(
std::vector<Real>(), numSimulations, 1.e-6, 2863311530UL));
#endif
ext::shared_ptr<DefaultLossModel> rdlmT(
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 <<
#ifndef QL_PATCH_SOLARIS
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 <<
#ifndef QL_PATCH_SOLARIS
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;
}
}