QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
lossdistribution.hpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 Roland Lichters
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20/*! \file lossdistribution.hpp
21 \brief Loss distributions and probability of n defaults
22*/
23
24#ifndef quantlib_loss_distribution_hpp
25#define quantlib_loss_distribution_hpp
26
30
31namespace QuantLib {
32
33 //! Probability formulas and algorithms
34 class LossDist {
35 public:
36 LossDist() = default;
37 virtual ~LossDist() = default;
38
39 virtual Distribution operator()(const std::vector<Real>& volumes,
40 const std::vector<Real>& probabilities) const = 0;
41 virtual Size buckets() const = 0;
42 virtual Real maximum() const = 0;
43
44 /*! Binomial probability of n defaults using prob[0]
45 */
46 static Real binomialProbabilityOfNEvents(int n, std::vector<Real>& p);
47
48 /*! Binomial probability of at least n defaults using prob[0]
49 */
50 static Real binomialProbabilityOfAtLeastNEvents(int n, std::vector<Real>& p);
51 /*! Probability of exactly n default events
52 Xiaofong Ma, "Numerical Methods for the Valuation of Synthetic
53 Collateralized Debt Obligations", PhD Thesis,
54 Graduate Department of Computer Science, University of Toronto, 2007
55 http://www.cs.toronto.edu/pub/reports/na/ma-07-phd.pdf (formula 2.1)
56 */
57 static std::vector<Real> probabilityOfNEvents(std::vector<Real>& p);
58
59 static Real probabilityOfNEvents(int n, std::vector<Real>& p);
60
61 /*! Probability of at least n defaults
62 */
63 static Real probabilityOfAtLeastNEvents(int n, std::vector<Real>& p);
64 };
65
66 //! Probability of N events
68 public:
69 explicit ProbabilityOfNEvents (int n) : n_(n) {}
70 Real operator()(std::vector<Real> p) const;
71 private:
73 };
74
75 //! Probability of at least N events
77 public:
78 explicit ProbabilityOfAtLeastNEvents (int n) : n_(n) {}
79 Real operator()(std::vector<Real> p) const;
80 private:
82 };
83
84 //! Probability of at least N events
86 public:
88 Real operator()(std::vector<Real> p) const;
89
90 private:
91 int n_;
92 };
93
94 //! Binomial loss distribution
95 class LossDistBinomial : public LossDist {
96 public:
98 : nBuckets_(nBuckets), maximum_(maximum) {}
100 Distribution operator()(const std::vector<Real>& volumes,
101 const std::vector<Real>& probabilities) const override;
102 Size buckets() const override { return nBuckets_; }
103 Real maximum() const override { return maximum_; }
104 Real volume() const { return volume_; }
105 Size size () const { return n_; }
106 std::vector<Real> probability() const { return probability_; }
107 std::vector<Real> excessProbability() const { return excessProbability_; }
108 private:
111 mutable Real volume_;
112 mutable Size n_;
113 mutable std::vector<Real> probability_;
114 mutable std::vector<Real> excessProbability_;
115 };
116
117 //! Loss Distribution for Homogeneous Pool
118 /*! Loss Distribution for Homogeneous Pool
119
120 Loss distribution for equal volumes but varying probabilities of
121 default.
122
123 The method builds the exact loss distribution for a homogeneous pool
124 of underlyings iteratively by computing the convolution of the given
125 loss distribution with the "loss distribution" of an additional credit
126 following
127
128 Xiaofong Ma, "Numerical Methods for the Valuation of Synthetic
129 Collateralized Debt Obligations", PhD Thesis,
130 Graduate Department of Computer Science, University of Toronto, 2007
131 http://www.cs.toronto.edu/pub/reports/na/ma-07-phd.pdf (formula 2.1)
132
133 avoiding numerical instability of the algorithm by
134
135 John Hull and Alan White, "Valuation of a CDO and nth to default CDS
136 without Monte Carlo simulation", Journal of Derivatives 12, 2, 2004
137 */
139 public:
141 Distribution operator()(Real volume, const std::vector<Real>& probabilities) const;
142 Distribution operator()(const std::vector<Real>& volumes,
143 const std::vector<Real>& probabilities) const override;
144 Size buckets() const override { return nBuckets_; }
145 Real maximum() const override { return maximum_; }
146 Size size() const { return n_; }
147 Real volume() const { return volume_; }
148 std::vector<Real> probability() const { return probability_; }
149 std::vector<Real> excessProbability() const { return excessProbability_; }
150 private:
153 mutable Size n_ = 0;
154 mutable Real volume_ = 0.0;
155 mutable std::vector<Real> probability_;
156 mutable std::vector<Real> excessProbability_;
157 };
158
159 //! Loss distribution with Hull-White bucketing
160 /*! Loss distribution with Hull-White bucketing
161
162 Loss distribution for varying volumes and probabilities of default,
163 independence assumed.
164
165 The implementation of the loss distribution follows
166
167 John Hull and Alan White, "Valuation of a CDO and nth to default CDS
168 without Monte Carlo simulation", Journal of Derivatives 12, 2, 2004.
169 */
171 public:
173 Real epsilon = 1e-6)
174 : nBuckets_(nBuckets), maximum_(maximum), epsilon_(epsilon) {}
175 Distribution operator()(const std::vector<Real>& volumes,
176 const std::vector<Real>& probabilities) const override;
177 Size buckets() const override { return nBuckets_; }
178 Real maximum() const override { return maximum_; }
179
180 private:
181 int locateTargetBucket (Real loss, Size i0 = 0) const;
182
186 };
187
188 //! Loss distribution with Monte Carlo simulation
189 /*!
190 Loss distribution for varying volumes and probabilities of default
191 via Monte Carlo simulation of independent default events.
192 */
194 public:
195 LossDistMonteCarlo (Size nBuckets, Real maximum, Size simulations,
196 long seed = 42, Real epsilon = 1e-6)
197 : nBuckets_(nBuckets), maximum_(maximum),
198 simulations_(simulations), seed_(seed), epsilon_(epsilon) {}
199 Distribution operator()(const std::vector<Real>& volumes,
200 const std::vector<Real>& probabilities) const override;
201 Size buckets() const override { return nBuckets_; }
202 Real maximum() const override { return maximum_; }
203
204 private:
208 long seed_;
210 };
211
212}
213
214#endif
Binomial distribution.
Real operator()(std::vector< Real > p) const
Binomial loss distribution.
Size buckets() const override
std::vector< Real > excessProbability_
std::vector< Real > probability_
std::vector< Real > probability() const
std::vector< Real > excessProbability() const
Real maximum() const override
LossDistBinomial(Size nBuckets, Real maximum)
Distribution operator()(Size n, Real volume, Real probability) const
Loss distribution with Hull-White bucketing.
Distribution operator()(const std::vector< Real > &volumes, const std::vector< Real > &probabilities) const override
Size buckets() const override
int locateTargetBucket(Real loss, Size i0=0) const
Real maximum() const override
LossDistBucketing(Size nBuckets, Real maximum, Real epsilon=1e-6)
Loss Distribution for Homogeneous Pool.
std::vector< Real > excessProbability_
LossDistHomogeneous(Size nBuckets, Real maximum)
Distribution operator()(Real volume, const std::vector< Real > &probabilities) const
std::vector< Real > probability() const
std::vector< Real > excessProbability() const
Probability formulas and algorithms.
static Real binomialProbabilityOfAtLeastNEvents(int n, std::vector< Real > &p)
static std::vector< Real > probabilityOfNEvents(std::vector< Real > &p)
virtual ~LossDist()=default
virtual Real maximum() const =0
virtual Distribution operator()(const std::vector< Real > &volumes, const std::vector< Real > &probabilities) const =0
virtual Size buckets() const =0
static Real probabilityOfAtLeastNEvents(int n, std::vector< Real > &p)
static Real binomialProbabilityOfNEvents(int n, std::vector< Real > &p)
Loss distribution with Monte Carlo simulation.
Distribution operator()(const std::vector< Real > &volumes, const std::vector< Real > &probabilities) const override
Size buckets() const override
LossDistMonteCarlo(Size nBuckets, Real maximum, Size simulations, long seed=42, Real epsilon=1e-6)
Real maximum() const override
Probability of at least N events.
Real operator()(std::vector< Real > p) const
Probability of N events.
Real operator()(std::vector< Real > p) const
Discretized probability density and cumulative probability.
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
One-factor copula base class.