Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Functions
test_hullwhitebucketing Namespace Reference

Functions

void computeDiscreteDistribution (std::vector< std::vector< double > >::iterator beginPDs, std::vector< std::vector< double > >::iterator endPDs, std::vector< std::vector< double > >::iterator beginLGDs, double runningDensity, double runningLoss, std::map< double, double > &dist)
 
std::map< double, double > lossDistribution (const std::vector< double > &pds, const std::vector< double > &lgds)
 
std::map< double, double > lossDistribution (const std::vector< vector< double > > &pds, const std::vector< vector< double > > &lgds)
 
std::ostream & operator<< (std::ostream &os, const BucketedDistribution &dist)
 
double expectedTrancheLoss (const std::map< double, double > &dist, double detachmentPoint)
 
double expectedTrancheLoss (QuantLib::Distribution dist, double attachmentAmount, double detachmentAmount)
 
double expectedTrancheLoss (const vector< double > &prob, const vector< double > &loss, double detachment)
 

Function Documentation

◆ computeDiscreteDistribution()

void computeDiscreteDistribution ( std::vector< std::vector< double > >::iterator  beginPDs,
std::vector< std::vector< double > >::iterator  endPDs,
std::vector< std::vector< double > >::iterator  beginLGDs,
double  runningDensity,
double  runningLoss,
std::map< double, double > &  dist 
)

Definition at line 46 of file hullwhitebucketing.cpp.

49 {
50 if (beginPDs == endPDs) {
51 dist[runningLoss] = dist[runningLoss] + runningDensity;
52 } else {
53 for (size_t eventId = 0; eventId < beginPDs->size(); eventId++) {
54 double loss = runningLoss + beginLGDs->at(eventId);
55 double density = runningDensity * beginPDs->at(eventId);
56 computeDiscreteDistribution(beginPDs + 1, endPDs, beginLGDs + 1, density, loss, dist);
57 }
58 }
59}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ lossDistribution() [1/2]

std::map< double, double > lossDistribution ( const std::vector< double > &  pds,
const std::vector< double > &  lgds 
)

Definition at line 61 of file hullwhitebucketing.cpp.

61 {
62
63 QL_REQUIRE(pds.size() == lgds.size(), "Mismatch number of pds and lgds");
64
65 vector<vector<double>> statePDs;
66 vector<vector<double>> stateLGDs;
67 for (const auto& pd : pds) {
68 statePDs.push_back({pd, 1 - pd});
69 }
70 for (const auto& lgd : lgds) {
71 stateLGDs.push_back({lgd, 0});
72 }
73 std::map<double, double> dist;
74 computeDiscreteDistribution(statePDs.begin(), statePDs.end(), stateLGDs.begin(), 1.0, 0.0, dist);
75 return dist;
76}
void computeDiscreteDistribution(std::vector< std::vector< double > >::iterator beginPDs, std::vector< std::vector< double > >::iterator endPDs, std::vector< std::vector< double > >::iterator beginLGDs, double runningDensity, double runningLoss, std::map< double, double > &dist)
+ Here is the call graph for this function:

◆ lossDistribution() [2/2]

std::map< double, double > lossDistribution ( const std::vector< vector< double > > &  pds,
const std::vector< vector< double > > &  lgds 
)

Definition at line 78 of file hullwhitebucketing.cpp.

79 {
80
81 QL_REQUIRE(pds.size() == lgds.size(), "Mismatch number of pds and lgds");
82
83 vector<vector<double>> statePDs;
84 vector<vector<double>> stateLGDs;
85 for (const auto& pd : pds) {
86 statePDs.push_back(pd);
87 statePDs.back().push_back(1.0-std::accumulate(pd.begin(), pd.end(), 0.0));
88 }
89 for (const auto& lgd : lgds) {
90 stateLGDs.push_back(lgd);
91 stateLGDs.back().push_back(0);
92 }
93
94 std::map<double, double> dist;
95 computeDiscreteDistribution(statePDs.begin(), statePDs.end(), stateLGDs.begin(), 1.0, 0.0, dist);
96 return dist;
97}
+ Here is the call graph for this function:

◆ operator<<()

std::ostream & operator<< ( std::ostream &  os,
const BucketedDistribution &  dist 
)

Definition at line 138 of file hullwhitebucketing.cpp.

138 {
139 os << "#\tLB\tUB\tPD\tAvg" << std::endl;
140 for (Size i = 0; i < dist.A.size(); ++i) {
141
142 os << i << "\t" << dist.lowerBound[i] << "\t" << dist.upperBound[i] << "\t" << dist.p[i] << "\t"
143 << dist.A[i] << std::endl;
144 }
145 return os;
146}

◆ expectedTrancheLoss() [1/3]

double expectedTrancheLoss ( const std::map< double, double > &  dist,
double  detachmentPoint 
)

Definition at line 148 of file hullwhitebucketing.cpp.

148 {
149 double expectedLoss = 0.0;
150 for (const auto& [L, pd] : dist) {
151 if (L <= detachmentPoint) {
152 expectedLoss += L * pd;
153 } else {
154 expectedLoss += detachmentPoint * pd;
155 }
156 }
157 return expectedLoss;
158}
+ Here is the caller graph for this function:

◆ expectedTrancheLoss() [2/3]

double expectedTrancheLoss ( QuantLib::Distribution  dist,
double  attachmentAmount,
double  detachmentAmount 
)

Definition at line 160 of file hullwhitebucketing.cpp.

160 {
161 QuantLib::Real expectedLoss = 0;
162 dist.normalize();
163 for (QuantLib::Size i = 0; i < dist.size(); i++) {
164 // Real x = dist.x(i) + dist.dx(i)/2; // in QL distribution.cpp
165 QuantLib::Real x = dist.average(i);
166 if (x < attachmentAmount)
167 continue;
168 if (x > detachmentAmount)
169 break;
170 expectedLoss += (x - attachmentAmount) * dist.dx(i) * dist.density(i);
171 }
172 expectedLoss += (detachmentAmount - attachmentAmount) * (1.0 - dist.cumulativeDensity(detachmentAmount));
173 return expectedLoss;
174}

◆ expectedTrancheLoss() [3/3]

double expectedTrancheLoss ( const vector< double > &  prob,
const vector< double > &  loss,
double  detachment 
)

Definition at line 177 of file hullwhitebucketing.cpp.

177 {
178 QuantLib::Real expectedLoss = 0;
179 for (size_t i = 0; i < prob.size(); ++i) {
180 expectedLoss += std::min(loss[i], detachment) * prob[i];
181 }
182 return expectedLoss;
183}