binomialPdf

Evaluates the binomial probability function.

Synopsis

binomialPdf (k, n, p)

Required Arguments

int k (Input)
Argument for which the binomial probability function is to be evaluated.
int n (Input)
Number of Bernoulli trials.
float p (Input)
Probability of success on each trial.

Return Value

The probability that a binomial random variable takes on a value equal to k.

Description

The function binomialPdf evaluates the probability that a binomial random variable with parameters n and p takes on the value k. Specifically,

\[f(k|n,p) = \Pr(X=k) = \binom{n}{k} p^k (1-p)^{n-k}\]

where \(k=\{0,1,2,\ldots,n\}\), \(n\geq 1\), \(0\leq p\leq 1\), and

\[\binom{n}{k} = \frac{n!}{k!(n-k)!}\]

These probabilities are computed by the recursive relationship:

\[\Pr(X=k) = \frac{(n+1-k)p}{k(1-p)} \Pr(X=k-1)\]

To avoid the possibility of underflow, the probabilities are computed forward from 0, if k is not greater than n × p, and are computed backward from n otherwise. The smallest positive machine number, ɛ, is used as the starting value for summing the probabilities, which are rescaled by \((1-\text{p})^n \varepsilon\) if forward computation is performed and by \(\text{p}^n \varepsilon\) if backward computation is done.

For the special case of p = 0, binomialPdf is set to 0 if k is greater than 0 and to 1 otherwise; and for the case p = 1, binomialPdf is set to 0 if k is less than n and to 1 otherwise.

Examples

Suppose X is a binomial random variable with \(n=5\) and \(p=0.95\). In this example, we find the probability that X is equal to 3.

from __future__ import print_function
from numpy import *
from pyimsl.stat.binomialPdf import binomialPdf

k = 3
n = 5
p = 0.95
pr = binomialPdf(k, n, p)
print("Pr(x = 3) = %6.4f" % pr)

Output

Pr(x = 3) = 0.0214