binomial.qbk 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. [section:binomial_dist Binomial Distribution]
  2. ``#include <boost/math/distributions/binomial.hpp>``
  3. namespace boost{ namespace math{
  4. template <class RealType = double,
  5. class ``__Policy`` = ``__policy_class`` >
  6. class binomial_distribution;
  7. typedef binomial_distribution<> binomial;
  8. template <class RealType, class ``__Policy``>
  9. class binomial_distribution
  10. {
  11. public:
  12. typedef RealType value_type;
  13. typedef Policy policy_type;
  14. static const ``['unspecified-type]`` clopper_pearson_exact_interval;
  15. static const ``['unspecified-type]`` jeffreys_prior_interval;
  16. // construct:
  17. binomial_distribution(RealType n, RealType p);
  18. // parameter access::
  19. RealType success_fraction() const;
  20. RealType trials() const;
  21. // Bounds on success fraction:
  22. static RealType find_lower_bound_on_p(
  23. RealType trials,
  24. RealType successes,
  25. RealType probability,
  26. ``['unspecified-type]`` method = clopper_pearson_exact_interval);
  27. static RealType find_upper_bound_on_p(
  28. RealType trials,
  29. RealType successes,
  30. RealType probability,
  31. ``['unspecified-type]`` method = clopper_pearson_exact_interval);
  32. // estimate min/max number of trials:
  33. static RealType find_minimum_number_of_trials(
  34. RealType k, // number of events
  35. RealType p, // success fraction
  36. RealType alpha); // risk level
  37. static RealType find_maximum_number_of_trials(
  38. RealType k, // number of events
  39. RealType p, // success fraction
  40. RealType alpha); // risk level
  41. };
  42. }} // namespaces
  43. The class type `binomial_distribution` represents a
  44. [@http://mathworld.wolfram.com/BinomialDistribution.html binomial distribution]:
  45. it is used when there are exactly two mutually
  46. exclusive outcomes of a trial. These outcomes are labelled
  47. "success" and "failure". The
  48. __binomial_distrib is used to obtain
  49. the probability of observing k successes in N trials, with the
  50. probability of success on a single trial denoted by p. The
  51. binomial distribution assumes that p is fixed for all trials.
  52. [note The random variable for the binomial distribution is the number of successes,
  53. (the number of trials is a fixed property of the distribution)
  54. whereas for the negative binomial,
  55. the random variable is the number of trials, for a fixed number of successes.]
  56. The PDF for the binomial distribution is given by:
  57. [equation binomial_ref2]
  58. The following two graphs illustrate how the PDF changes depending
  59. upon the distributions parameters, first we'll keep the success
  60. fraction /p/ fixed at 0.5, and vary the sample size:
  61. [graph binomial_pdf_1]
  62. Alternatively, we can keep the sample size fixed at N=20 and
  63. vary the success fraction /p/:
  64. [graph binomial_pdf_2]
  65. [discrete_quantile_warning Binomial]
  66. [h4 Member Functions]
  67. [h5 Construct]
  68. binomial_distribution(RealType n, RealType p);
  69. Constructor: /n/ is the total number of trials, /p/ is the
  70. probability of success of a single trial.
  71. Requires `0 <= p <= 1`, and `n >= 0`, otherwise calls __domain_error.
  72. [h5 Accessors]
  73. RealType success_fraction() const;
  74. Returns the parameter /p/ from which this distribution was constructed.
  75. RealType trials() const;
  76. Returns the parameter /n/ from which this distribution was constructed.
  77. [h5 Lower Bound on the Success Fraction]
  78. static RealType find_lower_bound_on_p(
  79. RealType trials,
  80. RealType successes,
  81. RealType alpha,
  82. ``['unspecified-type]`` method = clopper_pearson_exact_interval);
  83. Returns a lower bound on the success fraction:
  84. [variablelist
  85. [[trials][The total number of trials conducted.]]
  86. [[successes][The number of successes that occurred.]]
  87. [[alpha][The largest acceptable probability that the true value of
  88. the success fraction is [*less than] the value returned.]]
  89. [[method][An optional parameter that specifies the method to be used
  90. to compute the interval (See below).]]
  91. ]
  92. For example, if you observe /k/ successes from /n/ trials the
  93. best estimate for the success fraction is simply ['k/n], but if you
  94. want to be 95% sure that the true value is [*greater than] some value,
  95. ['p[sub min]], then:
  96. p``[sub min]`` = binomial_distribution<RealType>::find_lower_bound_on_p(n, k, 0.05);
  97. [link math_toolkit.stat_tut.weg.binom_eg.binom_conf See worked example.]
  98. There are currently two possible values available for the /method/
  99. optional parameter: /clopper_pearson_exact_interval/
  100. or /jeffreys_prior_interval/. These constants are both members of
  101. class template `binomial_distribution`, so usage is for example:
  102. p = binomial_distribution<RealType>::find_lower_bound_on_p(
  103. n, k, 0.05, binomial_distribution<RealType>::jeffreys_prior_interval);
  104. The default method if this parameter is not specified is the Clopper Pearson
  105. "exact" interval. This produces an interval that guarantees at least
  106. `100(1-alpha)%` coverage, but which is known to be overly conservative,
  107. sometimes producing intervals with much greater than the requested coverage.
  108. The alternative calculation method produces a non-informative
  109. Jeffreys Prior interval. It produces `100(1-alpha)%` coverage only
  110. ['in the average case], though is typically very close to the requested
  111. coverage level. It is one of the main methods of calculation recommended
  112. in the review by Brown, Cai and DasGupta.
  113. Please note that the "textbook" calculation method using
  114. a normal approximation (the Wald interval) is deliberately
  115. not provided: it is known to produce consistently poor results,
  116. even when the sample size is surprisingly large.
  117. Refer to Brown, Cai and DasGupta for a full explanation. Many other methods
  118. of calculation are available, and may be more appropriate for specific
  119. situations. Unfortunately there appears to be no consensus amongst
  120. statisticians as to which is "best": refer to the discussion at the end of
  121. Brown, Cai and DasGupta for examples.
  122. The two methods provided here were chosen principally because they
  123. can be used for both one and two sided intervals.
  124. See also:
  125. Lawrence D. Brown, T. Tony Cai and Anirban DasGupta (2001),
  126. Interval Estimation for a Binomial Proportion,
  127. Statistical Science, Vol. 16, No. 2, 101-133.
  128. T. Tony Cai (2005),
  129. One-sided confidence intervals in discrete distributions,
  130. Journal of Statistical Planning and Inference 131, 63-88.
  131. Agresti, A. and Coull, B. A. (1998). Approximate is better than
  132. "exact" for interval estimation of binomial proportions. Amer.
  133. Statist. 52 119-126.
  134. Clopper, C. J. and Pearson, E. S. (1934). The use of confidence
  135. or fiducial limits illustrated in the case of the binomial.
  136. Biometrika 26 404-413.
  137. [h5 Upper Bound on the Success Fraction]
  138. static RealType find_upper_bound_on_p(
  139. RealType trials,
  140. RealType successes,
  141. RealType alpha,
  142. ``['unspecified-type]`` method = clopper_pearson_exact_interval);
  143. Returns an upper bound on the success fraction:
  144. [variablelist
  145. [[trials][The total number of trials conducted.]]
  146. [[successes][The number of successes that occurred.]]
  147. [[alpha][The largest acceptable probability that the true value of
  148. the success fraction is [*greater than] the value returned.]]
  149. [[method][An optional parameter that specifies the method to be used
  150. to compute the interval. Refer to the documentation for
  151. `find_upper_bound_on_p` above for the meaning of the
  152. method options.]]
  153. ]
  154. For example, if you observe /k/ successes from /n/ trials the
  155. best estimate for the success fraction is simply ['k/n], but if you
  156. want to be 95% sure that the true value is [*less than] some value,
  157. ['p[sub max]], then:
  158. p``[sub max]`` = binomial_distribution<RealType>::find_upper_bound_on_p(n, k, 0.05);
  159. [link math_toolkit.stat_tut.weg.binom_eg.binom_conf See worked example.]
  160. [note
  161. In order to obtain a two sided bound on the success fraction, you
  162. call both `find_lower_bound_on_p` *and* `find_upper_bound_on_p`
  163. each with the same arguments.
  164. If the desired risk level
  165. that the true success fraction lies outside the bounds is [alpha],
  166. then you pass [alpha]/2 to these functions.
  167. So for example a two sided 95% confidence interval would be obtained
  168. by passing [alpha] = 0.025 to each of the functions.
  169. [link math_toolkit.stat_tut.weg.binom_eg.binom_conf See worked example.]
  170. ]
  171. [h5 Estimating the Number of Trials Required for a Certain Number of Successes]
  172. static RealType find_minimum_number_of_trials(
  173. RealType k, // number of events
  174. RealType p, // success fraction
  175. RealType alpha); // probability threshold
  176. This function estimates the minimum number of trials required to ensure that
  177. more than k events is observed with a level of risk /alpha/ that k or
  178. fewer events occur.
  179. [variablelist
  180. [[k][The number of success observed.]]
  181. [[p][The probability of success for each trial.]]
  182. [[alpha][The maximum acceptable probability that k events or fewer will be observed.]]
  183. ]
  184. For example:
  185. binomial_distribution<RealType>::find_number_of_trials(10, 0.5, 0.05);
  186. Returns the smallest number of trials we must conduct to be 95% sure
  187. of seeing 10 events that occur with frequency one half.
  188. [h5 Estimating the Maximum Number of Trials to Ensure no more than a Certain Number of Successes]
  189. static RealType find_maximum_number_of_trials(
  190. RealType k, // number of events
  191. RealType p, // success fraction
  192. RealType alpha); // probability threshold
  193. This function estimates the maximum number of trials we can conduct
  194. to ensure that k successes or fewer are observed, with a risk /alpha/
  195. that more than k occur.
  196. [variablelist
  197. [[k][The number of success observed.]]
  198. [[p][The probability of success for each trial.]]
  199. [[alpha][The maximum acceptable probability that more than k events will be observed.]]
  200. ]
  201. For example:
  202. binomial_distribution<RealType>::find_maximum_number_of_trials(0, 1e-6, 0.05);
  203. Returns the largest number of trials we can conduct and still be 95% certain
  204. of not observing any events that occur with one in a million frequency.
  205. This is typically used in failure analysis.
  206. [link math_toolkit.stat_tut.weg.binom_eg.binom_size_eg See Worked Example.]
  207. [h4 Non-member Accessors]
  208. All the [link math_toolkit.dist_ref.nmp usual non-member accessor functions]
  209. that are generic to all distributions are supported: __usual_accessors.
  210. The domain for the random variable /k/ is `0 <= k <= N`, otherwise a
  211. __domain_error is returned.
  212. It's worth taking a moment to define what these accessors actually mean in
  213. the context of this distribution:
  214. [table Meaning of the non-member accessors
  215. [[Function][Meaning]]
  216. [[__pdf]
  217. [The probability of obtaining [*exactly k successes] from n trials
  218. with success fraction p. For example:
  219. `pdf(binomial(n, p), k)`]]
  220. [[__cdf]
  221. [The probability of obtaining [*k successes or fewer] from n trials
  222. with success fraction p. For example:
  223. `cdf(binomial(n, p), k)`]]
  224. [[__ccdf]
  225. [The probability of obtaining [*more than k successes] from n trials
  226. with success fraction p. For example:
  227. `cdf(complement(binomial(n, p), k))`]]
  228. [[__quantile]
  229. [Given a binomial distribution with ['n] trials, success fraction ['p] and probability ['P],
  230. finds the largest number of successes ['k] whose CDF is less than ['P].
  231. It is strongly recommended that you read the tutorial
  232. [link math_toolkit.pol_tutorial.understand_dis_quant Understanding Quantiles of Discrete Distributions] before
  233. using the quantile function.]]
  234. [[__quantile_c]
  235. [Given a binomial distribution with ['n] trials, success fraction ['p] and probability ['Q],
  236. finds the smallest number of successes ['k] whose CDF is greater than ['1-Q].
  237. It is strongly recommended that you read the tutorial
  238. [link math_toolkit.pol_tutorial.understand_dis_quant Understanding Quantiles of Discrete Distributions] before
  239. using the quantile function.]]
  240. ]
  241. [h4 Examples]
  242. Various [link math_toolkit.stat_tut.weg.binom_eg worked examples]
  243. are available illustrating the use of the binomial distribution.
  244. [h4 Accuracy]
  245. This distribution is implemented using the
  246. incomplete beta functions __ibeta and __ibetac,
  247. please refer to these functions for information on accuracy.
  248. [h4 Implementation]
  249. In the following table /p/ is the probability that one trial will
  250. be successful (the success fraction), /n/ is the number of trials,
  251. /k/ is the number of successes, /p/ is the probability and /q = 1-p/.
  252. [table
  253. [[Function][Implementation Notes]]
  254. [[pdf][Implementation is in terms of __ibeta_derivative: if [sub n]C[sub k ] is the binomial
  255. coefficient of a and b, then we have:
  256. [equation binomial_ref1]
  257. Which can be evaluated as `ibeta_derivative(k+1, n-k+1, p) / (n+1)`
  258. The function __ibeta_derivative is used here, since it has already
  259. been optimised for the lowest possible error - indeed this is really
  260. just a thin wrapper around part of the internals of the incomplete
  261. beta function.
  262. There are also various special cases: refer to the code for details.
  263. ]]
  264. [[cdf][Using the relation:
  265. ``
  266. p = I[sub 1-p](n - k, k + 1)
  267. = 1 - I[sub p](k + 1, n - k)
  268. = __ibetac(k + 1, n - k, p)``
  269. There are also various special cases: refer to the code for details.
  270. ]]
  271. [[cdf complement][Using the relation: q = __ibeta(k + 1, n - k, p)
  272. There are also various special cases: refer to the code for details. ]]
  273. [[quantile][Since the cdf is non-linear in variate /k/ none of the inverse
  274. incomplete beta functions can be used here. Instead the quantile
  275. is found numerically using a derivative free method
  276. (__root_finding_TOMS748).]]
  277. [[quantile from the complement][Found numerically as above.]]
  278. [[mean][ `p * n` ]]
  279. [[variance][ `p * n * (1-p)` ]]
  280. [[mode][`floor(p * (n + 1))`]]
  281. [[skewness][`(1 - 2 * p) / sqrt(n * p * (1 - p))`]]
  282. [[kurtosis][`3 - (6 / n) + (1 / (n * p * (1 - p)))`]]
  283. [[kurtosis excess][`(1 - 6 * p * q) / (n * p * q)`]]
  284. [[parameter estimation][The member functions `find_upper_bound_on_p`
  285. `find_lower_bound_on_p` and `find_number_of_trials` are
  286. implemented in terms of the inverse incomplete beta functions
  287. __ibetac_inv, __ibeta_inv, and __ibetac_invb respectively]]
  288. ]
  289. [h4 References]
  290. * [@http://mathworld.wolfram.com/BinomialDistribution.html Weisstein, Eric W. "Binomial Distribution." From MathWorld--A Wolfram Web Resource].
  291. * [@http://en.wikipedia.org/wiki/Beta_distribution Wikipedia binomial distribution].
  292. * [@http://www.itl.nist.gov/div898/handbook/eda/section3/eda366i.htm NIST Explorary Data Analysis].
  293. [endsect] [/section:binomial_dist Binomial]
  294. [/ binomial.qbk
  295. Copyright 2006 John Maddock and Paul A. Bristow.
  296. Distributed under the Boost Software License, Version 1.0.
  297. (See accompanying file LICENSE_1_0.txt or copy at
  298. http://www.boost.org/LICENSE_1_0.txt).
  299. ]