1 // Copyright John Maddock 2006
2 // Copyright Paul A. Bristow 2010
3
4 // Use, modification and distribution are subject to the
5 // Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt
7 // or copy at http://www.boost.org/LICENSE_1_0.txt)
8
9 #ifdef _MSC_VER
10 # pragma warning(disable: 4512) // assignment operator could not be generated.
11 # pragma warning(disable: 4510) // default constructor could not be generated.
12 # pragma warning(disable: 4610) // can never be instantiated - user defined constructor required.
13 #endif
14
15 #include <iostream>
16 using std::cout; using std::endl;
17 #include <iomanip>
18 using std::fixed; using std::left; using std::right; using std::right; using std::setw;
19 using std::setprecision;
20
21 #include <boost/math/distributions/binomial.hpp>
22
find_max_sample_size(double p,unsigned successes)23 void find_max_sample_size(double p, unsigned successes)
24 {
25 //
26 // p = success ratio.
27 // successes = Total number of observed successes.
28 //
29 // Calculate how many trials we can have to ensure the
30 // maximum number of successes does not exceed "successes".
31 // A typical use would be failure analysis, where you want
32 // zero or fewer "successes" with some probability.
33 //
34 // using namespace boost::math;
35 // Avoid potential binomial_distribution name ambiguity with std <random>
36 using boost::math::binomial_distribution;
37
38 // Print out general info:
39 cout <<
40 "________________________\n"
41 "Maximum Number of Trials\n"
42 "________________________\n\n";
43 cout << setprecision(7);
44 cout << setw(40) << left << "Success ratio" << "= " << p << "\n";
45 cout << setw(40) << left << "Maximum Number of \"successes\" permitted" << "= " << successes << "\n";
46 //
47 // Define a table of confidence intervals:
48 //
49 double alpha[] = { 0.5, 0.25, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 };
50 //
51 // Print table header:
52 //
53 cout << "\n\n"
54 "____________________________\n"
55 "Confidence Max Number\n"
56 " Value (%) Of Trials \n"
57 "____________________________\n";
58 //
59 // Now print out the data for the table rows.
60 //
61 for(unsigned i = 0; i < sizeof(alpha)/sizeof(alpha[0]); ++i)
62 {
63 // Confidence value:
64 cout << fixed << setprecision(3) << setw(10) << right << 100 * (1-alpha[i]);
65 // calculate trials:
66 double t = binomial_distribution<>::find_maximum_number_of_trials(successes, p, alpha[i]);
67 t = floor(t);
68 // Print Trials:
69 cout << fixed << setprecision(0) << setw(15) << right << t << endl;
70 }
71 cout << endl;
72 }
73
main()74 int main()
75 {
76 find_max_sample_size(1.0/1000, 0);
77 find_max_sample_size(1.0/10000, 0);
78 find_max_sample_size(1.0/100000, 0);
79 find_max_sample_size(1.0/1000000, 0);
80
81 return 0;
82 }
83
84
85 /*
86
87 Output:
88
89 binomial_sample_sizes.cpp
90 binomial_sample_sizes_example.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Debug\binomial_sample_sizes_example.exe
91 ________________________
92 Maximum Number of Trials
93 ________________________
94
95 Success ratio = 0.001
96 Maximum Number of "successes" permitted = 0
97
98
99 ____________________________
100 Confidence Max Number
101 Value (%) Of Trials
102 ____________________________
103 50.000 692
104 75.000 287
105 90.000 105
106 95.000 51
107 99.000 10
108 99.900 0
109 99.990 0
110 99.999 0
111
112 ________________________
113 Maximum Number of Trials
114 ________________________
115
116 Success ratio = 0.0001000
117 Maximum Number of "successes" permitted = 0
118
119
120 ____________________________
121 Confidence Max Number
122 Value (%) Of Trials
123 ____________________________
124 50.000 6931
125 75.000 2876
126 90.000 1053
127 95.000 512
128 99.000 100
129 99.900 10
130 99.990 0
131 99.999 0
132
133 ________________________
134 Maximum Number of Trials
135 ________________________
136
137 Success ratio = 0.0000100
138 Maximum Number of "successes" permitted = 0
139
140
141 ____________________________
142 Confidence Max Number
143 Value (%) Of Trials
144 ____________________________
145 50.000 69314
146 75.000 28768
147 90.000 10535
148 95.000 5129
149 99.000 1005
150 99.900 100
151 99.990 10
152 99.999 1
153
154 ________________________
155 Maximum Number of Trials
156 ________________________
157
158 Success ratio = 0.0000010
159 Maximum Number of "successes" permitted = 0
160
161
162 ____________________________
163 Confidence Max Number
164 Value (%) Of Trials
165 ____________________________
166 50.000 693146
167 75.000 287681
168 90.000 105360
169 95.000 51293
170 99.000 10050
171 99.900 1000
172 99.990 100
173 99.999 10
174
175
176 */
177