38 return 1.0 - binomial(
n-1);
52 vector<Real> probability(
n+1, 0.0);
55 for (
Size j = 0; j <
n; j++) {
57 probability[0] = prev[0] * (1.0 - p[j]);
58 for (
Size i = 1; i <= j; i++)
59 probability[i] = prev[i-1] * p[j] + prev[i] * (1.0 - p[j]);
60 probability[j+1] = prev[j] * p[j];
116 for (
int j = 0; j < k; j++)
117 sum -= probability[j];
147 Real probability)
const {
154 for (
Size i = 0; i <=
n; i++) {
166 for (
int k =
n_-1; k >= 0; k--)
176 const vector<Real>& probabilities)
const {
178 return operator()(nominals.size(), nominals[0], probabilities[0]);
183 const vector<Real>& p)
const {
191 for (
Size k = 0; k <
n_; k++) {
194 for (
Size i = 1; i <= k; i++)
195 probability_[i] = prev[i-1] * p[k] + prev[i] * (1.0 - p[k]);
202 for (
int k =
n_ - 1; k >= 0; k--)
206 for (
Size i = 0; i <=
n_; i++) {
221 const vector<Real>& probabilities)
const {
223 return operator()(nominals[0], probabilities);
228 const vector<Real>& probabilities)
const {
230 QL_REQUIRE (nominals.size() == probabilities.size(),
"sizes differ: "
231 << nominals.size() <<
" vs " << probabilities.size());
241 a[k] = dx * k + dx/2;
243 for (
Size i = 0; i < nominals.size(); i++) {
244 Real L = nominals[i];
245 Real P = probabilities[i];
246 for (
int k = a.size()-1; k >= 0; k--) {
249 QL_REQUIRE (u >= 0,
"u=" << u <<
" at i=" << i <<
" k=" << k);
250 QL_REQUIRE (u >= k,
"u=" << u <<
"<k=" << k <<
" at i=" << i);
264 Real f = 1.0 / (1.0 + (p[u]/p[k]) / P);
265 a[u] = (1.0 -
f) * a[u] +
f * (a[k] + L);
278 "a out of range at k=" << k <<
", contract " << i);
294 QL_REQUIRE (loss >= 0,
"loss " << loss <<
" must be >= 0");
297 if (dx * i > loss +
epsilon_)
return i - 1;
303 const vector<Real>& probabilities)
const {
311 for (
Size j = 0; j < nominals.size(); j++) {
313 if (
r <= probabilities[j])
Binomial probability distribution function.
Real operator()(std::vector< Real > p) const
Cumulative binomial distribution function.
void addDensity(int bucket, Real value)
void addAverage(int bucket, Real value)
std::vector< Real > excessProbability_
std::vector< Real > probability_
std::vector< Real > probability() const
Distribution operator()(Size n, Real volume, Real probability) const
Distribution operator()(const std::vector< Real > &volumes, const std::vector< Real > &probabilities) const override
int locateTargetBucket(Real loss, Size i0=0) const
std::vector< Real > excessProbability_
std::vector< Real > probability_
Distribution operator()(Real volume, const std::vector< Real > &probabilities) const
static Real binomialProbabilityOfAtLeastNEvents(int n, std::vector< Real > &p)
static std::vector< Real > probabilityOfNEvents(std::vector< Real > &p)
static Real probabilityOfAtLeastNEvents(int n, std::vector< Real > &p)
static Real binomialProbabilityOfNEvents(int n, std::vector< Real > &p)
Distribution operator()(const std::vector< Real > &volumes, const std::vector< Real > &probabilities) const override
Real operator()(std::vector< Real > p) const
Real operator()(std::vector< Real > p) const
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
std::size_t Size
size of a container
Loss distributions and probability of n defaults.
ext::shared_ptr< YieldTermStructure > r