File: binomial.qbk

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (400 lines) | stat: -rw-r--r-- 14,820 bytes parent folder | download | duplicates (10)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
[section:binomial_dist Binomial Distribution]

``#include <boost/math/distributions/binomial.hpp>``

   namespace boost{ namespace math{

   template <class RealType = double,
             class ``__Policy``   = ``__policy_class`` >
   class binomial_distribution;

   typedef binomial_distribution<> binomial;

   template <class RealType, class ``__Policy``>
   class binomial_distribution
   {
   public:
      typedef RealType  value_type;
      typedef Policy    policy_type;

      static const ``['unspecified-type]`` clopper_pearson_exact_interval;
      static const ``['unspecified-type]`` jeffreys_prior_interval;

      // construct:
      binomial_distribution(RealType n, RealType p);

      // parameter access::
      RealType success_fraction() const;
      RealType trials() const;

      // Bounds on success fraction:
      static RealType find_lower_bound_on_p(
         RealType trials,
         RealType successes,
         RealType probability,
         ``['unspecified-type]`` method = clopper_pearson_exact_interval);
      static RealType find_upper_bound_on_p(
         RealType trials,
         RealType successes,
         RealType probability,
         ``['unspecified-type]`` method = clopper_pearson_exact_interval);

      // estimate min/max number of trials:
      static RealType find_minimum_number_of_trials(
         RealType k,     // number of events
         RealType p,     // success fraction
         RealType alpha); // risk level

      static RealType find_maximum_number_of_trials(
         RealType k,     // number of events
         RealType p,     // success fraction
         RealType alpha); // risk level
   };

   }} // namespaces

The class type `binomial_distribution` represents a
[@http://mathworld.wolfram.com/BinomialDistribution.html binomial distribution]:
it is used when there are exactly two mutually
exclusive outcomes of a trial. These outcomes are labelled
"success" and "failure". The
__binomial_distrib is used to obtain
the probability of observing k successes in N trials, with the
probability of success on a single trial denoted by p. The
binomial distribution assumes that p is fixed for all trials.

[note The random variable for the binomial distribution is the number of successes,
(the number of trials is a fixed property of the distribution)
whereas for the negative binomial,
the random variable is the number of trials, for a fixed number of successes.]

The PDF for the binomial distribution is given by:

[equation binomial_ref2]

The following two graphs illustrate how the PDF changes depending
upon the distributions parameters, first we'll keep the success
fraction /p/ fixed at 0.5, and vary the sample size:

[graph binomial_pdf_1]

Alternatively, we can keep the sample size fixed at N=20 and
vary the success fraction /p/:

[graph binomial_pdf_2]

[discrete_quantile_warning Binomial]

[h4 Member Functions]

[h5 Construct]

   binomial_distribution(RealType n, RealType p);

Constructor: /n/ is the total number of trials, /p/ is the
probability of success of a single trial.

Requires `0 <= p <= 1`, and `n >= 0`, otherwise calls __domain_error.

[h5 Accessors]

   RealType success_fraction() const;

Returns the parameter /p/ from which this distribution was constructed.

   RealType trials() const;

Returns the parameter /n/ from which this distribution was constructed.

[h5 Lower Bound on the Success Fraction]

   static RealType find_lower_bound_on_p(
      RealType trials,
      RealType successes,
      RealType alpha,
      ``['unspecified-type]`` method = clopper_pearson_exact_interval);

Returns a lower bound on the success fraction:

[variablelist
[[trials][The total number of trials conducted.]]
[[successes][The number of successes that occurred.]]
[[alpha][The largest acceptable probability that the true value of
         the success fraction is [*less than] the value returned.]]
[[method][An optional parameter that specifies the method to be used
         to compute the interval (See below).]]
]

For example, if you observe /k/ successes from /n/ trials the
best estimate for the success fraction is simply ['k/n], but if you
want to be 95% sure that the true value is [*greater than] some value,
['p[sub min]], then:

   p``[sub min]`` = binomial_distribution<RealType>::find_lower_bound_on_p(n, k, 0.05);

[link math_toolkit.stat_tut.weg.binom_eg.binom_conf See worked example.]

There are currently two possible values available for the /method/
optional parameter: /clopper_pearson_exact_interval/
or /jeffreys_prior_interval/.  These constants are both members of
class template `binomial_distribution`, so usage is for example:

   p = binomial_distribution<RealType>::find_lower_bound_on_p(
       n, k, 0.05, binomial_distribution<RealType>::jeffreys_prior_interval);

The default method if this parameter is not specified is the Clopper Pearson
"exact" interval.  This produces an interval that guarantees at least
`100(1-alpha)%` coverage, but which is known to be overly conservative,
sometimes producing intervals with much greater than the requested coverage.

The alternative calculation method produces a non-informative
Jeffreys Prior interval.  It produces `100(1-alpha)%` coverage only
['in the average case], though is typically very close to the requested
coverage level.  It is one of the main methods of calculation recommended
in the review by Brown, Cai and DasGupta.

Please note that the "textbook" calculation method using
a normal approximation (the Wald interval) is deliberately
not provided: it is known to produce consistently poor results,
even when the sample size is surprisingly large.
Refer to Brown, Cai and DasGupta for a full explanation.  Many other methods
of calculation are available, and may be more appropriate for specific
situations.  Unfortunately there appears to be no consensus amongst
statisticians as to which is "best": refer to the discussion at the end of
Brown, Cai and DasGupta for examples.

The two methods provided here were chosen principally because they
can be used for both one and two sided intervals.
See also:

Lawrence D. Brown, T. Tony Cai and Anirban DasGupta (2001),
Interval Estimation for a Binomial Proportion,
Statistical Science, Vol. 16, No. 2, 101-133.

T. Tony Cai (2005),
One-sided confidence intervals in discrete distributions,
Journal of Statistical Planning and Inference 131, 63-88.

Agresti, A. and Coull, B. A. (1998). Approximate is better than
"exact" for interval estimation of binomial proportions. Amer.
Statist. 52 119-126.

Clopper, C. J. and Pearson, E. S. (1934). The use of confidence
or fiducial limits illustrated in the case of the binomial.
Biometrika 26 404-413.

[h5 Upper Bound on the Success Fraction]

   static RealType find_upper_bound_on_p(
      RealType trials,
      RealType successes,
      RealType alpha,
      ``['unspecified-type]`` method = clopper_pearson_exact_interval);

Returns an upper bound on the success fraction:

[variablelist
[[trials][The total number of trials conducted.]]
[[successes][The number of successes that occurred.]]
[[alpha][The largest acceptable probability that the true value of
         the success fraction is [*greater than] the value returned.]]
[[method][An optional parameter that specifies the method to be used
         to compute the interval. Refer to the documentation for
         `find_upper_bound_on_p` above for the meaning of the
         method options.]]
]

For example, if you observe /k/ successes from /n/ trials the
best estimate for the success fraction is simply ['k/n], but if you
want to be 95% sure that the true value is [*less than] some value,
['p[sub max]], then:

   p``[sub max]`` = binomial_distribution<RealType>::find_upper_bound_on_p(n, k, 0.05);

[link math_toolkit.stat_tut.weg.binom_eg.binom_conf See worked example.]

[note
In order to obtain a two sided bound on the success fraction, you
call both `find_lower_bound_on_p` *and* `find_upper_bound_on_p`
each with the same arguments.

If the desired risk level
that the true success fraction lies outside the bounds is [alpha],
then you pass [alpha]/2 to these functions.

So for example a two sided 95% confidence interval would be obtained
by passing [alpha] = 0.025 to each of the functions.

[link math_toolkit.stat_tut.weg.binom_eg.binom_conf See worked example.]
]


[h5 Estimating the Number of Trials Required for a Certain Number of Successes]

   static RealType find_minimum_number_of_trials(
      RealType k,     // number of events
      RealType p,     // success fraction
      RealType alpha); // probability threshold

This function estimates the minimum number of trials required to ensure that
more than k events is observed with a level of risk /alpha/ that k or
fewer events occur.

[variablelist
[[k][The number of success observed.]]
[[p][The probability of success for each trial.]]
[[alpha][The maximum acceptable probability that k events or fewer will be observed.]]
]

For example:

   binomial_distribution<RealType>::find_number_of_trials(10, 0.5, 0.05);

Returns the smallest number of trials we must conduct to be 95% sure
of seeing 10 events that occur with frequency one half.

[h5 Estimating the Maximum Number of Trials to Ensure no more than a Certain Number of Successes]

   static RealType find_maximum_number_of_trials(
      RealType k,     // number of events
      RealType p,     // success fraction
      RealType alpha); // probability threshold

This function estimates the maximum number of trials we can conduct
to ensure that k successes or fewer are observed, with a risk /alpha/
that more than k occur.

[variablelist
[[k][The number of success observed.]]
[[p][The probability of success for each trial.]]
[[alpha][The maximum acceptable probability that more than k events will be observed.]]
]

For example:

   binomial_distribution<RealType>::find_maximum_number_of_trials(0, 1e-6, 0.05);

Returns the largest number of trials we can conduct and still be 95% certain
of not observing any events that occur with one in a million frequency.
This is typically used in failure analysis.

[link math_toolkit.stat_tut.weg.binom_eg.binom_size_eg See Worked Example.]

[h4 Non-member Accessors]

All the [link math_toolkit.dist_ref.nmp usual non-member accessor functions]
that are generic to all distributions are supported: __usual_accessors.

The domain for the random variable /k/ is `0 <= k <= N`, otherwise a
__domain_error is returned.

It's worth taking a moment to define what these accessors actually mean in
the context of this distribution:

[table Meaning of the non-member accessors
[[Function][Meaning]]
[[__pdf]
   [The probability of obtaining [*exactly k successes] from n trials
   with success fraction p.  For example:

`pdf(binomial(n, p), k)`]]
[[__cdf]
   [The probability of obtaining [*k successes or fewer] from n trials
   with success fraction p.  For example:

`cdf(binomial(n, p), k)`]]
[[__ccdf]
   [The probability of obtaining [*more than k successes] from n trials
   with success fraction p.  For example:

`cdf(complement(binomial(n, p), k))`]]
[[__quantile]
   [Given a binomial distribution with ['n] trials, success fraction ['p] and probability ['P], 
   finds the largest number of successes ['k] whose CDF is less than ['P].
   It is strongly recommended that you read the tutorial 
   [link math_toolkit.pol_tutorial.understand_dis_quant Understanding Quantiles of Discrete Distributions] before
   using the quantile function.]]
[[__quantile_c]
   [Given a binomial distribution with ['n] trials, success fraction ['p] and probability ['Q], 
   finds the smallest number of successes ['k] whose CDF is greater than ['1-Q].
   It is strongly recommended that you read the tutorial 
   [link math_toolkit.pol_tutorial.understand_dis_quant Understanding Quantiles of Discrete Distributions] before
   using the quantile function.]]
]

[h4 Examples]

Various [link math_toolkit.stat_tut.weg.binom_eg worked examples]
are available illustrating the use of the binomial distribution.

[h4 Accuracy]

This distribution is implemented using the
incomplete beta functions __ibeta and __ibetac,
please refer to these functions for information on accuracy.

[h4 Implementation]

In the following table /p/ is the probability that one trial will
be successful (the success fraction), /n/ is the number of trials,
/k/ is the number of successes, /p/ is the probability and /q = 1-p/.

[table
[[Function][Implementation Notes]]
[[pdf][Implementation is in terms of __ibeta_derivative: if [sub n]C[sub k ] is the binomial
       coefficient of a and b, then we have:

[equation binomial_ref1]

Which can be evaluated as `ibeta_derivative(k+1, n-k+1, p) / (n+1)`

The function __ibeta_derivative is used here, since it has already
       been optimised for the lowest possible error - indeed this is really
       just a thin wrapper around part of the internals of the incomplete
       beta function.

There are also various special cases: refer to the code for details.
       ]]
[[cdf][Using the relation:

``
p = I[sub 1-p](n - k, k + 1)
  = 1 - I[sub p](k + 1, n - k)
  = __ibetac(k + 1, n - k, p)``

There are also various special cases: refer to the code for details.
]]
[[cdf complement][Using the relation: q = __ibeta(k + 1, n - k, p)

There are also various special cases: refer to the code for details. ]]
[[quantile][Since the cdf is non-linear in variate /k/ none of the inverse
            incomplete beta functions can be used here.  Instead the quantile
            is found numerically using a derivative free method
            (__root_finding_TOMS748).]]
[[quantile from the complement][Found numerically as above.]]
[[mean][ `p * n` ]]
[[variance][ `p * n * (1-p)` ]]
[[mode][`floor(p * (n + 1))`]]
[[skewness][`(1 - 2 * p) / sqrt(n * p * (1 - p))`]]
[[kurtosis][`3 - (6 / n) + (1 / (n * p * (1 - p)))`]]
[[kurtosis excess][`(1 - 6 * p * q) / (n * p * q)`]]
[[parameter estimation][The member functions `find_upper_bound_on_p`
       `find_lower_bound_on_p` and `find_number_of_trials` are
       implemented in terms of the inverse incomplete beta functions
       __ibetac_inv, __ibeta_inv, and __ibetac_invb respectively]]
]

[h4 References]

* [@http://mathworld.wolfram.com/BinomialDistribution.html Weisstein, Eric W. "Binomial Distribution." From MathWorld--A Wolfram Web Resource].
* [@http://en.wikipedia.org/wiki/Beta_distribution Wikipedia binomial distribution].
* [@http://www.itl.nist.gov/div898/handbook/eda/section3/eda366i.htm  NIST Exploratory Data Analysis].

[endsect] [/section:binomial_dist Binomial]

[/ binomial.qbk
  Copyright 2006 John Maddock and Paul A. Bristow.
  Distributed under 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).
]