binomial_sample_sizes.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright John Maddock 2006
  2. // Copyright Paul A. Bristow 2010
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifdef _MSC_VER
  8. # pragma warning(disable: 4512) // assignment operator could not be generated.
  9. # pragma warning(disable: 4510) // default constructor could not be generated.
  10. # pragma warning(disable: 4610) // can never be instantiated - user defined constructor required.
  11. #endif
  12. #include <iostream>
  13. using std::cout; using std::endl;
  14. #include <iomanip>
  15. using std::fixed; using std::left; using std::right; using std::right; using std::setw;
  16. using std::setprecision;
  17. #include <boost/math/distributions/binomial.hpp>
  18. void find_max_sample_size(double p, unsigned successes)
  19. {
  20. //
  21. // p = success ratio.
  22. // successes = Total number of observed successes.
  23. //
  24. // Calculate how many trials we can have to ensure the
  25. // maximum number of successes does not exceed "successes".
  26. // A typical use would be failure analysis, where you want
  27. // zero or fewer "successes" with some probability.
  28. //
  29. // using namespace boost::math;
  30. // Avoid potential binomial_distribution name ambiguity with std <random>
  31. using boost::math::binomial_distribution;
  32. // Print out general info:
  33. cout <<
  34. "________________________\n"
  35. "Maximum Number of Trials\n"
  36. "________________________\n\n";
  37. cout << setprecision(7);
  38. cout << setw(40) << left << "Success ratio" << "= " << p << "\n";
  39. cout << setw(40) << left << "Maximum Number of \"successes\" permitted" << "= " << successes << "\n";
  40. //
  41. // Define a table of confidence intervals:
  42. //
  43. double alpha[] = { 0.5, 0.25, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 };
  44. //
  45. // Print table header:
  46. //
  47. cout << "\n\n"
  48. "____________________________\n"
  49. "Confidence Max Number\n"
  50. " Value (%) Of Trials \n"
  51. "____________________________\n";
  52. //
  53. // Now print out the data for the table rows.
  54. //
  55. for(unsigned i = 0; i < sizeof(alpha)/sizeof(alpha[0]); ++i)
  56. {
  57. // Confidence value:
  58. cout << fixed << setprecision(3) << setw(10) << right << 100 * (1-alpha[i]);
  59. // calculate trials:
  60. double t = binomial_distribution<>::find_maximum_number_of_trials(successes, p, alpha[i]);
  61. t = floor(t);
  62. // Print Trials:
  63. cout << fixed << setprecision(0) << setw(15) << right << t << endl;
  64. }
  65. cout << endl;
  66. }
  67. int main()
  68. {
  69. find_max_sample_size(1.0/1000, 0);
  70. find_max_sample_size(1.0/10000, 0);
  71. find_max_sample_size(1.0/100000, 0);
  72. find_max_sample_size(1.0/1000000, 0);
  73. return 0;
  74. }
  75. /*
  76. Output:
  77. binomial_sample_sizes.cpp
  78. binomial_sample_sizes_example.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Debug\binomial_sample_sizes_example.exe
  79. ________________________
  80. Maximum Number of Trials
  81. ________________________
  82. Success ratio = 0.001
  83. Maximum Number of "successes" permitted = 0
  84. ____________________________
  85. Confidence Max Number
  86. Value (%) Of Trials
  87. ____________________________
  88. 50.000 692
  89. 75.000 287
  90. 90.000 105
  91. 95.000 51
  92. 99.000 10
  93. 99.900 0
  94. 99.990 0
  95. 99.999 0
  96. ________________________
  97. Maximum Number of Trials
  98. ________________________
  99. Success ratio = 0.0001000
  100. Maximum Number of "successes" permitted = 0
  101. ____________________________
  102. Confidence Max Number
  103. Value (%) Of Trials
  104. ____________________________
  105. 50.000 6931
  106. 75.000 2876
  107. 90.000 1053
  108. 95.000 512
  109. 99.000 100
  110. 99.900 10
  111. 99.990 0
  112. 99.999 0
  113. ________________________
  114. Maximum Number of Trials
  115. ________________________
  116. Success ratio = 0.0000100
  117. Maximum Number of "successes" permitted = 0
  118. ____________________________
  119. Confidence Max Number
  120. Value (%) Of Trials
  121. ____________________________
  122. 50.000 69314
  123. 75.000 28768
  124. 90.000 10535
  125. 95.000 5129
  126. 99.000 1005
  127. 99.900 100
  128. 99.990 10
  129. 99.999 1
  130. ________________________
  131. Maximum Number of Trials
  132. ________________________
  133. Success ratio = 0.0000010
  134. Maximum Number of "successes" permitted = 0
  135. ____________________________
  136. Confidence Max Number
  137. Value (%) Of Trials
  138. ____________________________
  139. 50.000 693146
  140. 75.000 287681
  141. 90.000 105360
  142. 95.000 51293
  143. 99.000 10050
  144. 99.900 1000
  145. 99.990 100
  146. 99.999 10
  147. */