negative_binomial.qbk 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. [section:negative_binomial_dist Negative Binomial Distribution]
  2. ``#include <boost/math/distributions/negative_binomial.hpp>``
  3. namespace boost{ namespace math{
  4. template <class RealType = double,
  5. class ``__Policy`` = ``__policy_class`` >
  6. class negative_binomial_distribution;
  7. typedef negative_binomial_distribution<> negative_binomial;
  8. template <class RealType, class ``__Policy``>
  9. class negative_binomial_distribution
  10. {
  11. public:
  12. typedef RealType value_type;
  13. typedef Policy policy_type;
  14. // Constructor from successes and success_fraction:
  15. negative_binomial_distribution(RealType r, RealType p);
  16. // Parameter accessors:
  17. RealType success_fraction() const;
  18. RealType successes() const;
  19. // Bounds on success fraction:
  20. static RealType find_lower_bound_on_p(
  21. RealType trials,
  22. RealType successes,
  23. RealType probability); // alpha
  24. static RealType find_upper_bound_on_p(
  25. RealType trials,
  26. RealType successes,
  27. RealType probability); // alpha
  28. // Estimate min/max number of trials:
  29. static RealType find_minimum_number_of_trials(
  30. RealType k, // Number of failures.
  31. RealType p, // Success fraction.
  32. RealType probability); // Probability threshold alpha.
  33. static RealType find_maximum_number_of_trials(
  34. RealType k, // Number of failures.
  35. RealType p, // Success fraction.
  36. RealType probability); // Probability threshold alpha.
  37. };
  38. }} // namespaces
  39. The class type `negative_binomial_distribution` represents a
  40. [@http://en.wikipedia.org/wiki/Negative_binomial_distribution negative_binomial distribution]:
  41. it is used when there are exactly two mutually exclusive outcomes of a
  42. [@http://en.wikipedia.org/wiki/Bernoulli_trial Bernoulli trial]:
  43. these outcomes are labelled "success" and "failure".
  44. For k + r Bernoulli trials each with success fraction p, the
  45. negative_binomial distribution gives the probability of observing
  46. k failures and r successes with success on the last trial.
  47. The negative_binomial distribution
  48. assumes that success_fraction p is fixed for all (k + r) trials.
  49. [note The random variable for the negative binomial distribution is the number of trials,
  50. (the number of successes is a fixed property of the distribution)
  51. whereas for the binomial,
  52. the random variable is the number of successes, for a fixed number of trials.]
  53. It has the PDF:
  54. [equation neg_binomial_ref]
  55. The following graph illustrate how the PDF varies as the success fraction
  56. /p/ changes:
  57. [graph negative_binomial_pdf_1]
  58. Alternatively, this graph shows how the shape of the PDF varies as
  59. the number of successes changes:
  60. [graph negative_binomial_pdf_2]
  61. [h4 Related Distributions]
  62. The name negative binomial distribution is reserved by some to the
  63. case where the successes parameter r is an integer.
  64. This integer version is also called the
  65. [@http://mathworld.wolfram.com/PascalDistribution.html Pascal distribution].
  66. This implementation uses real numbers for the computation throughout
  67. (because it uses the *real-valued* incomplete beta function family of functions).
  68. This real-valued version is also called the Polya Distribution.
  69. The Poisson distribution is a generalization of the Pascal distribution,
  70. where the success parameter r is an integer: to obtain the Pascal
  71. distribution you must ensure that an integer value is provided for r,
  72. and take integer values (floor or ceiling) from functions that return
  73. a number of successes.
  74. For large values of r (successes), the negative binomial distribution
  75. converges to the Poisson distribution.
  76. The geometric distribution is a special case
  77. where the successes parameter r = 1,
  78. so only a first and only success is required.
  79. geometric(p) = negative_binomial(1, p).
  80. The Poisson distribution is a special case for large successes
  81. poisson([lambda]) = lim [sub r [rarr] [infin]] negative_binomial(r, r / ([lambda] + r)))
  82. [discrete_quantile_warning Negative Binomial]
  83. [h4 Member Functions]
  84. [h5 Construct]
  85. negative_binomial_distribution(RealType r, RealType p);
  86. Constructor: /r/ is the total number of successes, /p/ is the
  87. probability of success of a single trial.
  88. Requires: `r > 0` and `0 <= p <= 1`.
  89. [h5 Accessors]
  90. RealType success_fraction() const; // successes / trials (0 <= p <= 1)
  91. Returns the parameter /p/ from which this distribution was constructed.
  92. RealType successes() const; // required successes (r > 0)
  93. Returns the parameter /r/ from which this distribution was constructed.
  94. The best method of calculation for the following functions is disputed:
  95. see __binomial_distrib for more discussion.
  96. [h5 Lower Bound on Parameter p]
  97. static RealType find_lower_bound_on_p(
  98. RealType failures,
  99. RealType successes,
  100. RealType probability) // (0 <= alpha <= 1), 0.05 equivalent to 95% confidence.
  101. Returns a *lower bound* on the success fraction:
  102. [variablelist
  103. [[failures][The total number of failures before the ['r]th success.]]
  104. [[successes][The number of successes required.]]
  105. [[alpha][The largest acceptable probability that the true value of
  106. the success fraction is [*less than] the value returned.]]
  107. ]
  108. For example, if you observe /k/ failures and /r/ successes from /n/ = k + r trials
  109. the best estimate for the success fraction is simply ['r/n], but if you
  110. want to be 95% sure that the true value is [*greater than] some value,
  111. ['p[sub min]], then:
  112. p``[sub min]`` = negative_binomial_distribution<RealType>::find_lower_bound_on_p(
  113. failures, successes, 0.05);
  114. [link math_toolkit.stat_tut.weg.neg_binom_eg.neg_binom_conf See negative binomial confidence interval example.]
  115. This function uses the Clopper-Pearson method of computing the lower bound on the
  116. success fraction, whilst many texts refer to this method as giving an "exact"
  117. result in practice it produces an interval that guarantees ['at least] the
  118. coverage required, and may produce pessimistic estimates for some combinations
  119. of /failures/ and /successes/. See:
  120. [@http://www.ucs.louisiana.edu/~kxk4695/Discrete_new.pdf
  121. Yong Cai and K. Krishnamoorthy, A Simple Improved Inferential Method for Some Discrete Distributions.
  122. Computational statistics and data analysis, 2005, vol. 48, no3, 605-621].
  123. [h5 Upper Bound on Parameter p]
  124. static RealType find_upper_bound_on_p(
  125. RealType trials,
  126. RealType successes,
  127. RealType alpha); // (0 <= alpha <= 1), 0.05 equivalent to 95% confidence.
  128. Returns an *upper bound* on the success fraction:
  129. [variablelist
  130. [[trials][The total number of trials conducted.]]
  131. [[successes][The number of successes that occurred.]]
  132. [[alpha][The largest acceptable probability that the true value of
  133. the success fraction is [*greater than] the value returned.]]
  134. ]
  135. For example, if you observe /k/ successes from /n/ trials the
  136. best estimate for the success fraction is simply ['k/n], but if you
  137. want to be 95% sure that the true value is [*less than] some value,
  138. ['p[sub max]], then:
  139. p``[sub max]`` = negative_binomial_distribution<RealType>::find_upper_bound_on_p(
  140. r, k, 0.05);
  141. [link math_toolkit.stat_tut.weg.neg_binom_eg.neg_binom_conf See negative binomial confidence interval example.]
  142. This function uses the Clopper-Pearson method of computing the lower bound on the
  143. success fraction, whilst many texts refer to this method as giving an "exact"
  144. result in practice it produces an interval that guarantees ['at least] the
  145. coverage required, and may produce pessimistic estimates for some combinations
  146. of /failures/ and /successes/. See:
  147. [@http://www.ucs.louisiana.edu/~kxk4695/Discrete_new.pdf
  148. Yong Cai and K. Krishnamoorthy, A Simple Improved Inferential Method for Some Discrete Distributions.
  149. Computational statistics and data analysis, 2005, vol. 48, no3, 605-621].
  150. [h5 Estimating Number of Trials to Ensure at Least a Certain Number of Failures]
  151. static RealType find_minimum_number_of_trials(
  152. RealType k, // number of failures.
  153. RealType p, // success fraction.
  154. RealType alpha); // probability threshold (0.05 equivalent to 95%).
  155. This functions estimates the number of trials required to achieve a certain
  156. probability that [*more than k failures will be observed].
  157. [variablelist
  158. [[k][The target number of failures to be observed.]]
  159. [[p][The probability of ['success] for each trial.]]
  160. [[alpha][The maximum acceptable risk that only k failures or fewer will be observed.]]
  161. ]
  162. For example:
  163. negative_binomial_distribution<RealType>::find_minimum_number_of_trials(10, 0.5, 0.05);
  164. Returns the smallest number of trials we must conduct to be 95% sure
  165. of seeing 10 failures that occur with frequency one half.
  166. [link math_toolkit.stat_tut.weg.neg_binom_eg.neg_binom_size_eg Worked Example.]
  167. This function uses numeric inversion of the negative binomial distribution
  168. to obtain the result: another interpretation of the result, is that it finds
  169. the number of trials (success+failures) that will lead to an /alpha/ probability
  170. of observing k failures or fewer.
  171. [h5 Estimating Number of Trials to Ensure a Maximum Number of Failures or Less]
  172. static RealType find_maximum_number_of_trials(
  173. RealType k, // number of failures.
  174. RealType p, // success fraction.
  175. RealType alpha); // probability threshold (0.05 equivalent to 95%).
  176. This functions estimates the maximum number of trials we can conduct and achieve
  177. a certain probability that [*k failures or fewer will be observed].
  178. [variablelist
  179. [[k][The maximum number of failures to be observed.]]
  180. [[p][The probability of ['success] for each trial.]]
  181. [[alpha][The maximum acceptable ['risk] that more than k failures will be observed.]]
  182. ]
  183. For example:
  184. negative_binomial_distribution<RealType>::find_maximum_number_of_trials(0, 1.0-1.0/1000000, 0.05);
  185. Returns the largest number of trials we can conduct and still be 95% sure
  186. of seeing no failures that occur with frequency one in one million.
  187. This function uses numeric inversion of the negative binomial distribution
  188. to obtain the result: another interpretation of the result, is that it finds
  189. the number of trials (success+failures) that will lead to an /alpha/ probability
  190. of observing more than k failures.
  191. [h4 Non-member Accessors]
  192. All the [link math_toolkit.dist_ref.nmp usual non-member accessor functions]
  193. that are generic to all distributions are supported: __usual_accessors.
  194. However it's worth taking a moment to define what these actually mean in
  195. the context of this distribution:
  196. [table Meaning of the non-member accessors.
  197. [[Function][Meaning]]
  198. [[__pdf]
  199. [The probability of obtaining [*exactly k failures] from k+r trials
  200. with success fraction p. For example:
  201. ``pdf(negative_binomial(r, p), k)``]]
  202. [[__cdf]
  203. [The probability of obtaining [*k failures or fewer] from k+r trials
  204. with success fraction p and success on the last trial. For example:
  205. ``cdf(negative_binomial(r, p), k)``]]
  206. [[__ccdf]
  207. [The probability of obtaining [*more than k failures] from k+r trials
  208. with success fraction p and success on the last trial. For example:
  209. ``cdf(complement(negative_binomial(r, p), k))``]]
  210. [[__quantile]
  211. [The [*greatest] number of failures k expected to be observed from k+r trials
  212. with success fraction p, at probability P. Note that the value returned
  213. is a real-number, and not an integer. Depending on the use case you may
  214. want to take either the floor or ceiling of the real result. For example:
  215. ``quantile(negative_binomial(r, p), P)``]]
  216. [[__quantile_c]
  217. [The [*smallest] number of failures k expected to be observed from k+r trials
  218. with success fraction p, at probability P. Note that the value returned
  219. is a real-number, and not an integer. Depending on the use case you may
  220. want to take either the floor or ceiling of the real result. For example:
  221. ``quantile(complement(negative_binomial(r, p), P))``]]
  222. ]
  223. [h4 Accuracy]
  224. This distribution is implemented using the
  225. incomplete beta functions __ibeta and __ibetac:
  226. please refer to these functions for information on accuracy.
  227. [h4 Implementation]
  228. In the following table, /p/ is the probability that any one trial will
  229. be successful (the success fraction), /r/ is the number of successes,
  230. /k/ is the number of failures, /p/ is the probability and /q = 1-p/.
  231. [table
  232. [[Function][Implementation Notes]]
  233. [[pdf][pdf = exp(lgamma(r + k) - lgamma(r) - lgamma(k+1)) * pow(p, r) * pow((1-p), k)
  234. Implementation is in terms of __ibeta_derivative:
  235. (p/(r + k)) * ibeta_derivative(r, static_cast<RealType>(k+1), p)
  236. The function __ibeta_derivative is used here, since it has already
  237. been optimised for the lowest possible error - indeed this is really
  238. just a thin wrapper around part of the internals of the incomplete
  239. beta function.
  240. ]]
  241. [[cdf][Using the relation:
  242. cdf = I[sub p](r, k+1) = ibeta(r, k+1, p)
  243. = ibeta(r, static_cast<RealType>(k+1), p)]]
  244. [[cdf complement][Using the relation:
  245. 1 - cdf = I[sub p](k+1, r)
  246. = ibetac(r, static_cast<RealType>(k+1), p)
  247. ]]
  248. [[quantile][ibeta_invb(r, p, P) - 1]]
  249. [[quantile from the complement][ibetac_invb(r, p, Q) -1)]]
  250. [[mean][ `r(1-p)/p` ]]
  251. [[variance][ `r (1-p) / p * p` ]]
  252. [[mode][`floor((r-1) * (1 - p)/p)`]]
  253. [[skewness][`(2 - p) / sqrt(r * (1 - p))`]]
  254. [[kurtosis][`6 / r + (p * p) / r * (1 - p )`]]
  255. [[kurtosis excess][`6 / r + (p * p) / r * (1 - p ) -3`]]
  256. [[parameter estimation member functions][]]
  257. [[`find_lower_bound_on_p`][ibeta_inv(successes, failures + 1, alpha)]]
  258. [[`find_upper_bound_on_p`][ibetac_inv(successes, failures, alpha) plus see comments in code.]]
  259. [[`find_minimum_number_of_trials`][ibeta_inva(k + 1, p, alpha)]]
  260. [[`find_maximum_number_of_trials`][ibetac_inva(k + 1, p, alpha)]]
  261. ]
  262. Implementation notes:
  263. * The real concept type (that deliberately lacks the Lanczos approximation),
  264. was found to take several minutes to evaluate some extreme test values,
  265. so the test has been disabled for this type.
  266. * Much greater speed, and perhaps greater accuracy,
  267. might be achieved for extreme values by using a normal approximation.
  268. This is NOT been tested or implemented.
  269. [endsect][/section:negative_binomial_dist Negative Binomial]
  270. [/ negative_binomial.qbk
  271. Copyright 2006 John Maddock and Paul A. Bristow.
  272. Distributed under the Boost Software License, Version 1.0.
  273. (See accompanying file LICENSE_1_0.txt or copy at
  274. http://www.boost.org/LICENSE_1_0.txt).
  275. ]