neg_binomial_sample_sizes.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // neg_binomial_sample_sizes.cpp
  2. // Copyright John Maddock 2006
  3. // Copyright Paul A. Bristow 2007, 2010
  4. // Use, modification and distribution are subject to the
  5. // Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt
  7. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. #include <boost/math/distributions/negative_binomial.hpp>
  9. using boost::math::negative_binomial;
  10. // Default RealType is double so this permits use of:
  11. double find_minimum_number_of_trials(
  12. double k, // number of failures (events), k >= 0.
  13. double p, // fraction of trails for which event occurs, 0 <= p <= 1.
  14. double probability); // probability threshold, 0 <= probability <= 1.
  15. #include <iostream>
  16. using std::cout;
  17. using std::endl;
  18. using std::fixed;
  19. using std::right;
  20. #include <iomanip>
  21. using std::setprecision;
  22. using std::setw;
  23. //[neg_binomial_sample_sizes
  24. /*`
  25. It centres around a routine that prints out a table of
  26. minimum sample sizes (number of trials) for various probability thresholds:
  27. */
  28. void find_number_of_trials(double failures, double p);
  29. /*`
  30. First define a table of significance levels: these are the maximum
  31. acceptable probability that /failure/ or fewer events will be observed.
  32. */
  33. double alpha[] = { 0.5, 0.25, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 };
  34. /*`
  35. Confidence value as % is (1 - alpha) * 100, so alpha 0.05 == 95% confidence
  36. that the desired number of failures will be observed.
  37. The values range from a very low 0.5 or 50% confidence up to an extremely high
  38. confidence of 99.999.
  39. Much of the rest of the program is pretty-printing, the important part
  40. is in the calculation of minimum number of trials required for each
  41. value of alpha using:
  42. (int)ceil(negative_binomial::find_minimum_number_of_trials(failures, p, alpha[i]);
  43. find_minimum_number_of_trials returns a double,
  44. so `ceil` rounds this up to ensure we have an integral minimum number of trials.
  45. */
  46. void find_number_of_trials(double failures, double p)
  47. {
  48. // trials = number of trials
  49. // failures = number of failures before achieving required success(es).
  50. // p = success fraction (0 <= p <= 1.).
  51. //
  52. // Calculate how many trials we need to ensure the
  53. // required number of failures DOES exceed "failures".
  54. cout << "\n""Target number of failures = " << (int)failures;
  55. cout << ", Success fraction = " << fixed << setprecision(1) << 100 * p << "%" << endl;
  56. // Print table header:
  57. cout << "____________________________\n"
  58. "Confidence Min Number\n"
  59. " Value (%) Of Trials \n"
  60. "____________________________\n";
  61. // Now print out the data for the alpha table values.
  62. for(unsigned i = 0; i < sizeof(alpha)/sizeof(alpha[0]); ++i)
  63. { // Confidence values %:
  64. cout << fixed << setprecision(3) << setw(10) << right << 100 * (1-alpha[i]) << " "
  65. // find_minimum_number_of_trials
  66. << setw(6) << right
  67. << (int)ceil(negative_binomial::find_minimum_number_of_trials(failures, p, alpha[i]))
  68. << endl;
  69. }
  70. cout << endl;
  71. } // void find_number_of_trials(double failures, double p)
  72. /*` finally we can produce some tables of minimum trials for the chosen confidence levels:
  73. */
  74. int main()
  75. {
  76. find_number_of_trials(5, 0.5);
  77. find_number_of_trials(50, 0.5);
  78. find_number_of_trials(500, 0.5);
  79. find_number_of_trials(50, 0.1);
  80. find_number_of_trials(500, 0.1);
  81. find_number_of_trials(5, 0.9);
  82. return 0;
  83. } // int main()
  84. //] [/neg_binomial_sample_sizes.cpp end of Quickbook in C++ markup]
  85. /*
  86. Output is:
  87. Target number of failures = 5, Success fraction = 50.0%
  88. ____________________________
  89. Confidence Min Number
  90. Value (%) Of Trials
  91. ____________________________
  92. 50.000 11
  93. 75.000 14
  94. 90.000 17
  95. 95.000 18
  96. 99.000 22
  97. 99.900 27
  98. 99.990 31
  99. 99.999 36
  100. Target number of failures = 50, Success fraction = 50.0%
  101. ____________________________
  102. Confidence Min Number
  103. Value (%) Of Trials
  104. ____________________________
  105. 50.000 101
  106. 75.000 109
  107. 90.000 115
  108. 95.000 119
  109. 99.000 128
  110. 99.900 137
  111. 99.990 146
  112. 99.999 154
  113. Target number of failures = 500, Success fraction = 50.0%
  114. ____________________________
  115. Confidence Min Number
  116. Value (%) Of Trials
  117. ____________________________
  118. 50.000 1001
  119. 75.000 1023
  120. 90.000 1043
  121. 95.000 1055
  122. 99.000 1078
  123. 99.900 1104
  124. 99.990 1126
  125. 99.999 1146
  126. Target number of failures = 50, Success fraction = 10.0%
  127. ____________________________
  128. Confidence Min Number
  129. Value (%) Of Trials
  130. ____________________________
  131. 50.000 56
  132. 75.000 58
  133. 90.000 60
  134. 95.000 61
  135. 99.000 63
  136. 99.900 66
  137. 99.990 68
  138. 99.999 71
  139. Target number of failures = 500, Success fraction = 10.0%
  140. ____________________________
  141. Confidence Min Number
  142. Value (%) Of Trials
  143. ____________________________
  144. 50.000 556
  145. 75.000 562
  146. 90.000 567
  147. 95.000 570
  148. 99.000 576
  149. 99.900 583
  150. 99.990 588
  151. 99.999 594
  152. Target number of failures = 5, Success fraction = 90.0%
  153. ____________________________
  154. Confidence Min Number
  155. Value (%) Of Trials
  156. ____________________________
  157. 50.000 57
  158. 75.000 73
  159. 90.000 91
  160. 95.000 103
  161. 99.000 127
  162. 99.900 159
  163. 99.990 189
  164. 99.999 217
  165. Target number of failures = 5, Success fraction = 95.0%
  166. ____________________________
  167. Confidence Min Number
  168. Value (%) Of Trials
  169. ____________________________
  170. 50.000 114
  171. 75.000 148
  172. 90.000 184
  173. 95.000 208
  174. 99.000 259
  175. 99.900 324
  176. 99.990 384
  177. 99.999 442
  178. */