3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Binomial Distribution</title>
5 <link rel="stylesheet" href="../../../math.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7 <link rel="home" href="../../../index.html" title="Math Toolkit 2.11.0">
8 <link rel="up" href="../dists.html" title="Distributions">
9 <link rel="prev" href="beta_dist.html" title="Beta Distribution">
10 <link rel="next" href="cauchy_dist.html" title="Cauchy-Lorentz Distribution">
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="beta_dist.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../dists.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="cauchy_dist.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
26 <div class="titlepage"><div><div><h4 class="title">
27 <a name="math_toolkit.dist_ref.dists.binomial_dist"></a><a class="link" href="binomial_dist.html" title="Binomial Distribution">Binomial
29 </h4></div></div></div>
30 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">distributions</span><span class="special">/</span><span class="identifier">binomial</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></pre>
31 <pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">math</span><span class="special">{</span>
33 <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">RealType</span> <span class="special">=</span> <span class="keyword">double</span><span class="special">,</span>
34 <span class="keyword">class</span> <a class="link" href="../../../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a> <span class="special">=</span> <a class="link" href="../../pol_ref/pol_ref_ref.html" title="Policy Class Reference">policies::policy<></a> <span class="special">></span>
35 <span class="keyword">class</span> <span class="identifier">binomial_distribution</span><span class="special">;</span>
37 <span class="keyword">typedef</span> <span class="identifier">binomial_distribution</span><span class="special"><></span> <span class="identifier">binomial</span><span class="special">;</span>
39 <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">RealType</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../../policy.html" title="Chapter 20. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">></span>
40 <span class="keyword">class</span> <span class="identifier">binomial_distribution</span>
41 <span class="special">{</span>
42 <span class="keyword">public</span><span class="special">:</span>
43 <span class="keyword">typedef</span> <span class="identifier">RealType</span> <span class="identifier">value_type</span><span class="special">;</span>
44 <span class="keyword">typedef</span> <span class="identifier">Policy</span> <span class="identifier">policy_type</span><span class="special">;</span>
46 <span class="keyword">static</span> <span class="keyword">const</span> <span class="emphasis"><em>unspecified-type</em></span> <span class="identifier">clopper_pearson_exact_interval</span><span class="special">;</span>
47 <span class="keyword">static</span> <span class="keyword">const</span> <span class="emphasis"><em>unspecified-type</em></span> <span class="identifier">jeffreys_prior_interval</span><span class="special">;</span>
49 <span class="comment">// construct:</span>
50 <span class="identifier">binomial_distribution</span><span class="special">(</span><span class="identifier">RealType</span> <span class="identifier">n</span><span class="special">,</span> <span class="identifier">RealType</span> <span class="identifier">p</span><span class="special">);</span>
52 <span class="comment">// parameter access::</span>
53 <span class="identifier">RealType</span> <span class="identifier">success_fraction</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
54 <span class="identifier">RealType</span> <span class="identifier">trials</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
56 <span class="comment">// Bounds on success fraction:</span>
57 <span class="keyword">static</span> <span class="identifier">RealType</span> <span class="identifier">find_lower_bound_on_p</span><span class="special">(</span>
58 <span class="identifier">RealType</span> <span class="identifier">trials</span><span class="special">,</span>
59 <span class="identifier">RealType</span> <span class="identifier">successes</span><span class="special">,</span>
60 <span class="identifier">RealType</span> <span class="identifier">probability</span><span class="special">,</span>
61 <span class="emphasis"><em>unspecified-type</em></span> <span class="identifier">method</span> <span class="special">=</span> <span class="identifier">clopper_pearson_exact_interval</span><span class="special">);</span>
62 <span class="keyword">static</span> <span class="identifier">RealType</span> <span class="identifier">find_upper_bound_on_p</span><span class="special">(</span>
63 <span class="identifier">RealType</span> <span class="identifier">trials</span><span class="special">,</span>
64 <span class="identifier">RealType</span> <span class="identifier">successes</span><span class="special">,</span>
65 <span class="identifier">RealType</span> <span class="identifier">probability</span><span class="special">,</span>
66 <span class="emphasis"><em>unspecified-type</em></span> <span class="identifier">method</span> <span class="special">=</span> <span class="identifier">clopper_pearson_exact_interval</span><span class="special">);</span>
68 <span class="comment">// estimate min/max number of trials:</span>
69 <span class="keyword">static</span> <span class="identifier">RealType</span> <span class="identifier">find_minimum_number_of_trials</span><span class="special">(</span>
70 <span class="identifier">RealType</span> <span class="identifier">k</span><span class="special">,</span> <span class="comment">// number of events</span>
71 <span class="identifier">RealType</span> <span class="identifier">p</span><span class="special">,</span> <span class="comment">// success fraction</span>
72 <span class="identifier">RealType</span> <span class="identifier">alpha</span><span class="special">);</span> <span class="comment">// risk level</span>
74 <span class="keyword">static</span> <span class="identifier">RealType</span> <span class="identifier">find_maximum_number_of_trials</span><span class="special">(</span>
75 <span class="identifier">RealType</span> <span class="identifier">k</span><span class="special">,</span> <span class="comment">// number of events</span>
76 <span class="identifier">RealType</span> <span class="identifier">p</span><span class="special">,</span> <span class="comment">// success fraction</span>
77 <span class="identifier">RealType</span> <span class="identifier">alpha</span><span class="special">);</span> <span class="comment">// risk level</span>
78 <span class="special">};</span>
80 <span class="special">}}</span> <span class="comment">// namespaces</span>
83 The class type <code class="computeroutput"><span class="identifier">binomial_distribution</span></code>
84 represents a <a href="http://mathworld.wolfram.com/BinomialDistribution.html" target="_top">binomial
85 distribution</a>: it is used when there are exactly two mutually exclusive
86 outcomes of a trial. These outcomes are labelled "success" and
87 "failure". The <a class="link" href="binomial_dist.html" title="Binomial Distribution">Binomial
88 Distribution</a> is used to obtain the probability of observing k successes
89 in N trials, with the probability of success on a single trial denoted
90 by p. The binomial distribution assumes that p is fixed for all trials.
92 <div class="note"><table border="0" summary="Note">
94 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td>
95 <th align="left">Note</th>
97 <tr><td align="left" valign="top"><p>
98 The random variable for the binomial distribution is the number of successes,
99 (the number of trials is a fixed property of the distribution) whereas
100 for the negative binomial, the random variable is the number of trials,
101 for a fixed number of successes.
105 The PDF for the binomial distribution is given by:
107 <div class="blockquote"><blockquote class="blockquote"><p>
108 <span class="inlinemediaobject"><img src="../../../../equations/binomial_ref2.svg"></span>
110 </p></blockquote></div>
112 The following two graphs illustrate how the PDF changes depending upon
113 the distributions parameters, first we'll keep the success fraction <span class="emphasis"><em>p</em></span>
114 fixed at 0.5, and vary the sample size:
116 <div class="blockquote"><blockquote class="blockquote"><p>
117 <span class="inlinemediaobject"><img src="../../../../graphs/binomial_pdf_1.svg" align="middle"></span>
119 </p></blockquote></div>
121 Alternatively, we can keep the sample size fixed at N=20 and vary the success
122 fraction <span class="emphasis"><em>p</em></span>:
124 <div class="blockquote"><blockquote class="blockquote"><p>
125 <span class="inlinemediaobject"><img src="../../../../graphs/binomial_pdf_2.svg" align="middle"></span>
127 </p></blockquote></div>
128 <div class="caution"><table border="0" summary="Caution">
130 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Caution]" src="../../../../../../../doc/src/images/caution.png"></td>
131 <th align="left">Caution</th>
133 <tr><td align="left" valign="top">
135 The Binomial distribution is a discrete distribution: internally, functions
136 like the <code class="computeroutput"><span class="identifier">cdf</span></code> and <code class="computeroutput"><span class="identifier">pdf</span></code> are treated "as if" they
137 are continuous functions, but in reality the results returned from these
138 functions only have meaning if an integer value is provided for the random
142 The quantile function will by default return an integer result that has
143 been <span class="emphasis"><em>rounded outwards</em></span>. That is to say lower quantiles
144 (where the probability is less than 0.5) are rounded downward, and upper
145 quantiles (where the probability is greater than 0.5) are rounded upwards.
146 This behaviour ensures that if an X% quantile is requested, then <span class="emphasis"><em>at
147 least</em></span> the requested coverage will be present in the central
148 region, and <span class="emphasis"><em>no more than</em></span> the requested coverage
149 will be present in the tails.
152 This behaviour can be changed so that the quantile functions are rounded
153 differently, or even return a real-valued result using <a class="link" href="../../pol_overview.html" title="Policy Overview">Policies</a>.
154 It is strongly recommended that you read the tutorial <a class="link" href="../../pol_tutorial/understand_dis_quant.html" title="Understanding Quantiles of Discrete Distributions">Understanding
155 Quantiles of Discrete Distributions</a> before using the quantile
156 function on the Binomial distribution. The <a class="link" href="../../pol_ref/discrete_quant_ref.html" title="Discrete Quantile Policies">reference
157 docs</a> describe how to change the rounding policy for these distributions.
162 <a name="math_toolkit.dist_ref.dists.binomial_dist.h0"></a>
163 <span class="phrase"><a name="math_toolkit.dist_ref.dists.binomial_dist.member_functions"></a></span><a class="link" href="binomial_dist.html#math_toolkit.dist_ref.dists.binomial_dist.member_functions">Member
167 <a name="math_toolkit.dist_ref.dists.binomial_dist.h1"></a>
168 <span class="phrase"><a name="math_toolkit.dist_ref.dists.binomial_dist.construct"></a></span><a class="link" href="binomial_dist.html#math_toolkit.dist_ref.dists.binomial_dist.construct">Construct</a>
170 <pre class="programlisting"><span class="identifier">binomial_distribution</span><span class="special">(</span><span class="identifier">RealType</span> <span class="identifier">n</span><span class="special">,</span> <span class="identifier">RealType</span> <span class="identifier">p</span><span class="special">);</span>
173 Constructor: <span class="emphasis"><em>n</em></span> is the total number of trials, <span class="emphasis"><em>p</em></span>
174 is the probability of success of a single trial.
177 Requires <code class="computeroutput"><span class="number">0</span> <span class="special"><=</span>
178 <span class="identifier">p</span> <span class="special"><=</span>
179 <span class="number">1</span></code>, and <code class="computeroutput"><span class="identifier">n</span>
180 <span class="special">>=</span> <span class="number">0</span></code>,
181 otherwise calls <a class="link" href="../../error_handling.html#math_toolkit.error_handling.domain_error">domain_error</a>.
184 <a name="math_toolkit.dist_ref.dists.binomial_dist.h2"></a>
185 <span class="phrase"><a name="math_toolkit.dist_ref.dists.binomial_dist.accessors"></a></span><a class="link" href="binomial_dist.html#math_toolkit.dist_ref.dists.binomial_dist.accessors">Accessors</a>
187 <pre class="programlisting"><span class="identifier">RealType</span> <span class="identifier">success_fraction</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
190 Returns the parameter <span class="emphasis"><em>p</em></span> from which this distribution
193 <pre class="programlisting"><span class="identifier">RealType</span> <span class="identifier">trials</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
196 Returns the parameter <span class="emphasis"><em>n</em></span> from which this distribution
200 <a name="math_toolkit.dist_ref.dists.binomial_dist.h3"></a>
201 <span class="phrase"><a name="math_toolkit.dist_ref.dists.binomial_dist.lower_bound_on_the_success_fract"></a></span><a class="link" href="binomial_dist.html#math_toolkit.dist_ref.dists.binomial_dist.lower_bound_on_the_success_fract">Lower
202 Bound on the Success Fraction</a>
204 <pre class="programlisting"><span class="keyword">static</span> <span class="identifier">RealType</span> <span class="identifier">find_lower_bound_on_p</span><span class="special">(</span>
205 <span class="identifier">RealType</span> <span class="identifier">trials</span><span class="special">,</span>
206 <span class="identifier">RealType</span> <span class="identifier">successes</span><span class="special">,</span>
207 <span class="identifier">RealType</span> <span class="identifier">alpha</span><span class="special">,</span>
208 <span class="emphasis"><em>unspecified-type</em></span> <span class="identifier">method</span> <span class="special">=</span> <span class="identifier">clopper_pearson_exact_interval</span><span class="special">);</span>
211 Returns a lower bound on the success fraction:
213 <div class="variablelist">
214 <p class="title"><b></b></p>
215 <dl class="variablelist">
216 <dt><span class="term">trials</span></dt>
218 The total number of trials conducted.
220 <dt><span class="term">successes</span></dt>
222 The number of successes that occurred.
224 <dt><span class="term">alpha</span></dt>
226 The largest acceptable probability that the true value of the success
227 fraction is <span class="bold"><strong>less than</strong></span> the value
230 <dt><span class="term">method</span></dt>
232 An optional parameter that specifies the method to be used to compute
233 the interval (See below).
238 For example, if you observe <span class="emphasis"><em>k</em></span> successes from <span class="emphasis"><em>n</em></span>
239 trials the best estimate for the success fraction is simply <span class="emphasis"><em>k/n</em></span>,
240 but if you want to be 95% sure that the true value is <span class="bold"><strong>greater
241 than</strong></span> some value, <span class="emphasis"><em>p<sub>min</sub></em></span>, then:
243 <pre class="programlisting"><span class="identifier">p</span><sub>min</sub> <span class="special">=</span> <span class="identifier">binomial_distribution</span><span class="special"><</span><span class="identifier">RealType</span><span class="special">>::</span><span class="identifier">find_lower_bound_on_p</span><span class="special">(</span><span class="identifier">n</span><span class="special">,</span> <span class="identifier">k</span><span class="special">,</span> <span class="number">0.05</span><span class="special">);</span>
246 <a class="link" href="../../stat_tut/weg/binom_eg/binom_conf.html" title="Calculating Confidence Limits on the Frequency of Occurrence for a Binomial Distribution">See worked
250 There are currently two possible values available for the <span class="emphasis"><em>method</em></span>
251 optional parameter: <span class="emphasis"><em>clopper_pearson_exact_interval</em></span>
252 or <span class="emphasis"><em>jeffreys_prior_interval</em></span>. These constants are both
253 members of class template <code class="computeroutput"><span class="identifier">binomial_distribution</span></code>,
254 so usage is for example:
256 <pre class="programlisting"><span class="identifier">p</span> <span class="special">=</span> <span class="identifier">binomial_distribution</span><span class="special"><</span><span class="identifier">RealType</span><span class="special">>::</span><span class="identifier">find_lower_bound_on_p</span><span class="special">(</span>
257 <span class="identifier">n</span><span class="special">,</span> <span class="identifier">k</span><span class="special">,</span> <span class="number">0.05</span><span class="special">,</span> <span class="identifier">binomial_distribution</span><span class="special"><</span><span class="identifier">RealType</span><span class="special">>::</span><span class="identifier">jeffreys_prior_interval</span><span class="special">);</span>
260 The default method if this parameter is not specified is the Clopper Pearson
261 "exact" interval. This produces an interval that guarantees at
262 least <code class="computeroutput"><span class="number">100</span><span class="special">(</span><span class="number">1</span><span class="special">-</span><span class="identifier">alpha</span><span class="special">)%</span></code> coverage, but which is known to be overly
263 conservative, sometimes producing intervals with much greater than the
267 The alternative calculation method produces a non-informative Jeffreys
268 Prior interval. It produces <code class="computeroutput"><span class="number">100</span><span class="special">(</span><span class="number">1</span><span class="special">-</span><span class="identifier">alpha</span><span class="special">)%</span></code>
269 coverage only <span class="emphasis"><em>in the average case</em></span>, though is typically
270 very close to the requested coverage level. It is one of the main methods
271 of calculation recommended in the review by Brown, Cai and DasGupta.
274 Please note that the "textbook" calculation method using a normal
275 approximation (the Wald interval) is deliberately not provided: it is known
276 to produce consistently poor results, even when the sample size is surprisingly
277 large. Refer to Brown, Cai and DasGupta for a full explanation. Many other
278 methods of calculation are available, and may be more appropriate for specific
279 situations. Unfortunately there appears to be no consensus amongst statisticians
280 as to which is "best": refer to the discussion at the end of
281 Brown, Cai and DasGupta for examples.
284 The two methods provided here were chosen principally because they can
285 be used for both one and two sided intervals. See also:
288 Lawrence D. Brown, T. Tony Cai and Anirban DasGupta (2001), Interval Estimation
289 for a Binomial Proportion, Statistical Science, Vol. 16, No. 2, 101-133.
292 T. Tony Cai (2005), One-sided confidence intervals in discrete distributions,
293 Journal of Statistical Planning and Inference 131, 63-88.
296 Agresti, A. and Coull, B. A. (1998). Approximate is better than "exact"
297 for interval estimation of binomial proportions. Amer. Statist. 52 119-126.
300 Clopper, C. J. and Pearson, E. S. (1934). The use of confidence or fiducial
301 limits illustrated in the case of the binomial. Biometrika 26 404-413.
304 <a name="math_toolkit.dist_ref.dists.binomial_dist.h4"></a>
305 <span class="phrase"><a name="math_toolkit.dist_ref.dists.binomial_dist.upper_bound_on_the_success_fract"></a></span><a class="link" href="binomial_dist.html#math_toolkit.dist_ref.dists.binomial_dist.upper_bound_on_the_success_fract">Upper
306 Bound on the Success Fraction</a>
308 <pre class="programlisting"><span class="keyword">static</span> <span class="identifier">RealType</span> <span class="identifier">find_upper_bound_on_p</span><span class="special">(</span>
309 <span class="identifier">RealType</span> <span class="identifier">trials</span><span class="special">,</span>
310 <span class="identifier">RealType</span> <span class="identifier">successes</span><span class="special">,</span>
311 <span class="identifier">RealType</span> <span class="identifier">alpha</span><span class="special">,</span>
312 <span class="emphasis"><em>unspecified-type</em></span> <span class="identifier">method</span> <span class="special">=</span> <span class="identifier">clopper_pearson_exact_interval</span><span class="special">);</span>
315 Returns an upper bound on the success fraction:
317 <div class="variablelist">
318 <p class="title"><b></b></p>
319 <dl class="variablelist">
320 <dt><span class="term">trials</span></dt>
322 The total number of trials conducted.
324 <dt><span class="term">successes</span></dt>
326 The number of successes that occurred.
328 <dt><span class="term">alpha</span></dt>
330 The largest acceptable probability that the true value of the success
331 fraction is <span class="bold"><strong>greater than</strong></span> the value
334 <dt><span class="term">method</span></dt>
336 An optional parameter that specifies the method to be used to compute
337 the interval. Refer to the documentation for <code class="computeroutput"><span class="identifier">find_upper_bound_on_p</span></code>
338 above for the meaning of the method options.
343 For example, if you observe <span class="emphasis"><em>k</em></span> successes from <span class="emphasis"><em>n</em></span>
344 trials the best estimate for the success fraction is simply <span class="emphasis"><em>k/n</em></span>,
345 but if you want to be 95% sure that the true value is <span class="bold"><strong>less
346 than</strong></span> some value, <span class="emphasis"><em>p<sub>max</sub></em></span>, then:
348 <pre class="programlisting"><span class="identifier">p</span><sub>max</sub> <span class="special">=</span> <span class="identifier">binomial_distribution</span><span class="special"><</span><span class="identifier">RealType</span><span class="special">>::</span><span class="identifier">find_upper_bound_on_p</span><span class="special">(</span><span class="identifier">n</span><span class="special">,</span> <span class="identifier">k</span><span class="special">,</span> <span class="number">0.05</span><span class="special">);</span>
351 <a class="link" href="../../stat_tut/weg/binom_eg/binom_conf.html" title="Calculating Confidence Limits on the Frequency of Occurrence for a Binomial Distribution">See worked
354 <div class="note"><table border="0" summary="Note">
356 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td>
357 <th align="left">Note</th>
359 <tr><td align="left" valign="top">
361 In order to obtain a two sided bound on the success fraction, you call
362 both <code class="computeroutput"><span class="identifier">find_lower_bound_on_p</span></code>
363 <span class="bold"><strong>and</strong></span> <code class="computeroutput"><span class="identifier">find_upper_bound_on_p</span></code>
364 each with the same arguments.
367 If the desired risk level that the true success fraction lies outside
368 the bounds is α, then you pass α/2 to these functions.
371 So for example a two sided 95% confidence interval would be obtained
372 by passing α = 0.025 to each of the functions.
375 <a class="link" href="../../stat_tut/weg/binom_eg/binom_conf.html" title="Calculating Confidence Limits on the Frequency of Occurrence for a Binomial Distribution">See worked
381 <a name="math_toolkit.dist_ref.dists.binomial_dist.h5"></a>
382 <span class="phrase"><a name="math_toolkit.dist_ref.dists.binomial_dist.estimating_the_number_of_trials_"></a></span><a class="link" href="binomial_dist.html#math_toolkit.dist_ref.dists.binomial_dist.estimating_the_number_of_trials_">Estimating
383 the Number of Trials Required for a Certain Number of Successes</a>
385 <pre class="programlisting"><span class="keyword">static</span> <span class="identifier">RealType</span> <span class="identifier">find_minimum_number_of_trials</span><span class="special">(</span>
386 <span class="identifier">RealType</span> <span class="identifier">k</span><span class="special">,</span> <span class="comment">// number of events</span>
387 <span class="identifier">RealType</span> <span class="identifier">p</span><span class="special">,</span> <span class="comment">// success fraction</span>
388 <span class="identifier">RealType</span> <span class="identifier">alpha</span><span class="special">);</span> <span class="comment">// probability threshold</span>
391 This function estimates the minimum number of trials required to ensure
392 that more than k events is observed with a level of risk <span class="emphasis"><em>alpha</em></span>
393 that k or fewer events occur.
395 <div class="variablelist">
396 <p class="title"><b></b></p>
397 <dl class="variablelist">
398 <dt><span class="term">k</span></dt>
400 The number of success observed.
402 <dt><span class="term">p</span></dt>
404 The probability of success for each trial.
406 <dt><span class="term">alpha</span></dt>
408 The maximum acceptable probability that k events or fewer will be
416 <pre class="programlisting"><span class="identifier">binomial_distribution</span><span class="special"><</span><span class="identifier">RealType</span><span class="special">>::</span><span class="identifier">find_number_of_trials</span><span class="special">(</span><span class="number">10</span><span class="special">,</span> <span class="number">0.5</span><span class="special">,</span> <span class="number">0.05</span><span class="special">);</span>
419 Returns the smallest number of trials we must conduct to be 95% sure of
420 seeing 10 events that occur with frequency one half.
423 <a name="math_toolkit.dist_ref.dists.binomial_dist.h6"></a>
424 <span class="phrase"><a name="math_toolkit.dist_ref.dists.binomial_dist.estimating_the_maximum_number_of"></a></span><a class="link" href="binomial_dist.html#math_toolkit.dist_ref.dists.binomial_dist.estimating_the_maximum_number_of">Estimating
425 the Maximum Number of Trials to Ensure no more than a Certain Number of
428 <pre class="programlisting"><span class="keyword">static</span> <span class="identifier">RealType</span> <span class="identifier">find_maximum_number_of_trials</span><span class="special">(</span>
429 <span class="identifier">RealType</span> <span class="identifier">k</span><span class="special">,</span> <span class="comment">// number of events</span>
430 <span class="identifier">RealType</span> <span class="identifier">p</span><span class="special">,</span> <span class="comment">// success fraction</span>
431 <span class="identifier">RealType</span> <span class="identifier">alpha</span><span class="special">);</span> <span class="comment">// probability threshold</span>
434 This function estimates the maximum number of trials we can conduct to
435 ensure that k successes or fewer are observed, with a risk <span class="emphasis"><em>alpha</em></span>
436 that more than k occur.
438 <div class="variablelist">
439 <p class="title"><b></b></p>
440 <dl class="variablelist">
441 <dt><span class="term">k</span></dt>
443 The number of success observed.
445 <dt><span class="term">p</span></dt>
447 The probability of success for each trial.
449 <dt><span class="term">alpha</span></dt>
451 The maximum acceptable probability that more than k events will be
459 <pre class="programlisting"><span class="identifier">binomial_distribution</span><span class="special"><</span><span class="identifier">RealType</span><span class="special">>::</span><span class="identifier">find_maximum_number_of_trials</span><span class="special">(</span><span class="number">0</span><span class="special">,</span> <span class="number">1e-6</span><span class="special">,</span> <span class="number">0.05</span><span class="special">);</span>
462 Returns the largest number of trials we can conduct and still be 95% certain
463 of not observing any events that occur with one in a million frequency.
464 This is typically used in failure analysis.
467 <a class="link" href="../../stat_tut/weg/binom_eg/binom_size_eg.html" title="Estimating Sample Sizes for a Binomial Distribution.">See Worked
471 <a name="math_toolkit.dist_ref.dists.binomial_dist.h7"></a>
472 <span class="phrase"><a name="math_toolkit.dist_ref.dists.binomial_dist.non_member_accessors"></a></span><a class="link" href="binomial_dist.html#math_toolkit.dist_ref.dists.binomial_dist.non_member_accessors">Non-member
476 All the <a class="link" href="../nmp.html" title="Non-Member Properties">usual non-member accessor
477 functions</a> that are generic to all distributions are supported:
478 <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.cdf">Cumulative Distribution Function</a>,
479 <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.pdf">Probability Density Function</a>,
480 <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.quantile">Quantile</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.hazard">Hazard Function</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.chf">Cumulative Hazard Function</a>,
481 <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.mean">mean</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.median">median</a>,
482 <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.mode">mode</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.variance">variance</a>,
483 <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.sd">standard deviation</a>,
484 <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.skewness">skewness</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.kurtosis">kurtosis</a>, <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.kurtosis_excess">kurtosis_excess</a>,
485 <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.range">range</a> and <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.support">support</a>.
488 The domain for the random variable <span class="emphasis"><em>k</em></span> is <code class="computeroutput"><span class="number">0</span> <span class="special"><=</span> <span class="identifier">k</span> <span class="special"><=</span> <span class="identifier">N</span></code>, otherwise a <a class="link" href="../../error_handling.html#math_toolkit.error_handling.domain_error">domain_error</a>
492 It's worth taking a moment to define what these accessors actually mean
493 in the context of this distribution:
496 <a name="math_toolkit.dist_ref.dists.binomial_dist.meaning_of_the_non_member_access"></a><p class="title"><b>Table 5.1. Meaning of the non-member accessors</b></p>
497 <div class="table-contents"><table class="table" summary="Meaning of the non-member accessors">
518 <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.pdf">Probability Density
524 The probability of obtaining <span class="bold"><strong>exactly k
525 successes</strong></span> from n trials with success fraction p. For
529 <code class="computeroutput"><span class="identifier">pdf</span><span class="special">(</span><span class="identifier">binomial</span><span class="special">(</span><span class="identifier">n</span><span class="special">,</span>
530 <span class="identifier">p</span><span class="special">),</span>
531 <span class="identifier">k</span><span class="special">)</span></code>
538 <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.cdf">Cumulative Distribution
544 The probability of obtaining <span class="bold"><strong>k successes
545 or fewer</strong></span> from n trials with success fraction p. For
549 <code class="computeroutput"><span class="identifier">cdf</span><span class="special">(</span><span class="identifier">binomial</span><span class="special">(</span><span class="identifier">n</span><span class="special">,</span>
550 <span class="identifier">p</span><span class="special">),</span>
551 <span class="identifier">k</span><span class="special">)</span></code>
558 <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.ccdf">Complement of
559 the Cumulative Distribution Function</a>
564 The probability of obtaining <span class="bold"><strong>more than
565 k successes</strong></span> from n trials with success fraction p.
569 <code class="computeroutput"><span class="identifier">cdf</span><span class="special">(</span><span class="identifier">complement</span><span class="special">(</span><span class="identifier">binomial</span><span class="special">(</span><span class="identifier">n</span><span class="special">,</span>
570 <span class="identifier">p</span><span class="special">),</span>
571 <span class="identifier">k</span><span class="special">))</span></code>
578 <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.quantile">Quantile</a>
583 Given a binomial distribution with <span class="emphasis"><em>n</em></span> trials,
584 success fraction <span class="emphasis"><em>p</em></span> and probability <span class="emphasis"><em>P</em></span>,
585 finds the largest number of successes <span class="emphasis"><em>k</em></span>
586 whose CDF is less than <span class="emphasis"><em>P</em></span>. It is strongly
587 recommended that you read the tutorial <a class="link" href="../../pol_tutorial/understand_dis_quant.html" title="Understanding Quantiles of Discrete Distributions">Understanding
588 Quantiles of Discrete Distributions</a> before using the quantile
596 <a class="link" href="../nmp.html#math_toolkit.dist_ref.nmp.quantile_c">Quantile
597 from the complement of the probability</a>
602 Given a binomial distribution with <span class="emphasis"><em>n</em></span> trials,
603 success fraction <span class="emphasis"><em>p</em></span> and probability <span class="emphasis"><em>Q</em></span>,
604 finds the smallest number of successes <span class="emphasis"><em>k</em></span>
605 whose CDF is greater than <span class="emphasis"><em>1-Q</em></span>. It is strongly
606 recommended that you read the tutorial <a class="link" href="../../pol_tutorial/understand_dis_quant.html" title="Understanding Quantiles of Discrete Distributions">Understanding
607 Quantiles of Discrete Distributions</a> before using the quantile
615 <br class="table-break"><h5>
616 <a name="math_toolkit.dist_ref.dists.binomial_dist.h8"></a>
617 <span class="phrase"><a name="math_toolkit.dist_ref.dists.binomial_dist.examples"></a></span><a class="link" href="binomial_dist.html#math_toolkit.dist_ref.dists.binomial_dist.examples">Examples</a>
620 Various <a class="link" href="../../stat_tut/weg/binom_eg.html" title="Binomial Distribution Examples">worked examples</a>
621 are available illustrating the use of the binomial distribution.
624 <a name="math_toolkit.dist_ref.dists.binomial_dist.h9"></a>
625 <span class="phrase"><a name="math_toolkit.dist_ref.dists.binomial_dist.accuracy"></a></span><a class="link" href="binomial_dist.html#math_toolkit.dist_ref.dists.binomial_dist.accuracy">Accuracy</a>
628 This distribution is implemented using the incomplete beta functions <a class="link" href="../../sf_beta/ibeta_function.html" title="Incomplete Beta Functions">ibeta</a> and <a class="link" href="../../sf_beta/ibeta_function.html" title="Incomplete Beta Functions">ibetac</a>,
629 please refer to these functions for information on accuracy.
632 <a name="math_toolkit.dist_ref.dists.binomial_dist.h10"></a>
633 <span class="phrase"><a name="math_toolkit.dist_ref.dists.binomial_dist.implementation"></a></span><a class="link" href="binomial_dist.html#math_toolkit.dist_ref.dists.binomial_dist.implementation">Implementation</a>
636 In the following table <span class="emphasis"><em>p</em></span> is the probability that one
637 trial will be successful (the success fraction), <span class="emphasis"><em>n</em></span>
638 is the number of trials, <span class="emphasis"><em>k</em></span> is the number of successes,
639 <span class="emphasis"><em>p</em></span> is the probability and <span class="emphasis"><em>q = 1-p</em></span>.
641 <div class="informaltable"><table class="table">
667 Implementation is in terms of <a class="link" href="../../sf_beta/beta_derivative.html" title="Derivative of the Incomplete Beta Function">ibeta_derivative</a>:
668 if <sub>n</sub>C<sub>k </sub> is the binomial coefficient of a and b, then we have:
670 <div class="blockquote"><blockquote class="blockquote"><p>
671 <span class="inlinemediaobject"><img src="../../../../equations/binomial_ref1.svg"></span>
673 </p></blockquote></div>
675 Which can be evaluated as <code class="computeroutput"><span class="identifier">ibeta_derivative</span><span class="special">(</span><span class="identifier">k</span><span class="special">+</span><span class="number">1</span><span class="special">,</span> <span class="identifier">n</span><span class="special">-</span><span class="identifier">k</span><span class="special">+</span><span class="number">1</span><span class="special">,</span> <span class="identifier">p</span><span class="special">)</span> <span class="special">/</span>
676 <span class="special">(</span><span class="identifier">n</span><span class="special">+</span><span class="number">1</span><span class="special">)</span></code>
679 The function <a class="link" href="../../sf_beta/beta_derivative.html" title="Derivative of the Incomplete Beta Function">ibeta_derivative</a>
680 is used here, since it has already been optimised for the lowest
681 possible error - indeed this is really just a thin wrapper around
682 part of the internals of the incomplete beta function.
685 There are also various special cases: refer to the code for details.
699 <pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="identifier">p</span> <span class="special">=</span> <span class="identifier">I</span><span class="special">[</span><span class="identifier">sub</span> <span class="number">1</span><span class="special">-</span><span class="identifier">p</span><span class="special">](</span><span class="identifier">n</span> <span class="special">-</span> <span class="identifier">k</span><span class="special">,</span> <span class="identifier">k</span> <span class="special">+</span> <span class="number">1</span><span class="special">)</span>
700 <span class="special">=</span> <span class="number">1</span> <span class="special">-</span> <span class="identifier">I</span><span class="special">[</span><span class="identifier">sub</span> <span class="identifier">p</span><span class="special">](</span><span class="identifier">k</span> <span class="special">+</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">n</span> <span class="special">-</span> <span class="identifier">k</span><span class="special">)</span>
701 <span class="special">=</span> <a class="link" href="../../sf_beta/ibeta_function.html" title="Incomplete Beta Functions">ibetac</a><span class="special">(</span><span class="identifier">k</span> <span class="special">+</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">n</span> <span class="special">-</span> <span class="identifier">k</span><span class="special">,</span> <span class="identifier">p</span><span class="special">)</span></pre>
703 There are also various special cases: refer to the code for details.
715 Using the relation: q = <a class="link" href="../../sf_beta/ibeta_function.html" title="Incomplete Beta Functions">ibeta</a>(k
719 There are also various special cases: refer to the code for details.
731 Since the cdf is non-linear in variate <span class="emphasis"><em>k</em></span>
732 none of the inverse incomplete beta functions can be used here.
733 Instead the quantile is found numerically using a derivative
734 free method (<a class="link" href="../../roots_noderiv/TOMS748.html" title="Algorithm TOMS 748: Alefeld, Potra and Shi: Enclosing zeros of continuous functions">TOMS
742 quantile from the complement
747 Found numerically as above.
759 <code class="computeroutput"><span class="identifier">p</span> <span class="special">*</span>
760 <span class="identifier">n</span></code>
772 <code class="computeroutput"><span class="identifier">p</span> <span class="special">*</span>
773 <span class="identifier">n</span> <span class="special">*</span>
774 <span class="special">(</span><span class="number">1</span><span class="special">-</span><span class="identifier">p</span><span class="special">)</span></code>
786 <code class="computeroutput"><span class="identifier">floor</span><span class="special">(</span><span class="identifier">p</span> <span class="special">*</span>
787 <span class="special">(</span><span class="identifier">n</span>
788 <span class="special">+</span> <span class="number">1</span><span class="special">))</span></code>
800 <code class="computeroutput"><span class="special">(</span><span class="number">1</span>
801 <span class="special">-</span> <span class="number">2</span>
802 <span class="special">*</span> <span class="identifier">p</span><span class="special">)</span> <span class="special">/</span>
803 <span class="identifier">sqrt</span><span class="special">(</span><span class="identifier">n</span> <span class="special">*</span>
804 <span class="identifier">p</span> <span class="special">*</span>
805 <span class="special">(</span><span class="number">1</span>
806 <span class="special">-</span> <span class="identifier">p</span><span class="special">))</span></code>
818 <code class="computeroutput"><span class="number">3</span> <span class="special">-</span>
819 <span class="special">(</span><span class="number">6</span>
820 <span class="special">/</span> <span class="identifier">n</span><span class="special">)</span> <span class="special">+</span>
821 <span class="special">(</span><span class="number">1</span>
822 <span class="special">/</span> <span class="special">(</span><span class="identifier">n</span> <span class="special">*</span>
823 <span class="identifier">p</span> <span class="special">*</span>
824 <span class="special">(</span><span class="number">1</span>
825 <span class="special">-</span> <span class="identifier">p</span><span class="special">)))</span></code>
837 <code class="computeroutput"><span class="special">(</span><span class="number">1</span>
838 <span class="special">-</span> <span class="number">6</span>
839 <span class="special">*</span> <span class="identifier">p</span>
840 <span class="special">*</span> <span class="identifier">q</span><span class="special">)</span> <span class="special">/</span>
841 <span class="special">(</span><span class="identifier">n</span>
842 <span class="special">*</span> <span class="identifier">p</span>
843 <span class="special">*</span> <span class="identifier">q</span><span class="special">)</span></code>
855 The member functions <code class="computeroutput"><span class="identifier">find_upper_bound_on_p</span></code>
856 <code class="computeroutput"><span class="identifier">find_lower_bound_on_p</span></code>
857 and <code class="computeroutput"><span class="identifier">find_number_of_trials</span></code>
858 are implemented in terms of the inverse incomplete beta functions
859 <a class="link" href="../../sf_beta/ibeta_inv_function.html" title="The Incomplete Beta Function Inverses">ibetac_inv</a>,
860 <a class="link" href="../../sf_beta/ibeta_inv_function.html" title="The Incomplete Beta Function Inverses">ibeta_inv</a>,
861 and <a class="link" href="../../sf_beta/ibeta_inv_function.html" title="The Incomplete Beta Function Inverses">ibetac_invb</a>
869 <a name="math_toolkit.dist_ref.dists.binomial_dist.h11"></a>
870 <span class="phrase"><a name="math_toolkit.dist_ref.dists.binomial_dist.references"></a></span><a class="link" href="binomial_dist.html#math_toolkit.dist_ref.dists.binomial_dist.references">References</a>
872 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
873 <li class="listitem">
874 <a href="http://mathworld.wolfram.com/BinomialDistribution.html" target="_top">Weisstein,
875 Eric W. "Binomial Distribution." From MathWorld--A Wolfram
878 <li class="listitem">
879 <a href="http://en.wikipedia.org/wiki/Beta_distribution" target="_top">Wikipedia
880 binomial distribution</a>.
882 <li class="listitem">
883 <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda366i.htm" target="_top">NIST
884 Explorary Data Analysis</a>.
888 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
889 <td align="left"></td>
890 <td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar
891 Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
892 Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
893 Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
894 Daryle Walker and Xiaogang Zhang<p>
895 Distributed under the Boost Software License, Version 1.0. (See accompanying
896 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>)
901 <div class="spirit-nav">
902 <a accesskey="p" href="beta_dist.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../dists.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="cauchy_dist.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>