geometric.qbk 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. [section:geometric_dist Geometric Distribution]
  2. ``#include <boost/math/distributions/geometric.hpp>``
  3. namespace boost{ namespace math{
  4. template <class RealType = double,
  5. class ``__Policy`` = ``__policy_class`` >
  6. class geometric_distribution;
  7. typedef geometric_distribution<> geometric;
  8. template <class RealType, class ``__Policy``>
  9. class geometric_distribution
  10. {
  11. public:
  12. typedef RealType value_type;
  13. typedef Policy policy_type;
  14. // Constructor from success_fraction:
  15. geometric_distribution(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 `geometric_distribution` represents a
  40. [@http://en.wikipedia.org/wiki/geometric_distribution geometric 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 [@http://en.wikipedia.org/wiki/Bernoulli_trial Bernoulli trials]
  45. each with success fraction /p/, the geometric distribution gives
  46. the probability of observing /k/ trials (failures, events, occurrences, or arrivals)
  47. before the first success.
  48. [note For this implementation, the set of trials *includes zero*
  49. (unlike another definition where the set of trials starts at one, sometimes named /shifted/).]
  50. The geometric distribution assumes that success_fraction /p/ is fixed for all /k/ trials.
  51. The probability that there are /k/ failures before the first success
  52. [expression Pr(Y=/k/) = (1-/p/)[super /k/] /p/]
  53. For example, when throwing a 6-face dice the success probability /p/ = 1/6 = 0.1666[recur].
  54. Throwing repeatedly until a /three/ appears,
  55. the probability distribution of the number of times /not-a-three/ is thrown is geometric.
  56. Geometric distribution has the Probability Density Function PDF:
  57. [expression (1-/p/)[super /k/] /p/]
  58. The following graph illustrates how the PDF and CDF vary for three examples
  59. of the success fraction /p/,
  60. (when considering the geometric distribution as a continuous function),
  61. [graph geometric_pdf_2]
  62. [graph geometric_cdf_2]
  63. and as discrete.
  64. [graph geometric_pdf_discrete]
  65. [graph geometric_cdf_discrete]
  66. [h4 Related Distributions]
  67. The geometric distribution is a special case of
  68. the __negative_binomial_distrib with successes parameter /r/ = 1,
  69. so only one first and only success is required : thus by definition
  70. __spaces `geometric(p) == negative_binomial(1, p)`
  71. negative_binomial_distribution(RealType r, RealType success_fraction);
  72. negative_binomial nb(1, success_fraction);
  73. geometric g(success_fraction);
  74. ASSERT(pdf(nb, 1) == pdf(g, 1));
  75. This implementation uses real numbers for the computation throughout
  76. (because it uses the *real-valued* power and exponential functions).
  77. So to obtain a conventional strictly-discrete geometric distribution
  78. you must ensure that an integer value is provided for the number of trials
  79. (random variable) /k/,
  80. and take integer values (floor or ceil functions) from functions that return
  81. a number of successes.
  82. [discrete_quantile_warning geometric]
  83. [h4 Member Functions]
  84. [h5 Constructor]
  85. geometric_distribution(RealType p);
  86. Constructor: /p/ or success_fraction is the probability of success of a single trial.
  87. Requires: `0 <= p <= 1`.
  88. [h5 Accessors]
  89. RealType success_fraction() const; // successes / trials (0 <= p <= 1)
  90. Returns the success_fraction parameter /p/ from which this distribution was constructed.
  91. RealType successes() const; // required successes always one,
  92. // included for compatibility with negative binomial distribution
  93. // with successes r == 1.
  94. Returns unity.
  95. The following functions are equivalent to those provided for the negative binomial,
  96. with successes = 1, but are provided here for completeness.
  97. The best method of calculation for the following functions is disputed:
  98. see __binomial_distrib and __negative_binomial_distrib for more discussion.
  99. [h5 Lower Bound on success_fraction Parameter ['p]]
  100. static RealType find_lower_bound_on_p(
  101. RealType failures,
  102. RealType probability) // (0 <= alpha <= 1), 0.05 equivalent to 95% confidence.
  103. Returns a *lower bound* on the success fraction:
  104. [variablelist
  105. [[failures][The total number of failures before the 1st success.]]
  106. [[alpha][The largest acceptable probability that the true value of
  107. the success fraction is [*less than] the value returned.]]
  108. ]
  109. For example, if you observe /k/ failures from /n/ trials
  110. the best estimate for the success fraction is simply 1/['n], but if you
  111. want to be 95% sure that the true value is [*greater than] some value,
  112. ['p[sub min]], then:
  113. p``[sub min]`` = geometric_distribution<RealType>::
  114. find_lower_bound_on_p(failures, 0.05);
  115. [link math_toolkit.stat_tut.weg.neg_binom_eg.neg_binom_conf See negative_binomial confidence interval example.]
  116. This function uses the Clopper-Pearson method of computing the lower bound on the
  117. success fraction, whilst many texts refer to this method as giving an "exact"
  118. result in practice it produces an interval that guarantees ['at least] the
  119. coverage required, and may produce pessimistic estimates for some combinations
  120. of /failures/ and /successes/. See:
  121. [@http://www.ucs.louisiana.edu/~kxk4695/Discrete_new.pdf
  122. Yong Cai and K. Krishnamoorthy, A Simple Improved Inferential Method for Some Discrete Distributions.
  123. Computational statistics and data analysis, 2005, vol. 48, no3, 605-621].
  124. [h5 Upper Bound on success_fraction Parameter p]
  125. static RealType find_upper_bound_on_p(
  126. RealType trials,
  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. [[alpha][The largest acceptable probability that the true value of
  132. the success fraction is [*greater than] the value returned.]]
  133. ]
  134. For example, if you observe /k/ successes from /n/ trials the
  135. best estimate for the success fraction is simply ['k/n], but if you
  136. want to be 95% sure that the true value is [*less than] some value,
  137. ['p[sub max]], then:
  138. p``[sub max]`` = geometric_distribution<RealType>::find_upper_bound_on_p(
  139. k, 0.05);
  140. [link math_toolkit.stat_tut.weg.neg_binom_eg.neg_binom_conf See negative binomial confidence interval example.]
  141. This function uses the Clopper-Pearson method of computing the lower bound on the
  142. success fraction, whilst many texts refer to this method as giving an "exact"
  143. result in practice it produces an interval that guarantees ['at least] the
  144. coverage required, and may produce pessimistic estimates for some combinations
  145. of /failures/ and /successes/. See:
  146. [@http://www.ucs.louisiana.edu/~kxk4695/Discrete_new.pdf
  147. Yong Cai and K. Krishnamoorthy, A Simple Improved Inferential Method for Some Discrete Distributions.
  148. Computational statistics and data analysis, 2005, vol. 48, no3, 605-621].
  149. [h5 Estimating Number of Trials to Ensure at Least a Certain Number of Failures]
  150. static RealType find_minimum_number_of_trials(
  151. RealType k, // number of failures.
  152. RealType p, // success fraction.
  153. RealType alpha); // probability threshold (0.05 equivalent to 95%).
  154. This functions estimates the number of trials required to achieve a certain
  155. probability that [*more than ['k] failures will be observed].
  156. [variablelist
  157. [[k][The target number of failures to be observed.]]
  158. [[p][The probability of ['success] for each trial.]]
  159. [[alpha][The maximum acceptable ['risk] that only ['k] failures or fewer will be observed.]]
  160. ]
  161. For example:
  162. geometric_distribution<RealType>::find_minimum_number_of_trials(10, 0.5, 0.05);
  163. Returns the smallest number of trials we must conduct to be 95% (1-0.05) sure
  164. of seeing 10 failures that occur with frequency one half.
  165. [link math_toolkit.stat_tut.weg.neg_binom_eg.neg_binom_size_eg Worked Example.]
  166. This function uses numeric inversion of the geometric distribution
  167. to obtain the result: another interpretation of the result is that it finds
  168. the number of trials (failures) that will lead to an /alpha/ probability
  169. of observing /k/ failures or fewer.
  170. [h5 Estimating Number of Trials to Ensure a Maximum Number of Failures or Less]
  171. static RealType find_maximum_number_of_trials(
  172. RealType k, // number of failures.
  173. RealType p, // success fraction.
  174. RealType alpha); // probability threshold (0.05 equivalent to 95%).
  175. This functions estimates the maximum number of trials we can conduct and achieve
  176. a certain probability that [*k failures or fewer will be observed].
  177. [variablelist
  178. [[k][The maximum number of failures to be observed.]]
  179. [[p][The probability of ['success] for each trial.]]
  180. [[alpha][The maximum acceptable ['risk] that more than ['k] failures will be observed.]]
  181. ]
  182. For example:
  183. geometric_distribution<RealType>::find_maximum_number_of_trials(0, 1.0-1.0/1000000, 0.05);
  184. Returns the largest number of trials we can conduct and still be 95% sure
  185. of seeing no failures that occur with frequency one in one million.
  186. This function uses numeric inversion of the geometric distribution
  187. to obtain the result: another interpretation of the result, is that it finds
  188. the number of trials that will lead to an /alpha/ probability
  189. of observing more than k failures.
  190. [h4 Non-member Accessors]
  191. All the [link math_toolkit.dist_ref.nmp usual non-member accessor functions]
  192. that are generic to all distributions are supported: __usual_accessors.
  193. However it's worth taking a moment to define what these actually mean in
  194. the context of this distribution:
  195. [table Meaning of the non-member accessors.
  196. [[Function][Meaning]]
  197. [[__pdf]
  198. [The probability of obtaining [*exactly k failures] from /k/ trials
  199. with success fraction p. For example:
  200. ``pdf(geometric(p), k)``]]
  201. [[__cdf]
  202. [The probability of obtaining [*k failures or fewer] from /k/ trials
  203. with success fraction p and success on the last trial. For example:
  204. ``cdf(geometric(p), k)``]]
  205. [[__ccdf]
  206. [The probability of obtaining [*more than k failures] from /k/ trials
  207. with success fraction p and success on the last trial. For example:
  208. ``cdf(complement(geometric(p), k))``]]
  209. [[__quantile]
  210. [The [*greatest] number of failures /k/ expected to be observed from /k/ trials
  211. with success fraction /p/, at probability /P/. Note that the value returned
  212. is a real-number, and not an integer. Depending on the use case you may
  213. want to take either the floor or ceiling of the real result. For example:
  214. ``quantile(geometric(p), P)``]]
  215. [[__quantile_c]
  216. [The [*smallest] number of failures /k/ expected to be observed from /k/ trials
  217. with success fraction /p/, at probability /P/. Note that the value returned
  218. is a real-number, and not an integer. Depending on the use case you may
  219. want to take either the floor or ceiling of the real result. For example:
  220. ``quantile(complement(geometric(p), P))``]]
  221. ]
  222. [h4 Accuracy]
  223. This distribution is implemented using the pow and exp functions, so most results
  224. are accurate within a few epsilon for the RealType.
  225. For extreme values of `double` /p/, for example 0.9999999999,
  226. accuracy can fall significantly, for example to 10 decimal digits (from 16).
  227. [h4 Implementation]
  228. In the following table, /p/ is the probability that any one trial will
  229. be successful (the success fraction), /k/ is the number of failures,
  230. /p/ is the probability and /q = 1-p/,
  231. /x/ is the given probability to estimate
  232. the expected number of failures using the quantile.
  233. [table
  234. [[Function][Implementation Notes]]
  235. [[pdf][pdf = p * pow(q, k)]]
  236. [[cdf][cdf = 1 - q[super k=1]]]
  237. [[cdf complement][exp(log1p(-p) * (k+1))]]
  238. [[quantile][k = log1p(-x) / log1p(-p) -1]]
  239. [[quantile from the complement][k = log(x) / log1p(-p) -1]]
  240. [[mean][(1-p)/p]]
  241. [[variance][(1-p)/p[sup2]]]
  242. [[mode][0]]
  243. [[skewness][(2-p)/[sqrt]q]]
  244. [[kurtosis][9+p[sup2]/q]]
  245. [[kurtosis excess][6 +p[sup2]/q]]
  246. [[parameter estimation member functions][See __negative_binomial_distrib]]
  247. [[`find_lower_bound_on_p`][See __negative_binomial_distrib]]
  248. [[`find_upper_bound_on_p`][See __negative_binomial_distrib]]
  249. [[`find_minimum_number_of_trials`][See __negative_binomial_distrib]]
  250. [[`find_maximum_number_of_trials`][See __negative_binomial_distrib]]
  251. ]
  252. [endsect] [/section:geometric_dist geometric]
  253. [/ geometric.qbk
  254. Copyright 2010 John Maddock and Paul A. Bristow.
  255. Distributed under the Boost Software License, Version 1.0.
  256. (See accompanying file LICENSE_1_0.txt or copy at
  257. http://www.boost.org/LICENSE_1_0.txt).
  258. ]