1 // neg_binomial_sample_sizes.cpp
2 
3 // Copyright John Maddock 2006
4 // Copyright Paul A. Bristow 2007, 2010
5 
6 // Use, modification and distribution are subject to the
7 // Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE_1_0.txt
9 // or copy at http://www.boost.org/LICENSE_1_0.txt)
10 
11 #include <boost/math/distributions/negative_binomial.hpp>
12 using boost::math::negative_binomial;
13 
14 // Default RealType is double so this permits use of:
15 double find_minimum_number_of_trials(
16 double k,     // number of failures (events), k >= 0.
17 double p,     // fraction of trails for which event occurs, 0 <= p <= 1.
18 double probability); // probability threshold, 0 <= probability <= 1.
19 
20 #include <iostream>
21 using std::cout;
22 using std::endl;
23 using std::fixed;
24 using std::right;
25 #include <iomanip>
26 using std::setprecision;
27 using std::setw;
28 
29 //[neg_binomial_sample_sizes
30 
31 /*`
32 It centres around a routine that prints out a table of
33 minimum sample sizes (number of trials) for various probability thresholds:
34 */
35   void find_number_of_trials(double failures, double p);
36 /*`
37 First define a table of significance levels: these are the maximum
38 acceptable probability that /failure/ or fewer events will be observed.
39 */
40   double alpha[] = { 0.5, 0.25, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 };
41 /*`
42 Confidence value as % is (1 - alpha) * 100, so alpha 0.05 == 95% confidence
43 that the desired number of failures will be observed.
44 The values range from a very low 0.5 or 50% confidence up to an extremely high
45 confidence of 99.999.
46 
47 Much of the rest of the program is pretty-printing, the important part
48 is in the calculation of minimum number of trials required for each
49 value of alpha using:
50 
51   (int)ceil(negative_binomial::find_minimum_number_of_trials(failures, p, alpha[i]);
52 
53 find_minimum_number_of_trials returns a double,
54 so `ceil` rounds this up to ensure we have an integral minimum number of trials.
55 */
56 
find_number_of_trials(double failures,double p)57 void find_number_of_trials(double failures, double p)
58 {
59    // trials = number of trials
60    // failures = number of failures before achieving required success(es).
61    // p        = success fraction (0 <= p <= 1.).
62    //
63    // Calculate how many trials we need to ensure the
64    // required number of failures DOES exceed "failures".
65 
66   cout << "\n""Target number of failures = " << (int)failures;
67   cout << ",   Success fraction = " << fixed << setprecision(1) << 100 * p << "%" << endl;
68    // Print table header:
69    cout << "____________________________\n"
70            "Confidence        Min Number\n"
71            " Value (%)        Of Trials \n"
72            "____________________________\n";
73    // Now print out the data for the alpha table values.
74   for(unsigned i = 0; i < sizeof(alpha)/sizeof(alpha[0]); ++i)
75    { // Confidence values %:
76       cout << fixed << setprecision(3) << setw(10) << right << 100 * (1-alpha[i]) << "      "
77       // find_minimum_number_of_trials
78       << setw(6) << right
79       << (int)ceil(negative_binomial::find_minimum_number_of_trials(failures, p, alpha[i]))
80       << endl;
81    }
82    cout << endl;
83 } // void find_number_of_trials(double failures, double p)
84 
85 /*` finally we can produce some tables of minimum trials for the chosen confidence levels:
86 */
87 
main()88 int main()
89 {
90     find_number_of_trials(5, 0.5);
91     find_number_of_trials(50, 0.5);
92     find_number_of_trials(500, 0.5);
93     find_number_of_trials(50, 0.1);
94     find_number_of_trials(500, 0.1);
95     find_number_of_trials(5, 0.9);
96 
97     return 0;
98 } // int main()
99 
100 //]  [/neg_binomial_sample_sizes.cpp end of Quickbook in C++ markup]
101 
102 /*
103 
104 Output is:
105 Target number of failures = 5,   Success fraction = 50.0%
106   ____________________________
107   Confidence        Min Number
108    Value (%)        Of Trials
109   ____________________________
110       50.000          11
111       75.000          14
112       90.000          17
113       95.000          18
114       99.000          22
115       99.900          27
116       99.990          31
117       99.999          36
118 
119 
120   Target number of failures = 50,   Success fraction = 50.0%
121   ____________________________
122   Confidence        Min Number
123    Value (%)        Of Trials
124   ____________________________
125       50.000         101
126       75.000         109
127       90.000         115
128       95.000         119
129       99.000         128
130       99.900         137
131       99.990         146
132       99.999         154
133 
134 
135   Target number of failures = 500,   Success fraction = 50.0%
136   ____________________________
137   Confidence        Min Number
138    Value (%)        Of Trials
139   ____________________________
140       50.000        1001
141       75.000        1023
142       90.000        1043
143       95.000        1055
144       99.000        1078
145       99.900        1104
146       99.990        1126
147       99.999        1146
148 
149 
150   Target number of failures = 50,   Success fraction = 10.0%
151   ____________________________
152   Confidence        Min Number
153    Value (%)        Of Trials
154   ____________________________
155       50.000          56
156       75.000          58
157       90.000          60
158       95.000          61
159       99.000          63
160       99.900          66
161       99.990          68
162       99.999          71
163 
164 
165   Target number of failures = 500,   Success fraction = 10.0%
166   ____________________________
167   Confidence        Min Number
168    Value (%)        Of Trials
169   ____________________________
170       50.000         556
171       75.000         562
172       90.000         567
173       95.000         570
174       99.000         576
175       99.900         583
176       99.990         588
177       99.999         594
178 
179 
180   Target number of failures = 5,   Success fraction = 90.0%
181   ____________________________
182   Confidence        Min Number
183    Value (%)        Of Trials
184   ____________________________
185       50.000          57
186       75.000          73
187       90.000          91
188       95.000         103
189       99.000         127
190       99.900         159
191       99.990         189
192       99.999         217
193 
194 
195   Target number of failures = 5,   Success fraction = 95.0%
196   ____________________________
197   Confidence        Min Number
198    Value (%)        Of Trials
199   ____________________________
200       50.000         114
201       75.000         148
202       90.000         184
203       95.000         208
204       99.000         259
205       99.900         324
206       99.990         384
207       99.999         442
208 
209 */
210