QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
binomialdistribution.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2003 Ferdinando Ametrano
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
24#ifndef quantlib_binomial_distribution_h
25#define quantlib_binomial_distribution_h
26
27#include <ql/math/factorial.hpp>
28#include <ql/math/beta.hpp>
29
30namespace QuantLib {
31
33
34 QL_REQUIRE(n>=k, "n<k not allowed");
35
37
38 }
39
41
42 return std::floor(0.5+std::exp(binomialCoefficientLn(n, k)));
43
44 }
45
47
52 public:
56 QL_DEPRECATED
58
62 QL_DEPRECATED
64
66 // function
67 Real operator()(BigNatural k) const;
68 private:
71 };
72
74
80 public:
84 QL_DEPRECATED
86
90 QL_DEPRECATED
92
94 // function
96 if (k >= n_)
97 return 1.0;
98 else
99 return 1.0 - incompleteBetaFunction(k+1, n_-k, p_);
100 }
101 private:
104 };
105
106
108 BigNatural n)
109 : n_(n) {
110
111 if (p==0.0) {
113 logOneMinusP_ = 0.0;
114 } else if (p==1.0) {
115 logP_ = 0.0;
117 } else {
118 QL_REQUIRE(p>0, "negative p not allowed");
119 QL_REQUIRE(p<1.0, "p>1.0 not allowed");
120
121 logP_ = std::log(p);
122 logOneMinusP_ = std::log(1.0-p);
123 }
124 }
125
126
127 inline
128 CumulativeBinomialDistribution::CumulativeBinomialDistribution(
129 Real p, BigNatural n)
130 : n_(n), p_(p) {
131
132 QL_REQUIRE(p>=0, "negative p not allowed");
133 QL_REQUIRE(p<=1.0, "p>1.0 not allowed");
134
135 }
136
137 inline Real BinomialDistribution::operator()(BigNatural k) const {
138
139 if (k > n_) return 0.0;
140
141 // p==1.0
142 if (logP_==0.0)
143 return (k==n_ ? 1.0 : 0.0);
144 // p==0.0
145 else if (logOneMinusP_==0.0)
146 return (k==0 ? 1.0 : 0.0);
147 else
148 return std::exp(binomialCoefficientLn(n_, k) +
149 k * logP_ + (n_-k) * logOneMinusP_);
150 }
151
152
153
160 inline Real PeizerPrattMethod2Inversion(Real z, BigNatural n) {
161
162 QL_REQUIRE(n%2==1,
163 "n must be an odd number: " << n << " not allowed");
164
165 Real result = (z/(n+1.0/3.0+0.1/(n+1.0)));
166 result *= result;
167 result = std::exp(-result*(n+1.0/6.0));
168 result = 0.5 + (z>0 ? 1 : -1) * std::sqrt((0.25 * (1.0-result)));
169 return result;
170 }
171
172}
173
174
175#endif
Binomial probability distribution function.
BinomialDistribution(Real p, BigNatural n)
Real operator()(BigNatural k) const
QL_DEPRECATED typedef Real argument_type
QL_DEPRECATED typedef Real result_type
Cumulative binomial distribution function.
static Real ln(Natural n)
Definition: factorial.cpp:58
#define QL_MAX_REAL
Definition: qldefines.hpp:176
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
Real incompleteBetaFunction(Real a, Real b, Real x, Real accuracy, Integer maxIteration)
Incomplete Beta function.
Definition: beta.cpp:67
Real binomialCoefficient(BigNatural n, BigNatural k)
Real binomialCoefficientLn(BigNatural n, BigNatural k)
unsigned QL_BIG_INTEGER BigNatural
large positive integer
Definition: types.hpp:46