Chris@16
|
1 // boost\math\distributions\binomial.hpp
|
Chris@16
|
2
|
Chris@16
|
3 // Copyright John Maddock 2006.
|
Chris@16
|
4 // Copyright Paul A. Bristow 2007.
|
Chris@16
|
5
|
Chris@16
|
6 // Use, modification and distribution are subject to the
|
Chris@16
|
7 // Boost Software License, Version 1.0.
|
Chris@16
|
8 // (See accompanying file LICENSE_1_0.txt
|
Chris@16
|
9 // or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
10
|
Chris@16
|
11 // http://en.wikipedia.org/wiki/binomial_distribution
|
Chris@16
|
12
|
Chris@16
|
13 // Binomial distribution is the discrete probability distribution of
|
Chris@16
|
14 // the number (k) of successes, in a sequence of
|
Chris@16
|
15 // n independent (yes or no, success or failure) Bernoulli trials.
|
Chris@16
|
16
|
Chris@16
|
17 // It expresses the probability of a number of events occurring in a fixed time
|
Chris@16
|
18 // if these events occur with a known average rate (probability of success),
|
Chris@16
|
19 // and are independent of the time since the last event.
|
Chris@16
|
20
|
Chris@16
|
21 // The number of cars that pass through a certain point on a road during a given period of time.
|
Chris@16
|
22 // The number of spelling mistakes a secretary makes while typing a single page.
|
Chris@16
|
23 // The number of phone calls at a call center per minute.
|
Chris@16
|
24 // The number of times a web server is accessed per minute.
|
Chris@16
|
25 // The number of light bulbs that burn out in a certain amount of time.
|
Chris@16
|
26 // The number of roadkill found per unit length of road
|
Chris@16
|
27
|
Chris@16
|
28 // http://en.wikipedia.org/wiki/binomial_distribution
|
Chris@16
|
29
|
Chris@16
|
30 // Given a sample of N measured values k[i],
|
Chris@16
|
31 // we wish to estimate the value of the parameter x (mean)
|
Chris@16
|
32 // of the binomial population from which the sample was drawn.
|
Chris@16
|
33 // To calculate the maximum likelihood value = 1/N sum i = 1 to N of k[i]
|
Chris@16
|
34
|
Chris@16
|
35 // Also may want a function for EXACTLY k.
|
Chris@16
|
36
|
Chris@16
|
37 // And probability that there are EXACTLY k occurrences is
|
Chris@16
|
38 // exp(-x) * pow(x, k) / factorial(k)
|
Chris@16
|
39 // where x is expected occurrences (mean) during the given interval.
|
Chris@16
|
40 // For example, if events occur, on average, every 4 min,
|
Chris@16
|
41 // and we are interested in number of events occurring in 10 min,
|
Chris@16
|
42 // then x = 10/4 = 2.5
|
Chris@16
|
43
|
Chris@16
|
44 // http://www.itl.nist.gov/div898/handbook/eda/section3/eda366i.htm
|
Chris@16
|
45
|
Chris@16
|
46 // The binomial distribution is used when there are
|
Chris@16
|
47 // exactly two mutually exclusive outcomes of a trial.
|
Chris@16
|
48 // These outcomes are appropriately labeled "success" and "failure".
|
Chris@16
|
49 // The binomial distribution is used to obtain
|
Chris@16
|
50 // the probability of observing x successes in N trials,
|
Chris@16
|
51 // with the probability of success on a single trial denoted by p.
|
Chris@16
|
52 // The binomial distribution assumes that p is fixed for all trials.
|
Chris@16
|
53
|
Chris@16
|
54 // P(x, p, n) = n!/(x! * (n-x)!) * p^x * (1-p)^(n-x)
|
Chris@16
|
55
|
Chris@16
|
56 // http://mathworld.wolfram.com/BinomialCoefficient.html
|
Chris@16
|
57
|
Chris@16
|
58 // The binomial coefficient (n; k) is the number of ways of picking
|
Chris@16
|
59 // k unordered outcomes from n possibilities,
|
Chris@16
|
60 // also known as a combination or combinatorial number.
|
Chris@16
|
61 // The symbols _nC_k and (n; k) are used to denote a binomial coefficient,
|
Chris@16
|
62 // and are sometimes read as "n choose k."
|
Chris@16
|
63 // (n; k) therefore gives the number of k-subsets possible out of a set of n distinct items.
|
Chris@16
|
64
|
Chris@16
|
65 // For example:
|
Chris@16
|
66 // The 2-subsets of {1,2,3,4} are the six pairs {1,2}, {1,3}, {1,4}, {2,3}, {2,4}, and {3,4}, so (4; 2)==6.
|
Chris@16
|
67
|
Chris@16
|
68 // http://functions.wolfram.com/GammaBetaErf/Binomial/ for evaluation.
|
Chris@16
|
69
|
Chris@16
|
70 // But note that the binomial distribution
|
Chris@16
|
71 // (like others including the poisson, negative binomial & Bernoulli)
|
Chris@16
|
72 // is strictly defined as a discrete function: only integral values of k are envisaged.
|
Chris@16
|
73 // However because of the method of calculation using a continuous gamma function,
|
Chris@16
|
74 // it is convenient to treat it as if a continous function,
|
Chris@16
|
75 // and permit non-integral values of k.
|
Chris@16
|
76 // To enforce the strict mathematical model, users should use floor or ceil functions
|
Chris@16
|
77 // on k outside this function to ensure that k is integral.
|
Chris@16
|
78
|
Chris@16
|
79 #ifndef BOOST_MATH_SPECIAL_BINOMIAL_HPP
|
Chris@16
|
80 #define BOOST_MATH_SPECIAL_BINOMIAL_HPP
|
Chris@16
|
81
|
Chris@16
|
82 #include <boost/math/distributions/fwd.hpp>
|
Chris@16
|
83 #include <boost/math/special_functions/beta.hpp> // for incomplete beta.
|
Chris@16
|
84 #include <boost/math/distributions/complement.hpp> // complements
|
Chris@16
|
85 #include <boost/math/distributions/detail/common_error_handling.hpp> // error checks
|
Chris@16
|
86 #include <boost/math/distributions/detail/inv_discrete_quantile.hpp> // error checks
|
Chris@16
|
87 #include <boost/math/special_functions/fpclassify.hpp> // isnan.
|
Chris@16
|
88 #include <boost/math/tools/roots.hpp> // for root finding.
|
Chris@16
|
89
|
Chris@16
|
90 #include <utility>
|
Chris@16
|
91
|
Chris@16
|
92 namespace boost
|
Chris@16
|
93 {
|
Chris@16
|
94 namespace math
|
Chris@16
|
95 {
|
Chris@16
|
96
|
Chris@16
|
97 template <class RealType, class Policy>
|
Chris@16
|
98 class binomial_distribution;
|
Chris@16
|
99
|
Chris@16
|
100 namespace binomial_detail{
|
Chris@16
|
101 // common error checking routines for binomial distribution functions:
|
Chris@16
|
102 template <class RealType, class Policy>
|
Chris@16
|
103 inline bool check_N(const char* function, const RealType& N, RealType* result, const Policy& pol)
|
Chris@16
|
104 {
|
Chris@16
|
105 if((N < 0) || !(boost::math::isfinite)(N))
|
Chris@16
|
106 {
|
Chris@16
|
107 *result = policies::raise_domain_error<RealType>(
|
Chris@16
|
108 function,
|
Chris@16
|
109 "Number of Trials argument is %1%, but must be >= 0 !", N, pol);
|
Chris@16
|
110 return false;
|
Chris@16
|
111 }
|
Chris@16
|
112 return true;
|
Chris@16
|
113 }
|
Chris@16
|
114 template <class RealType, class Policy>
|
Chris@16
|
115 inline bool check_success_fraction(const char* function, const RealType& p, RealType* result, const Policy& pol)
|
Chris@16
|
116 {
|
Chris@16
|
117 if((p < 0) || (p > 1) || !(boost::math::isfinite)(p))
|
Chris@16
|
118 {
|
Chris@16
|
119 *result = policies::raise_domain_error<RealType>(
|
Chris@16
|
120 function,
|
Chris@16
|
121 "Success fraction argument is %1%, but must be >= 0 and <= 1 !", p, pol);
|
Chris@16
|
122 return false;
|
Chris@16
|
123 }
|
Chris@16
|
124 return true;
|
Chris@16
|
125 }
|
Chris@16
|
126 template <class RealType, class Policy>
|
Chris@16
|
127 inline bool check_dist(const char* function, const RealType& N, const RealType& p, RealType* result, const Policy& pol)
|
Chris@16
|
128 {
|
Chris@16
|
129 return check_success_fraction(
|
Chris@16
|
130 function, p, result, pol)
|
Chris@16
|
131 && check_N(
|
Chris@16
|
132 function, N, result, pol);
|
Chris@16
|
133 }
|
Chris@16
|
134 template <class RealType, class Policy>
|
Chris@16
|
135 inline bool check_dist_and_k(const char* function, const RealType& N, const RealType& p, RealType k, RealType* result, const Policy& pol)
|
Chris@16
|
136 {
|
Chris@16
|
137 if(check_dist(function, N, p, result, pol) == false)
|
Chris@16
|
138 return false;
|
Chris@16
|
139 if((k < 0) || !(boost::math::isfinite)(k))
|
Chris@16
|
140 {
|
Chris@16
|
141 *result = policies::raise_domain_error<RealType>(
|
Chris@16
|
142 function,
|
Chris@16
|
143 "Number of Successes argument is %1%, but must be >= 0 !", k, pol);
|
Chris@16
|
144 return false;
|
Chris@16
|
145 }
|
Chris@16
|
146 if(k > N)
|
Chris@16
|
147 {
|
Chris@16
|
148 *result = policies::raise_domain_error<RealType>(
|
Chris@16
|
149 function,
|
Chris@16
|
150 "Number of Successes argument is %1%, but must be <= Number of Trials !", k, pol);
|
Chris@16
|
151 return false;
|
Chris@16
|
152 }
|
Chris@16
|
153 return true;
|
Chris@16
|
154 }
|
Chris@16
|
155 template <class RealType, class Policy>
|
Chris@16
|
156 inline bool check_dist_and_prob(const char* function, const RealType& N, RealType p, RealType prob, RealType* result, const Policy& pol)
|
Chris@16
|
157 {
|
Chris@16
|
158 if(check_dist(function, N, p, result, pol) && detail::check_probability(function, prob, result, pol) == false)
|
Chris@16
|
159 return false;
|
Chris@16
|
160 return true;
|
Chris@16
|
161 }
|
Chris@16
|
162
|
Chris@16
|
163 template <class T, class Policy>
|
Chris@16
|
164 T inverse_binomial_cornish_fisher(T n, T sf, T p, T q, const Policy& pol)
|
Chris@16
|
165 {
|
Chris@16
|
166 BOOST_MATH_STD_USING
|
Chris@16
|
167 // mean:
|
Chris@16
|
168 T m = n * sf;
|
Chris@16
|
169 // standard deviation:
|
Chris@16
|
170 T sigma = sqrt(n * sf * (1 - sf));
|
Chris@16
|
171 // skewness
|
Chris@16
|
172 T sk = (1 - 2 * sf) / sigma;
|
Chris@16
|
173 // kurtosis:
|
Chris@16
|
174 // T k = (1 - 6 * sf * (1 - sf) ) / (n * sf * (1 - sf));
|
Chris@16
|
175 // Get the inverse of a std normal distribution:
|
Chris@16
|
176 T x = boost::math::erfc_inv(p > q ? 2 * q : 2 * p, pol) * constants::root_two<T>();
|
Chris@16
|
177 // Set the sign:
|
Chris@16
|
178 if(p < 0.5)
|
Chris@16
|
179 x = -x;
|
Chris@16
|
180 T x2 = x * x;
|
Chris@16
|
181 // w is correction term due to skewness
|
Chris@16
|
182 T w = x + sk * (x2 - 1) / 6;
|
Chris@16
|
183 /*
|
Chris@16
|
184 // Add on correction due to kurtosis.
|
Chris@16
|
185 // Disabled for now, seems to make things worse?
|
Chris@16
|
186 //
|
Chris@16
|
187 if(n >= 10)
|
Chris@16
|
188 w += k * x * (x2 - 3) / 24 + sk * sk * x * (2 * x2 - 5) / -36;
|
Chris@16
|
189 */
|
Chris@16
|
190 w = m + sigma * w;
|
Chris@16
|
191 if(w < tools::min_value<T>())
|
Chris@16
|
192 return sqrt(tools::min_value<T>());
|
Chris@16
|
193 if(w > n)
|
Chris@16
|
194 return n;
|
Chris@16
|
195 return w;
|
Chris@16
|
196 }
|
Chris@16
|
197
|
Chris@16
|
198 template <class RealType, class Policy>
|
Chris@16
|
199 RealType quantile_imp(const binomial_distribution<RealType, Policy>& dist, const RealType& p, const RealType& q, bool comp)
|
Chris@16
|
200 { // Quantile or Percent Point Binomial function.
|
Chris@16
|
201 // Return the number of expected successes k,
|
Chris@16
|
202 // for a given probability p.
|
Chris@16
|
203 //
|
Chris@16
|
204 // Error checks:
|
Chris@16
|
205 BOOST_MATH_STD_USING // ADL of std names
|
Chris@16
|
206 RealType result = 0;
|
Chris@16
|
207 RealType trials = dist.trials();
|
Chris@16
|
208 RealType success_fraction = dist.success_fraction();
|
Chris@16
|
209 if(false == binomial_detail::check_dist_and_prob(
|
Chris@16
|
210 "boost::math::quantile(binomial_distribution<%1%> const&, %1%)",
|
Chris@16
|
211 trials,
|
Chris@16
|
212 success_fraction,
|
Chris@16
|
213 p,
|
Chris@16
|
214 &result, Policy()))
|
Chris@16
|
215 {
|
Chris@16
|
216 return result;
|
Chris@16
|
217 }
|
Chris@16
|
218
|
Chris@16
|
219 // Special cases:
|
Chris@16
|
220 //
|
Chris@16
|
221 if(p == 0)
|
Chris@16
|
222 { // There may actually be no answer to this question,
|
Chris@16
|
223 // since the probability of zero successes may be non-zero,
|
Chris@16
|
224 // but zero is the best we can do:
|
Chris@16
|
225 return 0;
|
Chris@16
|
226 }
|
Chris@16
|
227 if(p == 1)
|
Chris@16
|
228 { // Probability of n or fewer successes is always one,
|
Chris@16
|
229 // so n is the most sensible answer here:
|
Chris@16
|
230 return trials;
|
Chris@16
|
231 }
|
Chris@16
|
232 if (p <= pow(1 - success_fraction, trials))
|
Chris@16
|
233 { // p <= pdf(dist, 0) == cdf(dist, 0)
|
Chris@16
|
234 return 0; // So the only reasonable result is zero.
|
Chris@16
|
235 } // And root finder would fail otherwise.
|
Chris@16
|
236 if(success_fraction == 1)
|
Chris@16
|
237 { // our formulae break down in this case:
|
Chris@16
|
238 return p > 0.5f ? trials : 0;
|
Chris@16
|
239 }
|
Chris@16
|
240
|
Chris@16
|
241 // Solve for quantile numerically:
|
Chris@16
|
242 //
|
Chris@16
|
243 RealType guess = binomial_detail::inverse_binomial_cornish_fisher(trials, success_fraction, p, q, Policy());
|
Chris@16
|
244 RealType factor = 8;
|
Chris@16
|
245 if(trials > 100)
|
Chris@16
|
246 factor = 1.01f; // guess is pretty accurate
|
Chris@16
|
247 else if((trials > 10) && (trials - 1 > guess) && (guess > 3))
|
Chris@16
|
248 factor = 1.15f; // less accurate but OK.
|
Chris@16
|
249 else if(trials < 10)
|
Chris@16
|
250 {
|
Chris@16
|
251 // pretty inaccurate guess in this area:
|
Chris@16
|
252 if(guess > trials / 64)
|
Chris@16
|
253 {
|
Chris@16
|
254 guess = trials / 4;
|
Chris@16
|
255 factor = 2;
|
Chris@16
|
256 }
|
Chris@16
|
257 else
|
Chris@16
|
258 guess = trials / 1024;
|
Chris@16
|
259 }
|
Chris@16
|
260 else
|
Chris@16
|
261 factor = 2; // trials largish, but in far tails.
|
Chris@16
|
262
|
Chris@16
|
263 typedef typename Policy::discrete_quantile_type discrete_quantile_type;
|
Chris@16
|
264 boost::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
|
Chris@16
|
265 return detail::inverse_discrete_quantile(
|
Chris@16
|
266 dist,
|
Chris@16
|
267 comp ? q : p,
|
Chris@16
|
268 comp,
|
Chris@16
|
269 guess,
|
Chris@16
|
270 factor,
|
Chris@16
|
271 RealType(1),
|
Chris@16
|
272 discrete_quantile_type(),
|
Chris@16
|
273 max_iter);
|
Chris@16
|
274 } // quantile
|
Chris@16
|
275
|
Chris@16
|
276 }
|
Chris@16
|
277
|
Chris@16
|
278 template <class RealType = double, class Policy = policies::policy<> >
|
Chris@16
|
279 class binomial_distribution
|
Chris@16
|
280 {
|
Chris@16
|
281 public:
|
Chris@16
|
282 typedef RealType value_type;
|
Chris@16
|
283 typedef Policy policy_type;
|
Chris@16
|
284
|
Chris@16
|
285 binomial_distribution(RealType n = 1, RealType p = 0.5) : m_n(n), m_p(p)
|
Chris@16
|
286 { // Default n = 1 is the Bernoulli distribution
|
Chris@16
|
287 // with equal probability of 'heads' or 'tails.
|
Chris@16
|
288 RealType r;
|
Chris@16
|
289 binomial_detail::check_dist(
|
Chris@16
|
290 "boost::math::binomial_distribution<%1%>::binomial_distribution",
|
Chris@16
|
291 m_n,
|
Chris@16
|
292 m_p,
|
Chris@16
|
293 &r, Policy());
|
Chris@16
|
294 } // binomial_distribution constructor.
|
Chris@16
|
295
|
Chris@16
|
296 RealType success_fraction() const
|
Chris@16
|
297 { // Probability.
|
Chris@16
|
298 return m_p;
|
Chris@16
|
299 }
|
Chris@16
|
300 RealType trials() const
|
Chris@16
|
301 { // Total number of trials.
|
Chris@16
|
302 return m_n;
|
Chris@16
|
303 }
|
Chris@16
|
304
|
Chris@16
|
305 enum interval_type{
|
Chris@16
|
306 clopper_pearson_exact_interval,
|
Chris@16
|
307 jeffreys_prior_interval
|
Chris@16
|
308 };
|
Chris@16
|
309
|
Chris@16
|
310 //
|
Chris@16
|
311 // Estimation of the success fraction parameter.
|
Chris@16
|
312 // The best estimate is actually simply successes/trials,
|
Chris@16
|
313 // these functions are used
|
Chris@16
|
314 // to obtain confidence intervals for the success fraction.
|
Chris@16
|
315 //
|
Chris@16
|
316 static RealType find_lower_bound_on_p(
|
Chris@16
|
317 RealType trials,
|
Chris@16
|
318 RealType successes,
|
Chris@16
|
319 RealType probability,
|
Chris@16
|
320 interval_type t = clopper_pearson_exact_interval)
|
Chris@16
|
321 {
|
Chris@16
|
322 static const char* function = "boost::math::binomial_distribution<%1%>::find_lower_bound_on_p";
|
Chris@16
|
323 // Error checks:
|
Chris@16
|
324 RealType result = 0;
|
Chris@16
|
325 if(false == binomial_detail::check_dist_and_k(
|
Chris@16
|
326 function, trials, RealType(0), successes, &result, Policy())
|
Chris@16
|
327 &&
|
Chris@16
|
328 binomial_detail::check_dist_and_prob(
|
Chris@16
|
329 function, trials, RealType(0), probability, &result, Policy()))
|
Chris@16
|
330 { return result; }
|
Chris@16
|
331
|
Chris@16
|
332 if(successes == 0)
|
Chris@16
|
333 return 0;
|
Chris@16
|
334
|
Chris@16
|
335 // NOTE!!! The Clopper Pearson formula uses "successes" not
|
Chris@16
|
336 // "successes+1" as usual to get the lower bound,
|
Chris@16
|
337 // see http://www.itl.nist.gov/div898/handbook/prc/section2/prc241.htm
|
Chris@16
|
338 return (t == clopper_pearson_exact_interval) ? ibeta_inv(successes, trials - successes + 1, probability, static_cast<RealType*>(0), Policy())
|
Chris@16
|
339 : ibeta_inv(successes + 0.5f, trials - successes + 0.5f, probability, static_cast<RealType*>(0), Policy());
|
Chris@16
|
340 }
|
Chris@16
|
341 static RealType find_upper_bound_on_p(
|
Chris@16
|
342 RealType trials,
|
Chris@16
|
343 RealType successes,
|
Chris@16
|
344 RealType probability,
|
Chris@16
|
345 interval_type t = clopper_pearson_exact_interval)
|
Chris@16
|
346 {
|
Chris@16
|
347 static const char* function = "boost::math::binomial_distribution<%1%>::find_upper_bound_on_p";
|
Chris@16
|
348 // Error checks:
|
Chris@16
|
349 RealType result = 0;
|
Chris@16
|
350 if(false == binomial_detail::check_dist_and_k(
|
Chris@16
|
351 function, trials, RealType(0), successes, &result, Policy())
|
Chris@16
|
352 &&
|
Chris@16
|
353 binomial_detail::check_dist_and_prob(
|
Chris@16
|
354 function, trials, RealType(0), probability, &result, Policy()))
|
Chris@16
|
355 { return result; }
|
Chris@16
|
356
|
Chris@16
|
357 if(trials == successes)
|
Chris@16
|
358 return 1;
|
Chris@16
|
359
|
Chris@16
|
360 return (t == clopper_pearson_exact_interval) ? ibetac_inv(successes + 1, trials - successes, probability, static_cast<RealType*>(0), Policy())
|
Chris@16
|
361 : ibetac_inv(successes + 0.5f, trials - successes + 0.5f, probability, static_cast<RealType*>(0), Policy());
|
Chris@16
|
362 }
|
Chris@16
|
363 // Estimate number of trials parameter:
|
Chris@16
|
364 //
|
Chris@16
|
365 // "How many trials do I need to be P% sure of seeing k events?"
|
Chris@16
|
366 // or
|
Chris@16
|
367 // "How many trials can I have to be P% sure of seeing fewer than k events?"
|
Chris@16
|
368 //
|
Chris@16
|
369 static RealType find_minimum_number_of_trials(
|
Chris@16
|
370 RealType k, // number of events
|
Chris@16
|
371 RealType p, // success fraction
|
Chris@16
|
372 RealType alpha) // risk level
|
Chris@16
|
373 {
|
Chris@16
|
374 static const char* function = "boost::math::binomial_distribution<%1%>::find_minimum_number_of_trials";
|
Chris@16
|
375 // Error checks:
|
Chris@16
|
376 RealType result = 0;
|
Chris@16
|
377 if(false == binomial_detail::check_dist_and_k(
|
Chris@16
|
378 function, k, p, k, &result, Policy())
|
Chris@16
|
379 &&
|
Chris@16
|
380 binomial_detail::check_dist_and_prob(
|
Chris@16
|
381 function, k, p, alpha, &result, Policy()))
|
Chris@16
|
382 { return result; }
|
Chris@16
|
383
|
Chris@16
|
384 result = ibetac_invb(k + 1, p, alpha, Policy()); // returns n - k
|
Chris@16
|
385 return result + k;
|
Chris@16
|
386 }
|
Chris@16
|
387
|
Chris@16
|
388 static RealType find_maximum_number_of_trials(
|
Chris@16
|
389 RealType k, // number of events
|
Chris@16
|
390 RealType p, // success fraction
|
Chris@16
|
391 RealType alpha) // risk level
|
Chris@16
|
392 {
|
Chris@16
|
393 static const char* function = "boost::math::binomial_distribution<%1%>::find_maximum_number_of_trials";
|
Chris@16
|
394 // Error checks:
|
Chris@16
|
395 RealType result = 0;
|
Chris@16
|
396 if(false == binomial_detail::check_dist_and_k(
|
Chris@16
|
397 function, k, p, k, &result, Policy())
|
Chris@16
|
398 &&
|
Chris@16
|
399 binomial_detail::check_dist_and_prob(
|
Chris@16
|
400 function, k, p, alpha, &result, Policy()))
|
Chris@16
|
401 { return result; }
|
Chris@16
|
402
|
Chris@16
|
403 result = ibeta_invb(k + 1, p, alpha, Policy()); // returns n - k
|
Chris@16
|
404 return result + k;
|
Chris@16
|
405 }
|
Chris@16
|
406
|
Chris@16
|
407 private:
|
Chris@16
|
408 RealType m_n; // Not sure if this shouldn't be an int?
|
Chris@16
|
409 RealType m_p; // success_fraction
|
Chris@16
|
410 }; // template <class RealType, class Policy> class binomial_distribution
|
Chris@16
|
411
|
Chris@16
|
412 typedef binomial_distribution<> binomial;
|
Chris@16
|
413 // typedef binomial_distribution<double> binomial;
|
Chris@16
|
414 // IS now included since no longer a name clash with function binomial.
|
Chris@16
|
415 //typedef binomial_distribution<double> binomial; // Reserved name of type double.
|
Chris@16
|
416
|
Chris@16
|
417 template <class RealType, class Policy>
|
Chris@16
|
418 const std::pair<RealType, RealType> range(const binomial_distribution<RealType, Policy>& dist)
|
Chris@16
|
419 { // Range of permissible values for random variable k.
|
Chris@16
|
420 using boost::math::tools::max_value;
|
Chris@16
|
421 return std::pair<RealType, RealType>(static_cast<RealType>(0), dist.trials());
|
Chris@16
|
422 }
|
Chris@16
|
423
|
Chris@16
|
424 template <class RealType, class Policy>
|
Chris@16
|
425 const std::pair<RealType, RealType> support(const binomial_distribution<RealType, Policy>& dist)
|
Chris@16
|
426 { // Range of supported values for random variable k.
|
Chris@16
|
427 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
|
Chris@16
|
428 return std::pair<RealType, RealType>(static_cast<RealType>(0), dist.trials());
|
Chris@16
|
429 }
|
Chris@16
|
430
|
Chris@16
|
431 template <class RealType, class Policy>
|
Chris@16
|
432 inline RealType mean(const binomial_distribution<RealType, Policy>& dist)
|
Chris@16
|
433 { // Mean of Binomial distribution = np.
|
Chris@16
|
434 return dist.trials() * dist.success_fraction();
|
Chris@16
|
435 } // mean
|
Chris@16
|
436
|
Chris@16
|
437 template <class RealType, class Policy>
|
Chris@16
|
438 inline RealType variance(const binomial_distribution<RealType, Policy>& dist)
|
Chris@16
|
439 { // Variance of Binomial distribution = np(1-p).
|
Chris@16
|
440 return dist.trials() * dist.success_fraction() * (1 - dist.success_fraction());
|
Chris@16
|
441 } // variance
|
Chris@16
|
442
|
Chris@16
|
443 template <class RealType, class Policy>
|
Chris@16
|
444 RealType pdf(const binomial_distribution<RealType, Policy>& dist, const RealType& k)
|
Chris@16
|
445 { // Probability Density/Mass Function.
|
Chris@16
|
446 BOOST_FPU_EXCEPTION_GUARD
|
Chris@16
|
447
|
Chris@16
|
448 BOOST_MATH_STD_USING // for ADL of std functions
|
Chris@16
|
449
|
Chris@16
|
450 RealType n = dist.trials();
|
Chris@16
|
451
|
Chris@16
|
452 // Error check:
|
Chris@16
|
453 RealType result = 0; // initialization silences some compiler warnings
|
Chris@16
|
454 if(false == binomial_detail::check_dist_and_k(
|
Chris@16
|
455 "boost::math::pdf(binomial_distribution<%1%> const&, %1%)",
|
Chris@16
|
456 n,
|
Chris@16
|
457 dist.success_fraction(),
|
Chris@16
|
458 k,
|
Chris@16
|
459 &result, Policy()))
|
Chris@16
|
460 {
|
Chris@16
|
461 return result;
|
Chris@16
|
462 }
|
Chris@16
|
463
|
Chris@16
|
464 // Special cases of success_fraction, regardless of k successes and regardless of n trials.
|
Chris@16
|
465 if (dist.success_fraction() == 0)
|
Chris@16
|
466 { // probability of zero successes is 1:
|
Chris@16
|
467 return static_cast<RealType>(k == 0 ? 1 : 0);
|
Chris@16
|
468 }
|
Chris@16
|
469 if (dist.success_fraction() == 1)
|
Chris@16
|
470 { // probability of n successes is 1:
|
Chris@16
|
471 return static_cast<RealType>(k == n ? 1 : 0);
|
Chris@16
|
472 }
|
Chris@16
|
473 // k argument may be integral, signed, or unsigned, or floating point.
|
Chris@16
|
474 // If necessary, it has already been promoted from an integral type.
|
Chris@16
|
475 if (n == 0)
|
Chris@16
|
476 {
|
Chris@16
|
477 return 1; // Probability = 1 = certainty.
|
Chris@16
|
478 }
|
Chris@16
|
479 if (k == 0)
|
Chris@16
|
480 { // binomial coeffic (n 0) = 1,
|
Chris@16
|
481 // n ^ 0 = 1
|
Chris@16
|
482 return pow(1 - dist.success_fraction(), n);
|
Chris@16
|
483 }
|
Chris@16
|
484 if (k == n)
|
Chris@16
|
485 { // binomial coeffic (n n) = 1,
|
Chris@16
|
486 // n ^ 0 = 1
|
Chris@16
|
487 return pow(dist.success_fraction(), k); // * pow((1 - dist.success_fraction()), (n - k)) = 1
|
Chris@16
|
488 }
|
Chris@16
|
489
|
Chris@16
|
490 // Probability of getting exactly k successes
|
Chris@16
|
491 // if C(n, k) is the binomial coefficient then:
|
Chris@16
|
492 //
|
Chris@16
|
493 // f(k; n,p) = C(n, k) * p^k * (1-p)^(n-k)
|
Chris@16
|
494 // = (n!/(k!(n-k)!)) * p^k * (1-p)^(n-k)
|
Chris@16
|
495 // = (tgamma(n+1) / (tgamma(k+1)*tgamma(n-k+1))) * p^k * (1-p)^(n-k)
|
Chris@16
|
496 // = p^k (1-p)^(n-k) / (beta(k+1, n-k+1) * (n+1))
|
Chris@16
|
497 // = ibeta_derivative(k+1, n-k+1, p) / (n+1)
|
Chris@16
|
498 //
|
Chris@16
|
499 using boost::math::ibeta_derivative; // a, b, x
|
Chris@16
|
500 return ibeta_derivative(k+1, n-k+1, dist.success_fraction(), Policy()) / (n+1);
|
Chris@16
|
501
|
Chris@16
|
502 } // pdf
|
Chris@16
|
503
|
Chris@16
|
504 template <class RealType, class Policy>
|
Chris@16
|
505 inline RealType cdf(const binomial_distribution<RealType, Policy>& dist, const RealType& k)
|
Chris@16
|
506 { // Cumulative Distribution Function Binomial.
|
Chris@16
|
507 // The random variate k is the number of successes in n trials.
|
Chris@16
|
508 // k argument may be integral, signed, or unsigned, or floating point.
|
Chris@16
|
509 // If necessary, it has already been promoted from an integral type.
|
Chris@16
|
510
|
Chris@16
|
511 // Returns the sum of the terms 0 through k of the Binomial Probability Density/Mass:
|
Chris@16
|
512 //
|
Chris@16
|
513 // i=k
|
Chris@16
|
514 // -- ( n ) i n-i
|
Chris@16
|
515 // > | | p (1-p)
|
Chris@16
|
516 // -- ( i )
|
Chris@16
|
517 // i=0
|
Chris@16
|
518
|
Chris@16
|
519 // The terms are not summed directly instead
|
Chris@16
|
520 // the incomplete beta integral is employed,
|
Chris@16
|
521 // according to the formula:
|
Chris@16
|
522 // P = I[1-p]( n-k, k+1).
|
Chris@16
|
523 // = 1 - I[p](k + 1, n - k)
|
Chris@16
|
524
|
Chris@16
|
525 BOOST_MATH_STD_USING // for ADL of std functions
|
Chris@16
|
526
|
Chris@16
|
527 RealType n = dist.trials();
|
Chris@16
|
528 RealType p = dist.success_fraction();
|
Chris@16
|
529
|
Chris@16
|
530 // Error check:
|
Chris@16
|
531 RealType result = 0;
|
Chris@16
|
532 if(false == binomial_detail::check_dist_and_k(
|
Chris@16
|
533 "boost::math::cdf(binomial_distribution<%1%> const&, %1%)",
|
Chris@16
|
534 n,
|
Chris@16
|
535 p,
|
Chris@16
|
536 k,
|
Chris@16
|
537 &result, Policy()))
|
Chris@16
|
538 {
|
Chris@16
|
539 return result;
|
Chris@16
|
540 }
|
Chris@16
|
541 if (k == n)
|
Chris@16
|
542 {
|
Chris@16
|
543 return 1;
|
Chris@16
|
544 }
|
Chris@16
|
545
|
Chris@16
|
546 // Special cases, regardless of k.
|
Chris@16
|
547 if (p == 0)
|
Chris@16
|
548 { // This need explanation:
|
Chris@16
|
549 // the pdf is zero for all cases except when k == 0.
|
Chris@16
|
550 // For zero p the probability of zero successes is one.
|
Chris@16
|
551 // Therefore the cdf is always 1:
|
Chris@16
|
552 // the probability of k or *fewer* successes is always 1
|
Chris@16
|
553 // if there are never any successes!
|
Chris@16
|
554 return 1;
|
Chris@16
|
555 }
|
Chris@16
|
556 if (p == 1)
|
Chris@16
|
557 { // This is correct but needs explanation:
|
Chris@16
|
558 // when k = 1
|
Chris@16
|
559 // all the cdf and pdf values are zero *except* when k == n,
|
Chris@16
|
560 // and that case has been handled above already.
|
Chris@16
|
561 return 0;
|
Chris@16
|
562 }
|
Chris@16
|
563 //
|
Chris@16
|
564 // P = I[1-p](n - k, k + 1)
|
Chris@16
|
565 // = 1 - I[p](k + 1, n - k)
|
Chris@16
|
566 // Use of ibetac here prevents cancellation errors in calculating
|
Chris@16
|
567 // 1-p if p is very small, perhaps smaller than machine epsilon.
|
Chris@16
|
568 //
|
Chris@16
|
569 // Note that we do not use a finite sum here, since the incomplete
|
Chris@16
|
570 // beta uses a finite sum internally for integer arguments, so
|
Chris@16
|
571 // we'll just let it take care of the necessary logic.
|
Chris@16
|
572 //
|
Chris@16
|
573 return ibetac(k + 1, n - k, p, Policy());
|
Chris@16
|
574 } // binomial cdf
|
Chris@16
|
575
|
Chris@16
|
576 template <class RealType, class Policy>
|
Chris@16
|
577 inline RealType cdf(const complemented2_type<binomial_distribution<RealType, Policy>, RealType>& c)
|
Chris@16
|
578 { // Complemented Cumulative Distribution Function Binomial.
|
Chris@16
|
579 // The random variate k is the number of successes in n trials.
|
Chris@16
|
580 // k argument may be integral, signed, or unsigned, or floating point.
|
Chris@16
|
581 // If necessary, it has already been promoted from an integral type.
|
Chris@16
|
582
|
Chris@16
|
583 // Returns the sum of the terms k+1 through n of the Binomial Probability Density/Mass:
|
Chris@16
|
584 //
|
Chris@16
|
585 // i=n
|
Chris@16
|
586 // -- ( n ) i n-i
|
Chris@16
|
587 // > | | p (1-p)
|
Chris@16
|
588 // -- ( i )
|
Chris@16
|
589 // i=k+1
|
Chris@16
|
590
|
Chris@16
|
591 // The terms are not summed directly instead
|
Chris@16
|
592 // the incomplete beta integral is employed,
|
Chris@16
|
593 // according to the formula:
|
Chris@16
|
594 // Q = 1 -I[1-p]( n-k, k+1).
|
Chris@16
|
595 // = I[p](k + 1, n - k)
|
Chris@16
|
596
|
Chris@16
|
597 BOOST_MATH_STD_USING // for ADL of std functions
|
Chris@16
|
598
|
Chris@16
|
599 RealType const& k = c.param;
|
Chris@16
|
600 binomial_distribution<RealType, Policy> const& dist = c.dist;
|
Chris@16
|
601 RealType n = dist.trials();
|
Chris@16
|
602 RealType p = dist.success_fraction();
|
Chris@16
|
603
|
Chris@16
|
604 // Error checks:
|
Chris@16
|
605 RealType result = 0;
|
Chris@16
|
606 if(false == binomial_detail::check_dist_and_k(
|
Chris@16
|
607 "boost::math::cdf(binomial_distribution<%1%> const&, %1%)",
|
Chris@16
|
608 n,
|
Chris@16
|
609 p,
|
Chris@16
|
610 k,
|
Chris@16
|
611 &result, Policy()))
|
Chris@16
|
612 {
|
Chris@16
|
613 return result;
|
Chris@16
|
614 }
|
Chris@16
|
615
|
Chris@16
|
616 if (k == n)
|
Chris@16
|
617 { // Probability of greater than n successes is necessarily zero:
|
Chris@16
|
618 return 0;
|
Chris@16
|
619 }
|
Chris@16
|
620
|
Chris@16
|
621 // Special cases, regardless of k.
|
Chris@16
|
622 if (p == 0)
|
Chris@16
|
623 {
|
Chris@16
|
624 // This need explanation: the pdf is zero for all
|
Chris@16
|
625 // cases except when k == 0. For zero p the probability
|
Chris@16
|
626 // of zero successes is one. Therefore the cdf is always
|
Chris@16
|
627 // 1: the probability of *more than* k successes is always 0
|
Chris@16
|
628 // if there are never any successes!
|
Chris@16
|
629 return 0;
|
Chris@16
|
630 }
|
Chris@16
|
631 if (p == 1)
|
Chris@16
|
632 {
|
Chris@16
|
633 // This needs explanation, when p = 1
|
Chris@16
|
634 // we always have n successes, so the probability
|
Chris@16
|
635 // of more than k successes is 1 as long as k < n.
|
Chris@16
|
636 // The k == n case has already been handled above.
|
Chris@16
|
637 return 1;
|
Chris@16
|
638 }
|
Chris@16
|
639 //
|
Chris@16
|
640 // Calculate cdf binomial using the incomplete beta function.
|
Chris@16
|
641 // Q = 1 -I[1-p](n - k, k + 1)
|
Chris@16
|
642 // = I[p](k + 1, n - k)
|
Chris@16
|
643 // Use of ibeta here prevents cancellation errors in calculating
|
Chris@16
|
644 // 1-p if p is very small, perhaps smaller than machine epsilon.
|
Chris@16
|
645 //
|
Chris@16
|
646 // Note that we do not use a finite sum here, since the incomplete
|
Chris@16
|
647 // beta uses a finite sum internally for integer arguments, so
|
Chris@16
|
648 // we'll just let it take care of the necessary logic.
|
Chris@16
|
649 //
|
Chris@16
|
650 return ibeta(k + 1, n - k, p, Policy());
|
Chris@16
|
651 } // binomial cdf
|
Chris@16
|
652
|
Chris@16
|
653 template <class RealType, class Policy>
|
Chris@16
|
654 inline RealType quantile(const binomial_distribution<RealType, Policy>& dist, const RealType& p)
|
Chris@16
|
655 {
|
Chris@16
|
656 return binomial_detail::quantile_imp(dist, p, RealType(1-p), false);
|
Chris@16
|
657 } // quantile
|
Chris@16
|
658
|
Chris@16
|
659 template <class RealType, class Policy>
|
Chris@16
|
660 RealType quantile(const complemented2_type<binomial_distribution<RealType, Policy>, RealType>& c)
|
Chris@16
|
661 {
|
Chris@16
|
662 return binomial_detail::quantile_imp(c.dist, RealType(1-c.param), c.param, true);
|
Chris@16
|
663 } // quantile
|
Chris@16
|
664
|
Chris@16
|
665 template <class RealType, class Policy>
|
Chris@16
|
666 inline RealType mode(const binomial_distribution<RealType, Policy>& dist)
|
Chris@16
|
667 {
|
Chris@16
|
668 BOOST_MATH_STD_USING // ADL of std functions.
|
Chris@16
|
669 RealType p = dist.success_fraction();
|
Chris@16
|
670 RealType n = dist.trials();
|
Chris@16
|
671 return floor(p * (n + 1));
|
Chris@16
|
672 }
|
Chris@16
|
673
|
Chris@16
|
674 template <class RealType, class Policy>
|
Chris@16
|
675 inline RealType median(const binomial_distribution<RealType, Policy>& dist)
|
Chris@16
|
676 { // Bounds for the median of the negative binomial distribution
|
Chris@16
|
677 // VAN DE VEN R. ; WEBER N. C. ;
|
Chris@16
|
678 // Univ. Sydney, school mathematics statistics, Sydney N.S.W. 2006, AUSTRALIE
|
Chris@16
|
679 // Metrika (Metrika) ISSN 0026-1335 CODEN MTRKA8
|
Chris@16
|
680 // 1993, vol. 40, no3-4, pp. 185-189 (4 ref.)
|
Chris@16
|
681
|
Chris@16
|
682 // Bounds for median and 50 percetage point of binomial and negative binomial distribution
|
Chris@16
|
683 // Metrika, ISSN 0026-1335 (Print) 1435-926X (Online)
|
Chris@16
|
684 // Volume 41, Number 1 / December, 1994, DOI 10.1007/BF01895303
|
Chris@16
|
685 BOOST_MATH_STD_USING // ADL of std functions.
|
Chris@16
|
686 RealType p = dist.success_fraction();
|
Chris@16
|
687 RealType n = dist.trials();
|
Chris@16
|
688 // Wikipedia says one of floor(np) -1, floor (np), floor(np) +1
|
Chris@16
|
689 return floor(p * n); // Chose the middle value.
|
Chris@16
|
690 }
|
Chris@16
|
691
|
Chris@16
|
692 template <class RealType, class Policy>
|
Chris@16
|
693 inline RealType skewness(const binomial_distribution<RealType, Policy>& dist)
|
Chris@16
|
694 {
|
Chris@16
|
695 BOOST_MATH_STD_USING // ADL of std functions.
|
Chris@16
|
696 RealType p = dist.success_fraction();
|
Chris@16
|
697 RealType n = dist.trials();
|
Chris@16
|
698 return (1 - 2 * p) / sqrt(n * p * (1 - p));
|
Chris@16
|
699 }
|
Chris@16
|
700
|
Chris@16
|
701 template <class RealType, class Policy>
|
Chris@16
|
702 inline RealType kurtosis(const binomial_distribution<RealType, Policy>& dist)
|
Chris@16
|
703 {
|
Chris@16
|
704 RealType p = dist.success_fraction();
|
Chris@16
|
705 RealType n = dist.trials();
|
Chris@16
|
706 return 3 - 6 / n + 1 / (n * p * (1 - p));
|
Chris@16
|
707 }
|
Chris@16
|
708
|
Chris@16
|
709 template <class RealType, class Policy>
|
Chris@16
|
710 inline RealType kurtosis_excess(const binomial_distribution<RealType, Policy>& dist)
|
Chris@16
|
711 {
|
Chris@16
|
712 RealType p = dist.success_fraction();
|
Chris@16
|
713 RealType q = 1 - p;
|
Chris@16
|
714 RealType n = dist.trials();
|
Chris@16
|
715 return (1 - 6 * p * q) / (n * p * q);
|
Chris@16
|
716 }
|
Chris@16
|
717
|
Chris@16
|
718 } // namespace math
|
Chris@16
|
719 } // namespace boost
|
Chris@16
|
720
|
Chris@16
|
721 // This include must be at the end, *after* the accessors
|
Chris@16
|
722 // for this distribution have been defined, in order to
|
Chris@16
|
723 // keep compilers that support two-phase lookup happy.
|
Chris@16
|
724 #include <boost/math/distributions/detail/derived_accessors.hpp>
|
Chris@16
|
725
|
Chris@16
|
726 #endif // BOOST_MATH_SPECIAL_BINOMIAL_HPP
|
Chris@16
|
727
|
Chris@16
|
728
|