1 [section:geometric_dist Geometric Distribution]
3 ``#include <boost/math/distributions/geometric.hpp>``
5 namespace boost{ namespace math{
7 template <class RealType = double,
8 class ``__Policy`` = ``__policy_class`` >
9 class geometric_distribution;
11 typedef geometric_distribution<> geometric;
13 template <class RealType, class ``__Policy``>
14 class geometric_distribution
17 typedef RealType value_type;
18 typedef Policy policy_type;
19 // Constructor from success_fraction:
20 geometric_distribution(RealType p);
22 // Parameter accessors:
23 RealType success_fraction() const;
24 RealType successes() const;
26 // Bounds on success fraction:
27 static RealType find_lower_bound_on_p(
30 RealType probability); // alpha
31 static RealType find_upper_bound_on_p(
34 RealType probability); // alpha
36 // Estimate min/max number of trials:
37 static RealType find_minimum_number_of_trials(
38 RealType k, // Number of failures.
39 RealType p, // Success fraction.
40 RealType probability); // Probability threshold alpha.
41 static RealType find_maximum_number_of_trials(
42 RealType k, // Number of failures.
43 RealType p, // Success fraction.
44 RealType probability); // Probability threshold alpha.
49 The class type `geometric_distribution` represents a
50 [@http://en.wikipedia.org/wiki/geometric_distribution geometric distribution]:
51 it is used when there are exactly two mutually exclusive outcomes of a
52 [@http://en.wikipedia.org/wiki/Bernoulli_trial Bernoulli trial]:
53 these outcomes are labelled "success" and "failure".
55 For [@http://en.wikipedia.org/wiki/Bernoulli_trial Bernoulli trials]
56 each with success fraction /p/, the geometric distribution gives
57 the probability of observing /k/ trials (failures, events, occurrences, or arrivals)
58 before the first success.
60 [note For this implementation, the set of trials *includes zero*
61 (unlike another definition where the set of trials starts at one, sometimes named /shifted/).]
62 The geometric distribution assumes that success_fraction /p/ is fixed for all /k/ trials.
64 The probability that there are /k/ failures before the first success is
66 __spaces Pr(Y=/k/) = (1-/p/)[super /k/]/p/
68 For example, when throwing a 6-face dice the success probability /p/ = 1/6 = 0.1666[recur][space].
69 Throwing repeatedly until a /three/ appears,
70 the probability distribution of the number of times /not-a-three/ is thrown
73 Geometric distribution has the Probability Density Function PDF:
75 __spaces (1-/p/)[super /k/]/p/
77 The following graph illustrates how the PDF and CDF vary for three examples
78 of the success fraction /p/,
79 (when considering the geometric distribution as a continuous function),
81 [graph geometric_pdf_2]
83 [graph geometric_cdf_2]
87 [graph geometric_pdf_discrete]
89 [graph geometric_cdf_discrete]
92 [h4 Related Distributions]
94 The geometric distribution is a special case of
95 the __negative_binomial_distrib with successes parameter /r/ = 1,
96 so only one first and only success is required : thus by definition
97 __spaces `geometric(p) == negative_binomial(1, p)`
99 negative_binomial_distribution(RealType r, RealType success_fraction);
100 negative_binomial nb(1, success_fraction);
101 geometric g(success_fraction);
102 ASSERT(pdf(nb, 1) == pdf(g, 1));
104 This implementation uses real numbers for the computation throughout
105 (because it uses the *real-valued* power and exponential functions).
106 So to obtain a conventional strictly-discrete geometric distribution
107 you must ensure that an integer value is provided for the number of trials
108 (random variable) /k/,
109 and take integer values (floor or ceil functions) from functions that return
110 a number of successes.
112 [discrete_quantile_warning geometric]
114 [h4 Member Functions]
118 geometric_distribution(RealType p);
120 Constructor: /p/ or success_fraction is the probability of success of a single trial.
122 Requires: `0 <= p <= 1`.
126 RealType success_fraction() const; // successes / trials (0 <= p <= 1)
128 Returns the success_fraction parameter /p/ from which this distribution was constructed.
130 RealType successes() const; // required successes always one,
131 // included for compatibility with negative binomial distribution
132 // with successes r == 1.
136 The following functions are equivalent to those provided for the negative binomial,
137 with successes = 1, but are provided here for completeness.
139 The best method of calculation for the following functions is disputed:
140 see __binomial_distrib and __negative_binomial_distrib for more discussion.
142 [h5 Lower Bound on success_fraction Parameter ['p]]
144 static RealType find_lower_bound_on_p(
146 RealType probability) // (0 <= alpha <= 1), 0.05 equivalent to 95% confidence.
148 Returns a *lower bound* on the success fraction:
151 [[failures][The total number of failures before the 1st success.]]
152 [[alpha][The largest acceptable probability that the true value of
153 the success fraction is [*less than] the value returned.]]
156 For example, if you observe /k/ failures from /n/ trials
157 the best estimate for the success fraction is simply 1/['n], but if you
158 want to be 95% sure that the true value is [*greater than] some value,
161 p``[sub min]`` = geometric_distribution<RealType>::
162 find_lower_bound_on_p(failures, 0.05);
164 [link math_toolkit.stat_tut.weg.neg_binom_eg.neg_binom_conf See negative_binomial confidence interval example.]
166 This function uses the Clopper-Pearson method of computing the lower bound on the
167 success fraction, whilst many texts refer to this method as giving an "exact"
168 result in practice it produces an interval that guarantees ['at least] the
169 coverage required, and may produce pessimistic estimates for some combinations
170 of /failures/ and /successes/. See:
172 [@http://www.ucs.louisiana.edu/~kxk4695/Discrete_new.pdf
173 Yong Cai and K. Krishnamoorthy, A Simple Improved Inferential Method for Some Discrete Distributions.
174 Computational statistics and data analysis, 2005, vol. 48, no3, 605-621].
176 [h5 Upper Bound on success_fraction Parameter p]
178 static RealType find_upper_bound_on_p(
180 RealType alpha); // (0 <= alpha <= 1), 0.05 equivalent to 95% confidence.
182 Returns an *upper bound* on the success fraction:
185 [[trials][The total number of trials conducted.]]
186 [[alpha][The largest acceptable probability that the true value of
187 the success fraction is [*greater than] the value returned.]]
190 For example, if you observe /k/ successes from /n/ trials the
191 best estimate for the success fraction is simply ['k/n], but if you
192 want to be 95% sure that the true value is [*less than] some value,
195 p``[sub max]`` = geometric_distribution<RealType>::find_upper_bound_on_p(
198 [link math_toolkit.stat_tut.weg.neg_binom_eg.neg_binom_conf See negative binomial confidence interval example.]
200 This function uses the Clopper-Pearson method of computing the lower bound on the
201 success fraction, whilst many texts refer to this method as giving an "exact"
202 result in practice it produces an interval that guarantees ['at least] the
203 coverage required, and may produce pessimistic estimates for some combinations
204 of /failures/ and /successes/. See:
206 [@http://www.ucs.louisiana.edu/~kxk4695/Discrete_new.pdf
207 Yong Cai and K. Krishnamoorthy, A Simple Improved Inferential Method for Some Discrete Distributions.
208 Computational statistics and data analysis, 2005, vol. 48, no3, 605-621].
210 [h5 Estimating Number of Trials to Ensure at Least a Certain Number of Failures]
212 static RealType find_minimum_number_of_trials(
213 RealType k, // number of failures.
214 RealType p, // success fraction.
215 RealType alpha); // probability threshold (0.05 equivalent to 95%).
217 This functions estimates the number of trials required to achieve a certain
218 probability that [*more than ['k] failures will be observed].
221 [[k][The target number of failures to be observed.]]
222 [[p][The probability of ['success] for each trial.]]
223 [[alpha][The maximum acceptable ['risk] that only ['k] failures or fewer will be observed.]]
228 geometric_distribution<RealType>::find_minimum_number_of_trials(10, 0.5, 0.05);
230 Returns the smallest number of trials we must conduct to be 95% (1-0.05) sure
231 of seeing 10 failures that occur with frequency one half.
233 [link math_toolkit.stat_tut.weg.neg_binom_eg.neg_binom_size_eg Worked Example.]
235 This function uses numeric inversion of the geometric distribution
236 to obtain the result: another interpretation of the result is that it finds
237 the number of trials (failures) that will lead to an /alpha/ probability
238 of observing /k/ failures or fewer.
240 [h5 Estimating Number of Trials to Ensure a Maximum Number of Failures or Less]
242 static RealType find_maximum_number_of_trials(
243 RealType k, // number of failures.
244 RealType p, // success fraction.
245 RealType alpha); // probability threshold (0.05 equivalent to 95%).
247 This functions estimates the maximum number of trials we can conduct and achieve
248 a certain probability that [*k failures or fewer will be observed].
251 [[k][The maximum number of failures to be observed.]]
252 [[p][The probability of ['success] for each trial.]]
253 [[alpha][The maximum acceptable ['risk] that more than ['k] failures will be observed.]]
258 geometric_distribution<RealType>::find_maximum_number_of_trials(0, 1.0-1.0/1000000, 0.05);
260 Returns the largest number of trials we can conduct and still be 95% sure
261 of seeing no failures that occur with frequency one in one million.
263 This function uses numeric inversion of the geometric distribution
264 to obtain the result: another interpretation of the result, is that it finds
265 the number of trials that will lead to an /alpha/ probability
266 of observing more than k failures.
268 [h4 Non-member Accessors]
270 All the [link math_toolkit.dist_ref.nmp usual non-member accessor functions]
271 that are generic to all distributions are supported: __usual_accessors.
273 However it's worth taking a moment to define what these actually mean in
274 the context of this distribution:
276 [table Meaning of the non-member accessors.
277 [[Function][Meaning]]
279 [The probability of obtaining [*exactly k failures] from /k/ trials
280 with success fraction p. For example:
282 ``pdf(geometric(p), k)``]]
284 [The probability of obtaining [*k failures or fewer] from /k/ trials
285 with success fraction p and success on the last trial. For example:
287 ``cdf(geometric(p), k)``]]
289 [The probability of obtaining [*more than k failures] from /k/ trials
290 with success fraction p and success on the last trial. For example:
292 ``cdf(complement(geometric(p), k))``]]
294 [The [*greatest] number of failures /k/ expected to be observed from /k/ trials
295 with success fraction /p/, at probability /P/. Note that the value returned
296 is a real-number, and not an integer. Depending on the use case you may
297 want to take either the floor or ceiling of the real result. For example:
298 ``quantile(geometric(p), P)``]]
300 [The [*smallest] number of failures /k/ expected to be observed from /k/ trials
301 with success fraction /p/, at probability /P/. Note that the value returned
302 is a real-number, and not an integer. Depending on the use case you may
303 want to take either the floor or ceiling of the real result. For example:
304 ``quantile(complement(geometric(p), P))``]]
309 This distribution is implemented using the pow and exp functions, so most results
310 are accurate within a few epsilon for the RealType.
311 For extreme values of `double` /p/, for example 0.9999999999,
312 accuracy can fall significantly, for example to 10 decimal digits (from 16).
316 In the following table, /p/ is the probability that any one trial will
317 be successful (the success fraction), /k/ is the number of failures,
318 /p/ is the probability and /q = 1-p/,
319 /x/ is the given probability to estimate
320 the expected number of failures using the quantile.
323 [[Function][Implementation Notes]]
324 [[pdf][pdf = p * pow(q, k)]]
325 [[cdf][cdf = 1 - q[super k=1]]]
326 [[cdf complement][exp(log1p(-p) * (k+1))]]
327 [[quantile][k = log1p(-x) / log1p(-p) -1]]
328 [[quantile from the complement][k = log(x) / log1p(-p) -1]]
330 [[variance][(1-p)/p[sup2]]]
332 [[skewness][(2-p)/[sqrt]q]]
333 [[kurtosis][9+p[sup2]/q]]
334 [[kurtosis excess][6 +p[sup2]/q]]
335 [[parameter estimation member functions][See __negative_binomial_distrib]]
336 [[`find_lower_bound_on_p`][See __negative_binomial_distrib]]
337 [[`find_upper_bound_on_p`][See __negative_binomial_distrib]]
338 [[`find_minimum_number_of_trials`][See __negative_binomial_distrib]]
339 [[`find_maximum_number_of_trials`][See __negative_binomial_distrib]]
342 [endsect][/section:geometric_dist geometric]
345 Copyright 2010 John Maddock and Paul A. Bristow.
346 Distributed under the Boost Software License, Version 1.0.
347 (See accompanying file LICENSE_1_0.txt or copy at
348 http://www.boost.org/LICENSE_1_0.txt).