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