randomNegBinomial¶
Generates pseudorandom numbers from a negative binomial distribution.
Synopsis¶
randomNegBinomial (nRandom, rk, p)
Required Arguments¶
- int
nRandom
(Input) - Number of random numbers to generate.
- float
rk
(Input) - Negative binomial parameter. Parameter
rk
must be positive. Ifrk
is an integer, the generated deviates can be thought of as the number of failures in a sequence of Bernoulli trials beforerk
successes occur. - float
p
(Input) - Probability of failure on each trial. Parameter
p
must be greater than machine epsilon (see machine, Chapter 15, Utilities) and less than 1.0.
Return Value¶
An integer array of length nRandom
containing the random negative
binomial deviates.
Description¶
Function randomNegBinomial
generates pseudorandom numbers from a
negative binomial distribution with parameters rk
and p
. Parameters
rk
and p
must be positive and p
must be less than 1. The
probability function (with r = rk
and p = p
) is
for \(x=0,1,2,\ldots\)
If r is an integer, the distribution is often called the Pascal distribution and can be thought of as modeling the length of a sequence of Bernoulli trials until r successes are obtained, where p is the probability of getting a failure on any trial. In this form, the random variable takes values r, r + 1, r + 2, … and can be obtained from the negative binomial random variable defined above by adding r to the negative binomial variable. This latter form is also equivalent to the sum of r geometric random variables defined as taking values 1, 2, 3, …
If \(rp/(1-p)\) is less than 100 and \((1-p)^r\) is greater than the
machine epsilon, randomNegBinomial
uses the inverse CDF technique;
otherwise, for each negative binomial deviate, randomNegBinomial
generates a gamma \((r,p/(1-p))\) deviate Y and then generates a
Poisson deviate with parameter Y.
Example¶
In this example, randomNegBinomial
generates five pseudorandom negative
binomial deviates from a negative binomial (Pascal) distribution with
parameters r equal to 4 and p equal to 0.3.
from numpy import *
from pyimsl.stat.randomNegBinomial import randomNegBinomial
from pyimsl.stat.randomSeedSet import randomSeedSet
from pyimsl.stat.writeMatrix import writeMatrix
n_random = 5
rk = 4.0
p = 0.3
randomSeedSet(123457)
ir = randomNegBinomial(n_random, rk, p)
writeMatrix("Negative Binomial (4.0, 0.3) random deviates:", ir,
noColLabels=True, writeFormat="%5i")
Output¶
Negative Binomial (4.0, 0.3) random deviates:
5 1 3 2 3