File: neg_binomial_sample_sizes.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (209 lines) | stat: -rw-r--r-- 6,264 bytes parent folder | download | duplicates (19)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// neg_binomial_sample_sizes.cpp

// Copyright John Maddock 2006
// Copyright Paul A. Bristow 2007, 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)

#include <boost/math/distributions/negative_binomial.hpp>
using boost::math::negative_binomial;

// Default RealType is double so this permits use of:
double find_minimum_number_of_trials(
double k,     // number of failures (events), k >= 0.
double p,     // fraction of trails for which event occurs, 0 <= p <= 1.
double probability); // probability threshold, 0 <= probability <= 1.

#include <iostream>
using std::cout;
using std::endl;
using std::fixed;
using std::right;
#include <iomanip>
using std::setprecision;
using std::setw; 

//[neg_binomial_sample_sizes

/*`
It centres around a routine that prints out a table of 
minimum sample sizes (number of trials) for various probability thresholds:
*/
  void find_number_of_trials(double failures, double p);
/*`
First define a table of significance levels: these are the maximum 
acceptable probability that /failure/ or fewer events will be observed.
*/
  double alpha[] = { 0.5, 0.25, 0.1, 0.05, 0.01, 0.001, 0.0001, 0.00001 };
/*`
Confidence value as % is (1 - alpha) * 100, so alpha 0.05 == 95% confidence
that the desired number of failures will be observed.
The values range from a very low 0.5 or 50% confidence up to an extremely high
confidence of 99.999.

Much of the rest of the program is pretty-printing, the important part
is in the calculation of minimum number of trials required for each
value of alpha using:

  (int)ceil(negative_binomial::find_minimum_number_of_trials(failures, p, alpha[i]);

find_minimum_number_of_trials returns a double,
so `ceil` rounds this up to ensure we have an integral minimum number of trials.
*/
  
void find_number_of_trials(double failures, double p)
{
   // trials = number of trials
   // failures = number of failures before achieving required success(es).
   // p        = success fraction (0 <= p <= 1.).
   //
   // Calculate how many trials we need to ensure the
   // required number of failures DOES exceed "failures".

  cout << "\n""Target number of failures = " << (int)failures;
  cout << ",   Success fraction = " << fixed << setprecision(1) << 100 * p << "%" << endl;
   // Print table header:
   cout << "____________________________\n"
           "Confidence        Min Number\n"
           " Value (%)        Of Trials \n"
           "____________________________\n";
   // Now print out the data for the alpha table values.
  for(unsigned i = 0; i < sizeof(alpha)/sizeof(alpha[0]); ++i)
   { // Confidence values %:
      cout << fixed << setprecision(3) << setw(10) << right << 100 * (1-alpha[i]) << "      "
      // find_minimum_number_of_trials
      << setw(6) << right
      << (int)ceil(negative_binomial::find_minimum_number_of_trials(failures, p, alpha[i]))
      << endl;
   }
   cout << endl;
} // void find_number_of_trials(double failures, double p)

/*` finally we can produce some tables of minimum trials for the chosen confidence levels:
*/

int main()
{
    find_number_of_trials(5, 0.5);
    find_number_of_trials(50, 0.5);
    find_number_of_trials(500, 0.5);
    find_number_of_trials(50, 0.1);
    find_number_of_trials(500, 0.1);
    find_number_of_trials(5, 0.9);

    return 0;
} // int main()

//]  [/neg_binomial_sample_sizes.cpp end of Quickbook in C++ markup]

/*

Output is:
Target number of failures = 5,   Success fraction = 50.0%
  ____________________________
  Confidence        Min Number
   Value (%)        Of Trials 
  ____________________________
      50.000          11
      75.000          14
      90.000          17
      95.000          18
      99.000          22
      99.900          27
      99.990          31
      99.999          36
  
  
  Target number of failures = 50,   Success fraction = 50.0%
  ____________________________
  Confidence        Min Number
   Value (%)        Of Trials 
  ____________________________
      50.000         101
      75.000         109
      90.000         115
      95.000         119
      99.000         128
      99.900         137
      99.990         146
      99.999         154
  
  
  Target number of failures = 500,   Success fraction = 50.0%
  ____________________________
  Confidence        Min Number
   Value (%)        Of Trials 
  ____________________________
      50.000        1001
      75.000        1023
      90.000        1043
      95.000        1055
      99.000        1078
      99.900        1104
      99.990        1126
      99.999        1146
  
  
  Target number of failures = 50,   Success fraction = 10.0%
  ____________________________
  Confidence        Min Number
   Value (%)        Of Trials 
  ____________________________
      50.000          56
      75.000          58
      90.000          60
      95.000          61
      99.000          63
      99.900          66
      99.990          68
      99.999          71
  
  
  Target number of failures = 500,   Success fraction = 10.0%
  ____________________________
  Confidence        Min Number
   Value (%)        Of Trials 
  ____________________________
      50.000         556
      75.000         562
      90.000         567
      95.000         570
      99.000         576
      99.900         583
      99.990         588
      99.999         594
  
  
  Target number of failures = 5,   Success fraction = 90.0%
  ____________________________
  Confidence        Min Number
   Value (%)        Of Trials 
  ____________________________
      50.000          57
      75.000          73
      90.000          91
      95.000         103
      99.000         127
      99.900         159
      99.990         189
      99.999         217
  
  
  Target number of failures = 5,   Success fraction = 95.0%
  ____________________________
  Confidence        Min Number
   Value (%)        Of Trials 
  ____________________________
      50.000         114
      75.000         148
      90.000         184
      95.000         208
      99.000         259
      99.900         324
      99.990         384
      99.999         442

*/