negative_binomial_example.qbk 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. [section:neg_binom_eg Negative Binomial Distribution Examples]
  2. (See also the reference documentation for the __negative_binomial_distrib.)
  3. [section:neg_binom_conf Calculating Confidence Limits on the Frequency of Occurrence for the Negative Binomial Distribution]
  4. Imagine you have a process that follows a negative binomial distribution:
  5. for each trial conducted, an event either occurs or does it does not, referred
  6. to as "successes" and "failures". The frequency with which successes occur
  7. is variously referred to as the
  8. success fraction, success ratio, success percentage, occurrence frequency, or probability of occurrence.
  9. If, by experiment, you want to measure the
  10. the best estimate of success fraction is given simply
  11. by /k/ \/ /N/, for /k/ successes out of /N/ trials.
  12. However our confidence in that estimate will be shaped by how many trials were conducted,
  13. and how many successes were observed. The static member functions
  14. `negative_binomial_distribution<>::find_lower_bound_on_p` and
  15. `negative_binomial_distribution<>::find_upper_bound_on_p`
  16. allow you to calculate the confidence intervals for your estimate of the success fraction.
  17. The sample program [@../../example/neg_binom_confidence_limits.cpp
  18. neg_binom_confidence_limits.cpp] illustrates their use.
  19. [import ../../example/neg_binom_confidence_limits.cpp]
  20. [neg_binomial_confidence_limits]
  21. Let's see some sample output for a 1 in 10
  22. success ratio, first for a mere 20 trials:
  23. [pre'''______________________________________________
  24. 2-Sided Confidence Limits For Success Fraction
  25. ______________________________________________
  26. Number of trials = 20
  27. Number of successes = 2
  28. Number of failures = 18
  29. Observed frequency of occurrence = 0.1
  30. ___________________________________________
  31. Confidence Lower Upper
  32. Value (%) Limit Limit
  33. ___________________________________________
  34. 50.000 0.04812 0.13554
  35. 75.000 0.03078 0.17727
  36. 90.000 0.01807 0.22637
  37. 95.000 0.01235 0.26028
  38. 99.000 0.00530 0.33111
  39. 99.900 0.00164 0.41802
  40. 99.990 0.00051 0.49202
  41. 99.999 0.00016 0.55574
  42. ''']
  43. As you can see, even at the 95% confidence level the bounds (0.012 to 0.26) are
  44. really very wide, and very asymmetric about the observed value 0.1.
  45. Compare that with the program output for a mass
  46. 2000 trials:
  47. [pre'''______________________________________________
  48. 2-Sided Confidence Limits For Success Fraction
  49. ______________________________________________
  50. Number of trials = 2000
  51. Number of successes = 200
  52. Number of failures = 1800
  53. Observed frequency of occurrence = 0.1
  54. ___________________________________________
  55. Confidence Lower Upper
  56. Value (%) Limit Limit
  57. ___________________________________________
  58. 50.000 0.09536 0.10445
  59. 75.000 0.09228 0.10776
  60. 90.000 0.08916 0.11125
  61. 95.000 0.08720 0.11352
  62. 99.000 0.08344 0.11802
  63. 99.900 0.07921 0.12336
  64. 99.990 0.07577 0.12795
  65. 99.999 0.07282 0.13206
  66. ''']
  67. Now even when the confidence level is very high, the limits (at 99.999%, 0.07 to 0.13) are really
  68. quite close and nearly symmetric to the observed value of 0.1.
  69. [endsect][/section:neg_binom_conf Calculating Confidence Limits on the Frequency of Occurrence]
  70. [section:neg_binom_size_eg Estimating Sample Sizes for the Negative Binomial.]
  71. Imagine you have an event
  72. (let's call it a "failure" - though we could equally well call it a success if we felt it was a 'good' event)
  73. that you know will occur in 1 in N trials. You may want to know how many trials you need to
  74. conduct to be P% sure of observing at least k such failures.
  75. If the failure events follow a negative binomial
  76. distribution (each trial either succeeds or fails)
  77. then the static member function `negative_binomial_distibution<>::find_minimum_number_of_trials`
  78. can be used to estimate the minimum number of trials required to be P% sure
  79. of observing the desired number of failures.
  80. The example program
  81. [@../../example/neg_binomial_sample_sizes.cpp neg_binomial_sample_sizes.cpp]
  82. demonstrates its usage.
  83. [import ../../example/neg_binomial_sample_sizes.cpp]
  84. [neg_binomial_sample_sizes]
  85. [note Since we're calculating the /minimum/ number of trials required,
  86. we'll err on the safe side and take the ceiling of the result.
  87. Had we been calculating the
  88. /maximum/ number of trials permitted to observe less than a certain
  89. number of /failures/ then we would have taken the floor instead. We
  90. would also have called `find_minimum_number_of_trials` like this:
  91. ``
  92. floor(negative_binomial::find_minimum_number_of_trials(failures, p, alpha[i]))
  93. ``
  94. which would give us the largest number of trials we could conduct and
  95. still be P% sure of observing /failures or less/ failure events, when the
  96. probability of success is /p/.]
  97. We'll finish off by looking at some sample output, firstly suppose
  98. we wish to observe at least 5 "failures" with a 50/50 (0.5) chance of
  99. success or failure:
  100. [pre
  101. '''Target number of failures = 5, Success fraction = 50%
  102. ____________________________
  103. Confidence Min Number
  104. Value (%) Of Trials
  105. ____________________________
  106. 50.000 11
  107. 75.000 14
  108. 90.000 17
  109. 95.000 18
  110. 99.000 22
  111. 99.900 27
  112. 99.990 31
  113. 99.999 36
  114. '''
  115. ]
  116. So 18 trials or more would yield a 95% chance that at least our 5
  117. required failures would be observed.
  118. Compare that to what happens if the success ratio is 90%:
  119. [pre'''Target number of failures = 5.000, Success fraction = 90.000%
  120. ____________________________
  121. Confidence Min Number
  122. Value (%) Of Trials
  123. ____________________________
  124. 50.000 57
  125. 75.000 73
  126. 90.000 91
  127. 95.000 103
  128. 99.000 127
  129. 99.900 159
  130. 99.990 189
  131. 99.999 217
  132. ''']
  133. So now 103 trials are required to observe at least 5 failures with
  134. 95% certainty.
  135. [endsect] [/section:neg_binom_size_eg Estimating Sample Sizes.]
  136. [section:negative_binomial_example1 Negative Binomial Sales Quota Example.]
  137. This example program
  138. [@../../example/negative_binomial_example1.cpp negative_binomial_example1.cpp (full source code)]
  139. demonstrates a simple use to find the probability of meeting a sales quota.
  140. [import ../../example/negative_binomial_example1.cpp]
  141. [negative_binomial_eg1_1]
  142. [negative_binomial_eg1_2]
  143. [endsect] [/section:negative_binomial_example1]
  144. [section:negative_binomial_example2 Negative Binomial Table Printing Example.]
  145. Example program showing output of a table of values of cdf and pdf for various k failures.
  146. [import ../../example/negative_binomial_example2.cpp]
  147. [neg_binomial_example2]
  148. [neg_binomial_example2_1]
  149. [endsect] [/section:negative_binomial_example1 Negative Binomial example 2.]
  150. [endsect] [/section:neg_binom_eg Negative Binomial Distribution Examples]
  151. [/
  152. Copyright 2006 John Maddock and Paul A. Bristow.
  153. Distributed under the Boost Software License, Version 1.0.
  154. (See accompanying file LICENSE_1_0.txt or copy at
  155. http://www.boost.org/LICENSE_1_0.txt).
  156. ]