3 <meta http-equiv=
"Content-Type" content=
"text/html; charset=US-ASCII">
4 <title>Estimating Sample Sizes for the Negative Binomial.
</title>
5 <link rel=
"stylesheet" href=
"../../../../math.css" type=
"text/css">
6 <meta name=
"generator" content=
"DocBook XSL Stylesheets V1.77.1">
7 <link rel=
"home" href=
"../../../../index.html" title=
"Math Toolkit 2.5.1">
8 <link rel=
"up" href=
"../neg_binom_eg.html" title=
"Negative Binomial Distribution Examples">
9 <link rel=
"prev" href=
"neg_binom_conf.html" title=
"Calculating Confidence Limits on the Frequency of Occurrence for the Negative Binomial Distribution">
10 <link rel=
"next" href=
"negative_binomial_example1.html" title=
"Negative Binomial Sales Quota Example.">
12 <body bgcolor=
"white" text=
"black" link=
"#0000FF" vlink=
"#840084" alink=
"#0000FF">
13 <table cellpadding=
"2" width=
"100%"><tr>
14 <td valign=
"top"><img alt=
"Boost C++ Libraries" width=
"277" height=
"86" src=
"../../../../../../../../boost.png"></td>
15 <td align=
"center"><a href=
"../../../../../../../../index.html">Home
</a></td>
16 <td align=
"center"><a href=
"../../../../../../../../libs/libraries.htm">Libraries
</a></td>
17 <td align=
"center"><a href=
"http://www.boost.org/users/people.html">People
</a></td>
18 <td align=
"center"><a href=
"http://www.boost.org/users/faq.html">FAQ
</a></td>
19 <td align=
"center"><a href=
"../../../../../../../../more/index.htm">More
</a></td>
22 <div class=
"spirit-nav">
23 <a accesskey=
"p" href=
"neg_binom_conf.html"><img src=
"../../../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../neg_binom_eg.html"><img src=
"../../../../../../../../doc/src/images/up.png" alt=
"Up"></a><a accesskey=
"h" href=
"../../../../index.html"><img src=
"../../../../../../../../doc/src/images/home.png" alt=
"Home"></a><a accesskey=
"n" href=
"negative_binomial_example1.html"><img src=
"../../../../../../../../doc/src/images/next.png" alt=
"Next"></a>
26 <div class=
"titlepage"><div><div><h5 class=
"title">
27 <a name=
"math_toolkit.stat_tut.weg.neg_binom_eg.neg_binom_size_eg"></a><a class=
"link" href=
"neg_binom_size_eg.html" title=
"Estimating Sample Sizes for the Negative Binomial.">Estimating
28 Sample Sizes for the Negative Binomial.
</a>
29 </h5></div></div></div>
31 Imagine you have an event (let's call it a
"failure" - though
32 we could equally well call it a success if we felt it was a 'good' event)
33 that you know will occur in
1 in N trials. You may want to know how many
34 trials you need to conduct to be P% sure of observing at least k such
35 failures. If the failure events follow a negative binomial distribution
36 (each trial either succeeds or fails) then the static member function
37 <code class=
"computeroutput"><span class=
"identifier">negative_binomial_distibution
</span><span class=
"special"><>::
</span><span class=
"identifier">find_minimum_number_of_trials
</span></code>
38 can be used to estimate the minimum number of trials required to be P%
39 sure of observing the desired number of failures.
42 The example program
<a href=
"../../../../../../example/neg_binomial_sample_sizes.cpp" target=
"_top">neg_binomial_sample_sizes.cpp
</a>
43 demonstrates its usage.
46 It centres around a routine that prints out a table of minimum sample
47 sizes (number of trials) for various probability thresholds:
49 <pre class=
"programlisting"><span class=
"keyword">void
</span> <span class=
"identifier">find_number_of_trials
</span><span class=
"special">(
</span><span class=
"keyword">double
</span> <span class=
"identifier">failures
</span><span class=
"special">,
</span> <span class=
"keyword">double
</span> <span class=
"identifier">p
</span><span class=
"special">);
</span>
52 First define a table of significance levels: these are the maximum acceptable
53 probability that
<span class=
"emphasis"><em>failure
</em></span> or fewer events will be
56 <pre class=
"programlisting"><span class=
"keyword">double
</span> <span class=
"identifier">alpha
</span><span class=
"special">[]
</span> <span class=
"special">=
</span> <span class=
"special">{
</span> <span class=
"number">0.5</span><span class=
"special">,
</span> <span class=
"number">0.25</span><span class=
"special">,
</span> <span class=
"number">0.1</span><span class=
"special">,
</span> <span class=
"number">0.05</span><span class=
"special">,
</span> <span class=
"number">0.01</span><span class=
"special">,
</span> <span class=
"number">0.001</span><span class=
"special">,
</span> <span class=
"number">0.0001</span><span class=
"special">,
</span> <span class=
"number">0.00001</span> <span class=
"special">};
</span>
59 Confidence value as % is (
1 - alpha) *
100, so alpha
0.05 ==
95% confidence
60 that the desired number of failures will be observed. The values range
61 from a very low
0.5 or
50% confidence up to an extremely high confidence
65 Much of the rest of the program is pretty-printing, the important part
66 is in the calculation of minimum number of trials required for each value
69 <pre class=
"programlisting"><span class=
"special">(
</span><span class=
"keyword">int
</span><span class=
"special">)
</span><span class=
"identifier">ceil
</span><span class=
"special">(
</span><span class=
"identifier">negative_binomial
</span><span class=
"special">::
</span><span class=
"identifier">find_minimum_number_of_trials
</span><span class=
"special">(
</span><span class=
"identifier">failures
</span><span class=
"special">,
</span> <span class=
"identifier">p
</span><span class=
"special">,
</span> <span class=
"identifier">alpha
</span><span class=
"special">[
</span><span class=
"identifier">i
</span><span class=
"special">]);
</span>
72 find_minimum_number_of_trials returns a double, so
<code class=
"computeroutput"><span class=
"identifier">ceil
</span></code>
73 rounds this up to ensure we have an integral minimum number of trials.
75 <pre class=
"programlisting"><span class=
"keyword">void
</span> <span class=
"identifier">find_number_of_trials
</span><span class=
"special">(
</span><span class=
"keyword">double
</span> <span class=
"identifier">failures
</span><span class=
"special">,
</span> <span class=
"keyword">double
</span> <span class=
"identifier">p
</span><span class=
"special">)
</span>
76 <span class=
"special">{
</span>
77 <span class=
"comment">// trials = number of trials
</span>
78 <span class=
"comment">// failures = number of failures before achieving required success(es).
</span>
79 <span class=
"comment">// p = success fraction (
0 <= p
<=
1.).
</span>
80 <span class=
"comment">//
</span>
81 <span class=
"comment">// Calculate how many trials we need to ensure the
</span>
82 <span class=
"comment">// required number of failures DOES exceed
"failures".
</span>
84 <span class=
"identifier">cout
</span> <span class=
"special"><<</span> <span class=
"string">"\n"</span><span class=
"string">"Target number of failures = "</span> <span class=
"special"><<</span> <span class=
"special">(
</span><span class=
"keyword">int
</span><span class=
"special">)
</span><span class=
"identifier">failures
</span><span class=
"special">;
</span>
85 <span class=
"identifier">cout
</span> <span class=
"special"><<</span> <span class=
"string">", Success fraction = "</span> <span class=
"special"><<</span> <span class=
"identifier">fixed
</span> <span class=
"special"><<</span> <span class=
"identifier">setprecision
</span><span class=
"special">(
</span><span class=
"number">1</span><span class=
"special">)
</span> <span class=
"special"><<</span> <span class=
"number">100</span> <span class=
"special">*
</span> <span class=
"identifier">p
</span> <span class=
"special"><<</span> <span class=
"string">"%"</span> <span class=
"special"><<</span> <span class=
"identifier">endl
</span><span class=
"special">;
</span>
86 <span class=
"comment">// Print table header:
</span>
87 <span class=
"identifier">cout
</span> <span class=
"special"><<</span> <span class=
"string">"____________________________\n"</span>
88 <span class=
"string">"Confidence Min Number\n"</span>
89 <span class=
"string">" Value (%) Of Trials \n"</span>
90 <span class=
"string">"____________________________\n"</span><span class=
"special">;
</span>
91 <span class=
"comment">// Now print out the data for the alpha table values.
</span>
92 <span class=
"keyword">for
</span><span class=
"special">(
</span><span class=
"keyword">unsigned
</span> <span class=
"identifier">i
</span> <span class=
"special">=
</span> <span class=
"number">0</span><span class=
"special">;
</span> <span class=
"identifier">i
</span> <span class=
"special"><</span> <span class=
"keyword">sizeof
</span><span class=
"special">(
</span><span class=
"identifier">alpha
</span><span class=
"special">)/
</span><span class=
"keyword">sizeof
</span><span class=
"special">(
</span><span class=
"identifier">alpha
</span><span class=
"special">[
</span><span class=
"number">0</span><span class=
"special">]);
</span> <span class=
"special">++
</span><span class=
"identifier">i
</span><span class=
"special">)
</span>
93 <span class=
"special">{
</span> <span class=
"comment">// Confidence values %:
</span>
94 <span class=
"identifier">cout
</span> <span class=
"special"><<</span> <span class=
"identifier">fixed
</span> <span class=
"special"><<</span> <span class=
"identifier">setprecision
</span><span class=
"special">(
</span><span class=
"number">3</span><span class=
"special">)
</span> <span class=
"special"><<</span> <span class=
"identifier">setw
</span><span class=
"special">(
</span><span class=
"number">10</span><span class=
"special">)
</span> <span class=
"special"><<</span> <span class=
"identifier">right
</span> <span class=
"special"><<</span> <span class=
"number">100</span> <span class=
"special">*
</span> <span class=
"special">(
</span><span class=
"number">1</span><span class=
"special">-
</span><span class=
"identifier">alpha
</span><span class=
"special">[
</span><span class=
"identifier">i
</span><span class=
"special">])
</span> <span class=
"special"><<</span> <span class=
"string">" "</span>
95 <span class=
"comment">// find_minimum_number_of_trials
</span>
96 <span class=
"special"><<</span> <span class=
"identifier">setw
</span><span class=
"special">(
</span><span class=
"number">6</span><span class=
"special">)
</span> <span class=
"special"><<</span> <span class=
"identifier">right
</span>
97 <span class=
"special"><<</span> <span class=
"special">(
</span><span class=
"keyword">int
</span><span class=
"special">)
</span><span class=
"identifier">ceil
</span><span class=
"special">(
</span><span class=
"identifier">negative_binomial
</span><span class=
"special">::
</span><span class=
"identifier">find_minimum_number_of_trials
</span><span class=
"special">(
</span><span class=
"identifier">failures
</span><span class=
"special">,
</span> <span class=
"identifier">p
</span><span class=
"special">,
</span> <span class=
"identifier">alpha
</span><span class=
"special">[
</span><span class=
"identifier">i
</span><span class=
"special">]))
</span>
98 <span class=
"special"><<</span> <span class=
"identifier">endl
</span><span class=
"special">;
</span>
99 <span class=
"special">}
</span>
100 <span class=
"identifier">cout
</span> <span class=
"special"><<</span> <span class=
"identifier">endl
</span><span class=
"special">;
</span>
101 <span class=
"special">}
</span> <span class=
"comment">// void find_number_of_trials(double failures, double p)
</span>
104 finally we can produce some tables of minimum trials for the chosen confidence
107 <pre class=
"programlisting"><span class=
"keyword">int
</span> <span class=
"identifier">main
</span><span class=
"special">()
</span>
108 <span class=
"special">{
</span>
109 <span class=
"identifier">find_number_of_trials
</span><span class=
"special">(
</span><span class=
"number">5</span><span class=
"special">,
</span> <span class=
"number">0.5</span><span class=
"special">);
</span>
110 <span class=
"identifier">find_number_of_trials
</span><span class=
"special">(
</span><span class=
"number">50</span><span class=
"special">,
</span> <span class=
"number">0.5</span><span class=
"special">);
</span>
111 <span class=
"identifier">find_number_of_trials
</span><span class=
"special">(
</span><span class=
"number">500</span><span class=
"special">,
</span> <span class=
"number">0.5</span><span class=
"special">);
</span>
112 <span class=
"identifier">find_number_of_trials
</span><span class=
"special">(
</span><span class=
"number">50</span><span class=
"special">,
</span> <span class=
"number">0.1</span><span class=
"special">);
</span>
113 <span class=
"identifier">find_number_of_trials
</span><span class=
"special">(
</span><span class=
"number">500</span><span class=
"special">,
</span> <span class=
"number">0.1</span><span class=
"special">);
</span>
114 <span class=
"identifier">find_number_of_trials
</span><span class=
"special">(
</span><span class=
"number">5</span><span class=
"special">,
</span> <span class=
"number">0.9</span><span class=
"special">);
</span>
116 <span class=
"keyword">return
</span> <span class=
"number">0</span><span class=
"special">;
</span>
117 <span class=
"special">}
</span> <span class=
"comment">// int main()
</span>
119 <div class=
"note"><table border=
"0" summary=
"Note">
121 <td rowspan=
"2" align=
"center" valign=
"top" width=
"25"><img alt=
"[Note]" src=
"../../../../../../../../doc/src/images/note.png"></td>
122 <th align=
"left">Note
</th>
124 <tr><td align=
"left" valign=
"top">
126 Since we're calculating the
<span class=
"emphasis"><em>minimum
</em></span> number of
127 trials required, we'll err on the safe side and take the ceiling of
128 the result. Had we been calculating the
<span class=
"emphasis"><em>maximum
</em></span>
129 number of trials permitted to observe less than a certain number of
130 <span class=
"emphasis"><em>failures
</em></span> then we would have taken the floor instead.
131 We would also have called
<code class=
"computeroutput"><span class=
"identifier">find_minimum_number_of_trials
</span></code>
134 <pre class=
"programlisting"><span class=
"identifier">floor
</span><span class=
"special">(
</span><span class=
"identifier">negative_binomial
</span><span class=
"special">::
</span><span class=
"identifier">find_minimum_number_of_trials
</span><span class=
"special">(
</span><span class=
"identifier">failures
</span><span class=
"special">,
</span> <span class=
"identifier">p
</span><span class=
"special">,
</span> <span class=
"identifier">alpha
</span><span class=
"special">[
</span><span class=
"identifier">i
</span><span class=
"special">]))
</span>
137 which would give us the largest number of trials we could conduct and
138 still be P% sure of observing
<span class=
"emphasis"><em>failures or less
</em></span>
139 failure events, when the probability of success is
<span class=
"emphasis"><em>p
</em></span>.
144 We'll finish off by looking at some sample output, firstly suppose we
145 wish to observe at least
5 "failures" with a
50/
50 (
0.5) chance
146 of success or failure:
148 <pre class=
"programlisting">Target number of failures =
5, Success fraction =
50%
150 ____________________________
151 Confidence Min Number
153 ____________________________
165 So
18 trials or more would yield a
95% chance that at least our
5 required
166 failures would be observed.
169 Compare that to what happens if the success ratio is
90%:
171 <pre class=
"programlisting">Target number of failures =
5.000, Success fraction =
90.000%
173 ____________________________
174 Confidence Min Number
176 ____________________________
187 So now
103 trials are required to observe at least
5 failures with
95%
191 <table xmlns:
rev=
"http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width=
"100%"><tr>
192 <td align=
"left"></td>
193 <td align=
"right"><div class=
"copyright-footer">Copyright
© 2006-
2010,
2012-
2014 Nikhar Agrawal,
194 Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert
195 Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Johan R
åde, Gautam Sewani,
196 Benjamin Sobotta, Thijs van den Berg, Daryle Walker and Xiaogang Zhang
<p>
197 Distributed under the Boost Software License, Version
1.0. (See accompanying
198 file LICENSE_1_0.txt or copy at
<a href=
"http://www.boost.org/LICENSE_1_0.txt" target=
"_top">http://www.boost.org/LICENSE_1_0.txt
</a>)
203 <div class=
"spirit-nav">
204 <a accesskey=
"p" href=
"neg_binom_conf.html"><img src=
"../../../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../neg_binom_eg.html"><img src=
"../../../../../../../../doc/src/images/up.png" alt=
"Up"></a><a accesskey=
"h" href=
"../../../../index.html"><img src=
"../../../../../../../../doc/src/images/home.png" alt=
"Home"></a><a accesskey=
"n" href=
"negative_binomial_example1.html"><img src=
"../../../../../../../../doc/src/images/next.png" alt=
"Next"></a>