This example shows how to model losses across correlated assets.
#if !defined(BOOST_ALL_NO_LIB) && defined(BOOST_MSVC)
#endif
#include <iostream>
#include <iomanip>
#include <string>
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 = {
0.001, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09
};
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;
for (
Real& hazardRate : hazardRates) {
defTS.emplace_back(
defTS.back()->enableExtrapolation();
}
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(),
auto theBskt = ext::make_shared<Basket>(
todaysDate,
names, std::vector<Real>(hazardRates.size(), 100.), thePool,
0.03, .06);
std::vector<Real> recoveries(hazardRates.size(), 0.4);
Date calcDate(
TARGET().advance(Settings::instance().evaluationDate(),
std::vector<std::vector<Real>> fctrsWeights(hazardRates.size(),
std::vector<Real>(1, std::sqrt(factorValue)));
#ifndef QL_PATCH_SOLARIS
auto lmGLHP = ext::make_shared<GaussianLHPLossModel>(
fctrsWeights[0][0] * fctrsWeights[0][0], recoveries);
theBskt->setLossModel(lmGLHP);
std::cout << "GLHP Expected 10-Yr Losses: " << std::endl;
std::cout << theBskt->expectedTrancheLoss(calcDate) << std::endl;
auto ktLossLM = ext::make_shared<GaussianConstantLossLM>(fctrsWeights,
recoveries, LatentModelIntegrationType::GaussianQuadrature,
auto lmBinomial = ext::make_shared<GaussianBinomialLossModel>(ktLossLM);
theBskt->setLossModel(lmBinomial);
std::cout << "Gaussian Binomial Expected 10-Yr Losses: " << std::endl;
std::cout << theBskt->expectedTrancheLoss(calcDate) << std::endl;
#endif
initT.
tOrders = std::vector<Integer>(2, 3);
auto ktTLossLM = ext::make_shared<TConstantLossLM>(fctrsWeights,
recoveries,
LatentModelIntegrationType::Trapezoid,
initT);
auto lmTBinomial = ext::make_shared<TBinomialLossModel>(ktTLossLM);
theBskt->setLossModel(lmTBinomial);
std::cout << "T Binomial Expected 10-Yr Losses: " << std::endl;
std::cout << theBskt->expectedTrancheLoss(calcDate) << std::endl;
Size numSimulations = 100000;
#ifndef QL_PATCH_SOLARIS
auto gLM = ext::make_shared<GaussianConstantLossLM>(fctrsWeights,
recoveries,
LatentModelIntegrationType::GaussianQuadrature,
auto inhomogeneousLM = ext::make_shared<IHGaussPoolLossModel>(gLM, numBuckets);
theBskt->setLossModel(inhomogeneousLM);
std::cout << "G Inhomogeneous Expected 10-Yr Losses: " << std::endl;
std::cout << theBskt->expectedTrancheLoss(calcDate) << std::endl;
recoveries, numSimulations, 1.e-6, 2863311530UL);
theBskt->setLossModel(rdlmG);
std::cout << "Random G Expected 10-Yr Losses: " << std::endl;
std::cout << theBskt->expectedTrancheLoss(calcDate) << std::endl;
#endif
recoveries, numSimulations, 1.e-6, 2863311530UL);
theBskt->setLossModel(rdlmT);
std::cout << "Random T Expected 10-Yr Losses: " << std::endl;
std::cout << theBskt->expectedTrancheLoss(calcDate) << std::endl;
#ifndef QL_PATCH_SOLARIS
std::vector<std::vector<Real>> fctrsWeightsRR(2 * hazardRates.size(),
std::vector<Real>(1, std::sqrt(factorValue)));
auto sptLG = ext::make_shared<GaussianSpotLossLM>(
fctrsWeightsRR, recoveries, modelA,
LatentModelIntegrationType::GaussianQuadrature,
auto sptLT = ext::make_shared<TSpotLossLM>(fctrsWeightsRR,
recoveries, modelA,
LatentModelIntegrationType::GaussianQuadrature, initT);
auto rdLlmG = ext::make_shared<RandomLossLM<GaussianCopulaPolicy>>(sptLG,
numSimulations, 1.e-6, 2863311530UL);
theBskt->setLossModel(rdLlmG);
std::cout << "Random Loss G Expected 10-Yr Losses: " << std::endl;
std::cout << theBskt->expectedTrancheLoss(calcDate) << std::endl;
auto rdLlmT = ext::make_shared<RandomLossLM<TCopulaPolicy>>(sptLT,
numSimulations, 1.e-6, 2863311530UL);
theBskt->setLossModel(rdLlmT);
std::cout << "Random Loss T Expected 10-Yr Losses: " << std::endl;
std::cout << theBskt->expectedTrancheLoss(calcDate) << std::endl;
std::vector<Period> bcTenors = {{1, Years}, {5, Years}};
std::vector<Real> bcLossPercentages = {0.03, 0.12};
std::vector<std::vector<Handle<Quote>>> correls;
std::vector<Handle<Quote>> corr1Y;
corr1Y.emplace_back(
ext::make_shared<SimpleQuote>(fctrsWeights[0][0] * fctrsWeights[0][0]));
corr1Y.emplace_back(
ext::make_shared<SimpleQuote>(fctrsWeights[0][0] * fctrsWeights[0][0]));
correls.push_back(corr1Y);
std::vector<Handle<Quote>> corr2Y;
corr2Y.emplace_back(
ext::make_shared<SimpleQuote>(fctrsWeights[0][0] * fctrsWeights[0][0]));
corr2Y.emplace_back(
ext::make_shared<SimpleQuote>(fctrsWeights[0][0] * fctrsWeights[0][0]));
correls.push_back(corr2Y);
auto correlSurface = ext::make_shared<BaseCorrelationTermStructure<BilinearInterpolation>>(
defTS[0]->settlementDays(),
defTS[0]->calendar(),
Unadjusted,
bcTenors,
bcLossPercentages,
correls,
auto bcLMG_LHP_Bilin = ext::make_shared<GaussianLHPFlatBCLM>(correlHandle, recoveries,
theBskt->setLossModel(bcLMG_LHP_Bilin);
std::cout << "Base Correlation GLHP Expected 10-Yr Losses: "
<< std::endl;
std::cout << theBskt->expectedTrancheLoss(calcDate) << std::endl;
#endif
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.
Gaussian random number generator.
Date adjust(const Date &, BusinessDayConvention convention=Following) const
Shared handle to an observable.
ISDA standard default contractual key for corporate US debt.
Student t random number generator.
Random sequence generator based on a pseudo-random number generator.
Student-T Latent Model's copula policy.
flat hazard-rate term structure
std::size_t Size
size of a container
Global definitions and compiler switches.
std::vector< Integer > tOrders