binomial_example_nag.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright Paul A. 2007, 2010
  2. // Copyright John Maddock 2007
  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. // Simple example of computing probabilities for a binomial random variable.
  8. // Replication of source nag_binomial_dist (g01bjc).
  9. // Shows how to replace NAG C library calls by Boost Math Toolkit C++ calls.
  10. // Note that the default policy does not replicate the way that NAG
  11. // library calls handle 'bad' arguments, but you can define policies that do,
  12. // as well as other policies that may suit your application even better.
  13. // See the examples of changing default policies for details.
  14. #include <boost/math/distributions/binomial.hpp>
  15. #include <iostream>
  16. using std::cout; using std::endl; using std::ios; using std::showpoint;
  17. #include <iomanip>
  18. using std::fixed; using std::setw;
  19. int main()
  20. {
  21. cout << "Using the binomial distribution to replicate a NAG library call." << endl;
  22. using boost::math::binomial_distribution;
  23. // This replicates the computation of the examples of using nag-binomial_dist
  24. // using g01bjc in section g01 Somple Calculations on Statistical Data.
  25. // http://www.nag.co.uk/numeric/cl/manual/pdf/G01/g01bjc.pdf
  26. // Program results section 8.3 page 3.g01bjc.3
  27. //8.2. Program Data
  28. //g01bjc Example Program Data
  29. //4 0.50 2 : n, p, k
  30. //19 0.44 13
  31. //100 0.75 67
  32. //2000 0.33 700
  33. //8.3. Program Results
  34. //g01bjc Example Program Results
  35. //n p k plek pgtk peqk
  36. //4 0.500 2 0.68750 0.31250 0.37500
  37. //19 0.440 13 0.99138 0.00862 0.01939
  38. //100 0.750 67 0.04460 0.95540 0.01700
  39. //2000 0.330 700 0.97251 0.02749 0.00312
  40. cout.setf(ios::showpoint); // Trailing zeros to show significant decimal digits.
  41. cout.precision(5); // Might calculate this from trials in distribution?
  42. cout << fixed;
  43. // Binomial distribution.
  44. // Note that cdf(dist, k) is equivalent to NAG library plek probability of <= k
  45. // cdf(complement(dist, k)) is equivalent to NAG library pgtk probability of > k
  46. // pdf(dist, k) is equivalent to NAG library peqk probability of == k
  47. cout << " n p k plek pgtk peqk " << endl;
  48. binomial_distribution<>my_dist(4, 0.5);
  49. cout << setw(4) << (int)my_dist.trials() << " " << my_dist.success_fraction()
  50. << " " << 2 << " " << cdf(my_dist, 2) << " "
  51. << cdf(complement(my_dist, 2)) << " " << pdf(my_dist, 2) << endl;
  52. binomial_distribution<>two(19, 0.440);
  53. cout << setw(4) << (int)two.trials() << " " << two.success_fraction()
  54. << " " << 13 << " " << cdf(two, 13) << " "
  55. << cdf(complement(two, 13)) << " " << pdf(two, 13) << endl;
  56. binomial_distribution<>three(100, 0.750);
  57. cout << setw(4) << (int)three.trials() << " " << three.success_fraction()
  58. << " " << 67 << " " << cdf(three, 67) << " " << cdf(complement(three, 67))
  59. << " " << pdf(three, 67) << endl;
  60. binomial_distribution<>four(2000, 0.330);
  61. cout << setw(4) << (int)four.trials() << " " << four.success_fraction()
  62. << " " << 700 << " "
  63. << cdf(four, 700) << " " << cdf(complement(four, 700))
  64. << " " << pdf(four, 700) << endl;
  65. return 0;
  66. } // int main()
  67. /*
  68. Example of using the binomial distribution to replicate a NAG library call.
  69. n p k plek pgtk peqk
  70. 4 0.50000 2 0.68750 0.31250 0.37500
  71. 19 0.44000 13 0.99138 0.00862 0.01939
  72. 100 0.75000 67 0.04460 0.95540 0.01700
  73. 2000 0.33000 700 0.97251 0.02749 0.00312
  74. */