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