policy_eg_10.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright John Maddock 2007.
  2. // Copyright Paul A. Bristow 2010
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. // Note that this file contains quickbook mark-up as well as code
  7. // and comments, don't change any of the special comment mark-ups!
  8. //[policy_eg_10
  9. /*`
  10. To understand how the rounding policies for
  11. the discrete distributions can be used, we'll
  12. use the 50-sample binomial distribution with a
  13. success fraction of 0.5 once again, and calculate
  14. all the possible quantiles at 0.05 and 0.95.
  15. Begin by including the needed headers (and some using statements for conciseness):
  16. */
  17. #include <iostream>
  18. using std::cout; using std::endl;
  19. using std::left; using std::fixed; using std::right; using std::scientific;
  20. #include <iomanip>
  21. using std::setw;
  22. using std::setprecision;
  23. #include <boost/math/distributions/binomial.hpp>
  24. /*`
  25. Next we'll bring the needed declarations into scope, and
  26. define distribution types for all the available rounding policies:
  27. */
  28. // Avoid
  29. // using namespace std; // and
  30. // using namespace boost::math;
  31. // to avoid potential ambiguity of names, like binomial.
  32. // using namespace boost::math::policies; is small risk, but
  33. // the necessary items are brought into scope thus:
  34. using boost::math::binomial_distribution;
  35. using boost::math::policies::policy;
  36. using boost::math::policies::discrete_quantile;
  37. using boost::math::policies::integer_round_outwards;
  38. using boost::math::policies::integer_round_down;
  39. using boost::math::policies::integer_round_up;
  40. using boost::math::policies::integer_round_nearest;
  41. using boost::math::policies::integer_round_inwards;
  42. using boost::math::policies::real;
  43. using boost::math::binomial_distribution; // Not std::binomial_distribution.
  44. typedef binomial_distribution<
  45. double,
  46. policy<discrete_quantile<integer_round_outwards> > >
  47. binom_round_outwards;
  48. typedef binomial_distribution<
  49. double,
  50. policy<discrete_quantile<integer_round_inwards> > >
  51. binom_round_inwards;
  52. typedef binomial_distribution<
  53. double,
  54. policy<discrete_quantile<integer_round_down> > >
  55. binom_round_down;
  56. typedef binomial_distribution<
  57. double,
  58. policy<discrete_quantile<integer_round_up> > >
  59. binom_round_up;
  60. typedef binomial_distribution<
  61. double,
  62. policy<discrete_quantile<integer_round_nearest> > >
  63. binom_round_nearest;
  64. typedef binomial_distribution<
  65. double,
  66. policy<discrete_quantile<real> > >
  67. binom_real_quantile;
  68. /*`
  69. Now let's set to work calling those quantiles:
  70. */
  71. int main()
  72. {
  73. cout <<
  74. "Testing rounding policies for a 50 sample binomial distribution,\n"
  75. "with a success fraction of 0.5.\n\n"
  76. "Lower quantiles are calculated at p = 0.05\n\n"
  77. "Upper quantiles at p = 0.95.\n\n";
  78. cout << setw(25) << right
  79. << "Policy"<< setw(18) << right
  80. << "Lower Quantile" << setw(18) << right
  81. << "Upper Quantile" << endl;
  82. // Test integer_round_outwards:
  83. cout << setw(25) << right
  84. << "integer_round_outwards"
  85. << setw(18) << right
  86. << quantile(binom_round_outwards(50, 0.5), 0.05)
  87. << setw(18) << right
  88. << quantile(binom_round_outwards(50, 0.5), 0.95)
  89. << endl;
  90. // Test integer_round_inwards:
  91. cout << setw(25) << right
  92. << "integer_round_inwards"
  93. << setw(18) << right
  94. << quantile(binom_round_inwards(50, 0.5), 0.05)
  95. << setw(18) << right
  96. << quantile(binom_round_inwards(50, 0.5), 0.95)
  97. << endl;
  98. // Test integer_round_down:
  99. cout << setw(25) << right
  100. << "integer_round_down"
  101. << setw(18) << right
  102. << quantile(binom_round_down(50, 0.5), 0.05)
  103. << setw(18) << right
  104. << quantile(binom_round_down(50, 0.5), 0.95)
  105. << endl;
  106. // Test integer_round_up:
  107. cout << setw(25) << right
  108. << "integer_round_up"
  109. << setw(18) << right
  110. << quantile(binom_round_up(50, 0.5), 0.05)
  111. << setw(18) << right
  112. << quantile(binom_round_up(50, 0.5), 0.95)
  113. << endl;
  114. // Test integer_round_nearest:
  115. cout << setw(25) << right
  116. << "integer_round_nearest"
  117. << setw(18) << right
  118. << quantile(binom_round_nearest(50, 0.5), 0.05)
  119. << setw(18) << right
  120. << quantile(binom_round_nearest(50, 0.5), 0.95)
  121. << endl;
  122. // Test real:
  123. cout << setw(25) << right
  124. << "real"
  125. << setw(18) << right
  126. << quantile(binom_real_quantile(50, 0.5), 0.05)
  127. << setw(18) << right
  128. << quantile(binom_real_quantile(50, 0.5), 0.95)
  129. << endl;
  130. } // int main()
  131. /*`
  132. Which produces the program output:
  133. [pre
  134. policy_eg_10.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\policy_eg_10.exe
  135. Testing rounding policies for a 50 sample binomial distribution,
  136. with a success fraction of 0.5.
  137. Lower quantiles are calculated at p = 0.05
  138. Upper quantiles at p = 0.95.
  139. Policy Lower Quantile Upper Quantile
  140. integer_round_outwards 18 31
  141. integer_round_inwards 19 30
  142. integer_round_down 18 30
  143. integer_round_up 19 31
  144. integer_round_nearest 19 30
  145. real 18.701 30.299
  146. ]
  147. */
  148. //] //[policy_eg_10] ends quickbook import.