#include <boost/math/distributions/negative_binomial.hpp>
namespace boost{ namespace math{ template <class RealType = double, class Policy = policies::policy<> > class negative_binomial_distribution; typedef negative_binomial_distribution<> negative_binomial; template <class RealType, class Policy> class negative_binomial_distribution { public: typedef RealType value_type; typedef Policy policy_type; // Constructor from successes and success_fraction: negative_binomial_distribution(RealType r, RealType p); // Parameter accessors: RealType success_fraction() const; RealType successes() const; // Bounds on success fraction: static RealType find_lower_bound_on_p( RealType trials, RealType successes, RealType probability); // alpha static RealType find_upper_bound_on_p( RealType trials, RealType successes, RealType probability); // alpha // Estimate min/max number of trials: static RealType find_minimum_number_of_trials( RealType k, // Number of failures. RealType p, // Success fraction. RealType probability); // Probability threshold alpha. static RealType find_maximum_number_of_trials( RealType k, // Number of failures. RealType p, // Success fraction. RealType probability); // Probability threshold alpha. }; }} // namespaces
The class type For k + r Bernoulli trials each with success fraction p, the negative_binomial distribution gives the probability of observing k failures and r successes with success on the last trial. The negative_binomial distribution assumes that success_fraction p is fixed for all (k + r) trials.
It has the PDF:
The following graph illustrate how the PDF varies as the success fraction p changes:
Alternatively, this graph shows how the shape of the PDF varies as the number of successes changes:
Related DistributionsThe name negative binomial distribution is reserved by some to the case where the successes parameter r is an integer. This integer version is also called the Pascal distribution. This implementation uses real numbers for the computation throughout (because it uses the real-valued incomplete beta function family of functions). This real-valued version is also called the Polya Distribution. The Poisson distribution is a generalization of the Pascal distribution, where the success parameter r is an integer: to obtain the Pascal distribution you must ensure that an integer value is provided for r, and take integer values (floor or ceiling) from functions that return a number of successes. For large values of r (successes), the negative binomial distribution converges to the Poisson distribution. The geometric distribution is a special case where the successes parameter r = 1, so only a first and only success is required. geometric(p) = negative_binomial(1, p). The Poisson distribution is a special case for large successes poisson(λ) = lim r → ∞ negative_binomial(r, r / (λ + r)))
Member FunctionsConstructnegative_binomial_distribution(RealType r, RealType p); Constructor: r is the total number of successes, p is the probability of success of a single trial.
Requires: AccessorsRealType success_fraction() const; // successes / trials (0 <= p <= 1) Returns the parameter p from which this distribution was constructed. RealType successes() const; // required successes (r > 0) Returns the parameter r from which this distribution was constructed. Lower Bound on Parameter pstatic RealType find_lower_bound_on_p( RealType failures, RealType successes, RealType probability) // (0 <= alpha <= 1), 0.05 equivalent to 95% confidence. Returns a lower bound on the success fraction:
For example, if you observe k failures and r successes from n = k + r trials the best estimate for the success fraction is simply r/n, but if you want to be 95% sure that the true value is greater than some value, pmin, then: pmin = negative_binomial_distribution<RealType>::find_lower_bound_on_p( failures, successes, 0.05); See negative binomial confidence interval example. This function uses the Clopper-Pearson method of computing the lower bound on the success fraction, whilst many texts refer to this method as giving an "exact" result in practice it produces an interval that guarantees at least the coverage required, and may produce pessimistic estimates for some combinations of failures and successes. See: Upper Bound on Parameter pstatic RealType find_upper_bound_on_p( RealType trials, RealType successes, RealType alpha); // (0 <= alpha <= 1), 0.05 equivalent to 95% confidence. Returns an upper bound on the success fraction:
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, pmax, then: pmax = negative_binomial_distribution<RealType>::find_upper_bound_on_p( r, k, 0.05); See negative binomial confidence interval example. This function uses the Clopper-Pearson method of computing the lower bound on the success fraction, whilst many texts refer to this method as giving an "exact" result in practice it produces an interval that guarantees at least the coverage required, and may produce pessimistic estimates for some combinations of failures and successes. See: Estimating Number of Trials to Ensure at Least a Certain Number of Failuresstatic RealType find_minimum_number_of_trials( RealType k, // number of failures. RealType p, // success fraction. RealType alpha); // probability threshold (0.05 equivalent to 95%). This functions estimates the number of trials required to achieve a certain probability that more than k failures will be observed.
For example: negative_binomial_distribution<RealType>::find_minimum_number_of_trials(10, 0.5, 0.05); Returns the smallest number of trials we must conduct to be 95% sure of seeing 10 failures that occur with frequency one half. This function uses numeric inversion of the negative binomial distribution to obtain the result: another interpretation of the result, is that it finds the number of trials (success+failures) that will lead to an alpha probability of observing k failures or fewer. Estimating Number of Trials to Ensure a Maximum Number of Failures or Lessstatic RealType find_maximum_number_of_trials( RealType k, // number of failures. RealType p, // success fraction. RealType alpha); // probability threshold (0.05 equivalent to 95%). This functions estimates the maximum number of trials we can conduct and achieve a certain probability that k failures or fewer will be observed.
For example: negative_binomial_distribution<RealType>::find_maximum_number_of_trials(0, 1.0-1.0/1000000, 0.05); Returns the largest number of trials we can conduct and still be 95% sure of seeing no failures that occur with frequency one in one million. This function uses numeric inversion of the negative binomial distribution to obtain the result: another interpretation of the result, is that it finds the number of trials (success+failures) that will lead to an alpha probability of observing more than k failures. Non-member AccessorsAll the usual non-member accessor functions that are generic to all distributions are supported: Cumulative Distribution Function, Probability Density Function, Quantile, Hazard Function, Cumulative Hazard Function, mean, median, mode, variance, standard deviation, skewness, kurtosis, kurtosis_excess, range and support. However it's worth taking a moment to define what these actually mean in the context of this distribution: Table?12.?Meaning of the non-member accessors.
AccuracyThis distribution is implemented using the incomplete beta functions ibeta and ibetac: please refer to these functions for information on accuracy. ImplementationIn the following table, p is the probability that any one trial will be successful (the success fraction), r is the number of successes, k is the number of failures, p is the probability and q = 1-p.
Implementation notes:
|