binomial_confidence_limits.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 confidence_limits_on_frequency(unsigned trials, unsigned successes)
  19. {
  20. //
  21. // trials = Total number of trials.
  22. // successes = Total number of observed successes.
  23. //
  24. // Calculate confidence limits for an observed
  25. // frequency of occurrence that follows a binomial distribution.
  26. //
  27. //using namespace std; // Avoid
  28. // using namespace boost::math; // potential name ambiguity with std <random>
  29. using boost::math::binomial_distribution;
  30. // Print out general info:
  31. cout <<
  32. "___________________________________________\n"
  33. "2-Sided Confidence Limits For Success Ratio\n"
  34. "___________________________________________\n\n";
  35. cout << setprecision(7);
  36. cout << setw(40) << left << "Number of Observations" << "= " << trials << "\n";
  37. cout << setw(40) << left << "Number of successes" << "= " << successes << "\n";
  38. cout << setw(40) << left << "Sample frequency of occurrence" << "= " << double(successes) / trials << "\n";
  39. //
  40. // Define a table of significance levels:
  41. //
  42. double alpha[] = { 0.5, 0.25, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 };
  43. //
  44. // Print table header:
  45. //
  46. cout << "\n\n"
  47. "_______________________________________________________________________\n"
  48. "Confidence Lower CP Upper CP Lower JP Upper JP\n"
  49. " Value (%) Limit Limit Limit Limit\n"
  50. "_______________________________________________________________________\n";
  51. //
  52. // Now print out the data for the table rows.
  53. //
  54. for(unsigned i = 0; i < sizeof(alpha)/sizeof(alpha[0]); ++i)
  55. {
  56. // Confidence value:
  57. cout << fixed << setprecision(3) << setw(10) << right << 100 * (1-alpha[i]);
  58. // Calculate Clopper Pearson bounds:
  59. double l = binomial_distribution<>::find_lower_bound_on_p(trials, successes, alpha[i]/2);
  60. double u = binomial_distribution<>::find_upper_bound_on_p(trials, successes, alpha[i]/2);
  61. // Print Clopper Pearson Limits:
  62. cout << fixed << setprecision(5) << setw(15) << right << l;
  63. cout << fixed << setprecision(5) << setw(15) << right << u;
  64. // Calculate Jeffreys Prior Bounds:
  65. l = binomial_distribution<>::find_lower_bound_on_p(trials, successes, alpha[i]/2, binomial_distribution<>::jeffreys_prior_interval);
  66. u = binomial_distribution<>::find_upper_bound_on_p(trials, successes, alpha[i]/2, binomial_distribution<>::jeffreys_prior_interval);
  67. // Print Jeffreys Prior Limits:
  68. cout << fixed << setprecision(5) << setw(15) << right << l;
  69. cout << fixed << setprecision(5) << setw(15) << right << u << std::endl;
  70. }
  71. cout << endl;
  72. } // void confidence_limits_on_frequency()
  73. int main()
  74. {
  75. confidence_limits_on_frequency(20, 4);
  76. confidence_limits_on_frequency(200, 40);
  77. confidence_limits_on_frequency(2000, 400);
  78. return 0;
  79. } // int main()
  80. /*
  81. ------ Build started: Project: binomial_confidence_limits, Configuration: Debug Win32 ------
  82. Compiling...
  83. binomial_confidence_limits.cpp
  84. Linking...
  85. Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\binomial_confidence_limits.exe"
  86. ___________________________________________
  87. 2-Sided Confidence Limits For Success Ratio
  88. ___________________________________________
  89. Number of Observations = 20
  90. Number of successes = 4
  91. Sample frequency of occurrence = 0.2
  92. _______________________________________________________________________
  93. Confidence Lower CP Upper CP Lower JP Upper JP
  94. Value (%) Limit Limit Limit Limit
  95. _______________________________________________________________________
  96. 50.000 0.12840 0.29588 0.14974 0.26916
  97. 75.000 0.09775 0.34633 0.11653 0.31861
  98. 90.000 0.07135 0.40103 0.08734 0.37274
  99. 95.000 0.05733 0.43661 0.07152 0.40823
  100. 99.000 0.03576 0.50661 0.04655 0.47859
  101. 99.900 0.01905 0.58632 0.02634 0.55960
  102. 99.990 0.01042 0.64997 0.01530 0.62495
  103. 99.999 0.00577 0.70216 0.00901 0.67897
  104. ___________________________________________
  105. 2-Sided Confidence Limits For Success Ratio
  106. ___________________________________________
  107. Number of Observations = 200
  108. Number of successes = 40
  109. Sample frequency of occurrence = 0.2000000
  110. _______________________________________________________________________
  111. Confidence Lower CP Upper CP Lower JP Upper JP
  112. Value (%) Limit Limit Limit Limit
  113. _______________________________________________________________________
  114. 50.000 0.17949 0.22259 0.18190 0.22001
  115. 75.000 0.16701 0.23693 0.16934 0.23429
  116. 90.000 0.15455 0.25225 0.15681 0.24956
  117. 95.000 0.14689 0.26223 0.14910 0.25951
  118. 99.000 0.13257 0.28218 0.13468 0.27940
  119. 99.900 0.11703 0.30601 0.11902 0.30318
  120. 99.990 0.10489 0.32652 0.10677 0.32366
  121. 99.999 0.09492 0.34485 0.09670 0.34197
  122. ___________________________________________
  123. 2-Sided Confidence Limits For Success Ratio
  124. ___________________________________________
  125. Number of Observations = 2000
  126. Number of successes = 400
  127. Sample frequency of occurrence = 0.2000000
  128. _______________________________________________________________________
  129. Confidence Lower CP Upper CP Lower JP Upper JP
  130. Value (%) Limit Limit Limit Limit
  131. _______________________________________________________________________
  132. 50.000 0.19382 0.20638 0.19406 0.20613
  133. 75.000 0.18965 0.21072 0.18990 0.21047
  134. 90.000 0.18537 0.21528 0.18561 0.21503
  135. 95.000 0.18267 0.21821 0.18291 0.21796
  136. 99.000 0.17745 0.22400 0.17769 0.22374
  137. 99.900 0.17150 0.23079 0.17173 0.23053
  138. 99.990 0.16658 0.23657 0.16681 0.23631
  139. 99.999 0.16233 0.24169 0.16256 0.24143
  140. */