hypergeometric.qbk 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. [section:hypergeometric_dist Hypergeometric Distribution]
  2. ``#include <boost/math/distributions/hypergeometric.hpp>``
  3. namespace boost{ namespace math{
  4. template <class RealType = double,
  5. class ``__Policy`` = ``__policy_class`` >
  6. class hypergeometric_distribution;
  7. template <class RealType, class Policy>
  8. class hypergeometric_distribution
  9. {
  10. public:
  11. typedef RealType value_type;
  12. typedef Policy policy_type;
  13. // Construct:
  14. hypergeometric_distribution(unsigned r, unsigned n, unsigned N);
  15. // Accessors:
  16. unsigned total()const;
  17. unsigned defective()const;
  18. unsigned sample_count()const;
  19. };
  20. typedef hypergeometric_distribution<> hypergeometric;
  21. }} // namespaces
  22. The hypergeometric distribution describes the number of "events" /k/
  23. from a sample /n/ drawn from a total population /N/ ['without replacement].
  24. Imagine we have a sample of /N/ objects of which /r/ are "defective"
  25. and N-r are "not defective"
  26. (the terms "success\/failure" or "red\/blue" are also used). If we sample /n/
  27. items /without replacement/ then what is the probability that exactly
  28. /k/ items in the sample are defective? The answer is given by the pdf of the
  29. hypergeometric distribution `f(k; r, n, N)`, whilst the probability of
  30. /k/ defectives or fewer is given by F(k; r, n, N), where F(k) is the
  31. CDF of the hypergeometric distribution.
  32. [note Unlike almost all of the other distributions in this library,
  33. the hypergeometric distribution is strictly discrete: it can not be
  34. extended to real valued arguments of its parameters or random variable.]
  35. The following graph shows how the distribution changes as the proportion
  36. of "defective" items changes, while keeping the population and sample sizes
  37. constant:
  38. [graph hypergeometric_pdf_1]
  39. Note that since the distribution is symmetrical in parameters /n/ and /r/, if we
  40. change the sample size and keep the population and proportion "defective" the same
  41. then we obtain basically the same graphs:
  42. [graph hypergeometric_pdf_2]
  43. [h4 Member Functions]
  44. hypergeometric_distribution(unsigned r, unsigned n, unsigned N);
  45. Constructs a hypergeometric distribution with a population of /N/ objects,
  46. of which /r/ are defective, and from which /n/ are sampled.
  47. unsigned total()const;
  48. Returns the total number of objects /N/.
  49. unsigned defective()const;
  50. Returns the number of objects /r/ in population /N/ which are defective.
  51. unsigned sample_count()const;
  52. Returns the number of objects /n/ which are sampled from the population /N/.
  53. [h4 Non-member Accessors]
  54. All the [link math_toolkit.dist_ref.nmp usual non-member accessor functions]
  55. that are generic to all distributions are supported: __usual_accessors.
  56. The domain of the random variable is the unsigned integers in the range
  57. \[max(0, n + r - N), min(n, r)\]. A __domain_error is raised if the
  58. random variable is outside this range, or is not an integral value.
  59. [caution
  60. The quantile function will by default return an integer result that has been
  61. /rounded outwards/. That is to say lower quantiles (where the probability is
  62. less than 0.5) are rounded downward, and upper quantiles (where the probability
  63. is greater than 0.5) are rounded upwards. This behaviour
  64. ensures that if an X% quantile is requested, then /at least/ the requested
  65. coverage will be present in the central region, and /no more than/
  66. the requested coverage will be present in the tails.
  67. This behaviour can be changed so that the quantile functions are rounded
  68. differently using
  69. [link math_toolkit.pol_overview Policies]. It is strongly
  70. recommended that you read the tutorial
  71. [link math_toolkit.pol_tutorial.understand_dis_quant
  72. Understanding Quantiles of Discrete Distributions] before
  73. using the quantile function on the Hypergeometric distribution. The
  74. [link math_toolkit.pol_ref.discrete_quant_ref reference docs]
  75. describe how to change the rounding policy
  76. for these distributions.
  77. However, note that the implementation method of the quantile function
  78. always returns an integral value, therefore attempting to use a __Policy
  79. that requires (or produces) a real valued result will result in a
  80. compile time error.
  81. ] [/ caution]
  82. [h4 Accuracy]
  83. For small N such that
  84. `N < boost::math::max_factorial<RealType>::value` then table based
  85. lookup of the results gives an accuracy to a few epsilon.
  86. `boost::math::max_factorial<RealType>::value` is 170 at double or long double
  87. precision.
  88. For larger N such that `N < boost::math::prime(boost::math::max_prime)`
  89. then only basic arithmetic is required for the calculation
  90. and the accuracy is typically < 20 epsilon. This takes care of N
  91. up to 104729.
  92. For `N > boost::math::prime(boost::math::max_prime)` then accuracy quickly
  93. degrades, with 5 or 6 decimal digits being lost for N = 110000.
  94. In general for very large N, the user should expect to lose log[sub 10]N
  95. decimal digits of precision during the calculation, with the results
  96. becoming meaningless for N >= 10[super 15].
  97. [h4 Testing]
  98. There are three sets of tests: our implementation is tested against a table of values
  99. produced by Mathematica's implementation of this distribution. We also sanity check
  100. our implementation against some spot values computed using the online calculator
  101. here [@http://stattrek.com/Tables/Hypergeometric.aspx http://stattrek.com/Tables/Hypergeometric.aspx].
  102. Finally we test accuracy against some high precision test data using
  103. this implementation and NTL::RR.
  104. [h4 Implementation]
  105. The PDF can be calculated directly using the formula:
  106. [equation hypergeometric1]
  107. However, this can only be used directly when the largest of the factorials
  108. is guaranteed not to overflow the floating point representation used.
  109. This formula is used directly when `N < max_factorial<RealType>::value`
  110. in which case table lookup of the factorials gives a rapid and accurate
  111. implementation method.
  112. For larger /N/ the method described in
  113. "An Accurate Computation of the Hypergeometric Distribution Function",
  114. Trong Wu, ACM Transactions on Mathematical Software, Vol. 19, No. 1,
  115. March 1993, Pages 33-43 is used. The method relies on the fact that
  116. there is an easy method for factorising a factorial into the product
  117. of prime numbers:
  118. [equation hypergeometric2]
  119. Where p[sub i] is the i'th prime number, and e[sub i] is a small
  120. positive integer or zero, which can be calculated via:
  121. [equation hypergeometric3]
  122. Further we can combine the factorials in the expression for the PDF
  123. to yield the PDF directly as the product of prime numbers:
  124. [equation hypergeometric4]
  125. With this time the exponents e[sub i] being either positive, negative
  126. or zero. Indeed such a degree of cancellation occurs in the calculation
  127. of the e[sub i] that many are zero, and typically most have a magnitude
  128. or no more than 1 or 2.
  129. Calculation of the product of the primes requires some care to prevent
  130. numerical overflow, we use a novel recursive method which splits the
  131. calculation into a series of sub-products, with a new sub-product
  132. started each time the next multiplication would cause either overflow
  133. or underflow. The sub-products are stored in a linked list on the
  134. program stack, and combined in an order that will guarantee no overflow
  135. or unnecessary-underflow once the last sub-product has been calculated.
  136. This method can be used as long as N is smaller than the largest prime
  137. number we have stored in our table of primes (currently 104729). The method
  138. is relatively slow (calculating the exponents requires the most time), but
  139. requires only a small number of arithmetic operations to
  140. calculate the result (indeed there is no shorter method involving only basic
  141. arithmetic once the exponents have been found), the method is therefore
  142. much more accurate than the alternatives.
  143. For much larger N, we can calculate the PDF from the factorials using
  144. either lgamma, or by directly combining lanczos approximations to avoid
  145. calculating via logarithms. We use the latter method, as it is usually
  146. 1 or 2 decimal digits more accurate than computing via logarithms with
  147. lgamma. However, in this area where N > 104729, the user should expect
  148. to lose around log[sub 10]N decimal digits during the calculation in
  149. the worst case.
  150. The CDF and its complement is calculated by directly summing the PDF's.
  151. We start by deciding whether the CDF, or its complement, is likely to be
  152. the smaller of the two and then calculate the PDF at /k/ (or /k+1/ if we're
  153. calculating the complement) and calculate successive PDF values via the
  154. recurrence relations:
  155. [equation hypergeometric5]
  156. Until we either reach the end of the distributions domain, or the next
  157. PDF value to be summed would be too small to affect the result.
  158. The quantile is calculated in a similar manner to the CDF: we first guess
  159. which end of the distribution we're nearer to, and then sum PDFs starting
  160. from the end of the distribution this time, until we have some value /k/ that
  161. gives the required CDF.
  162. The median is simply the quantile at 0.5, and the remaining properties are
  163. calculated via:
  164. [equation hypergeometric6]
  165. [endsect]
  166. [/ hypergeometric.qbk
  167. Copyright 2008 John Maddock.
  168. Distributed under the Boost Software License, Version 1.0.
  169. (See accompanying file LICENSE_1_0.txt or copy at
  170. http://www.boost.org/LICENSE_1_0.txt).
  171. ]