randomChiSquared

Generates pseudorandom numbers from a chi-squared distribution.

Synopsis

randomChiSquared (nRandom, df)

Required Arguments

int nRandom (Input)
Number of random numbers to generate.
float df (Input)
Degrees of freedom. Parameter df must be positive.

Return Value

An array of length nRandom containing the random chi-squared deviates.

Description

Function randomChiSquared generates pseudorandom numbers from a chi-squared distribution with df degrees of freedom. If df is an even integer less than 17, the chi-squared deviate r is generated as

\[r = -2 \ln \left(\prod_{i=1}^{n} u_i\right)\]

where n = df/2 and the \(u_i\) are independent random deviates from a uniform (0, 1) distribution. If df is an odd integer less than 17, the chi-squared deviate is generated in the same way, except the square of a normal deviate is added to the expression above. If df is greater than 16 or is not an integer, and if it is not too large to cause overflow in the gamma random number generator, the chi-squared deviate is generated as a special case of a gamma deviate, using function randomGamma. If overflow would occur in randomGamma, the chi-squared deviate is generated in the manner described above, using the logarithm of the product of uniforms, but scaling the quantities to prevent underflow and overflow.

Example

In this example, randomChiSquared generates five pseudorandom chi-squared deviates with five degrees of freedom.

from numpy import *
from pyimsl.stat.randomChiSquared import randomChiSquared
from pyimsl.stat.randomSeedSet import randomSeedSet
from pyimsl.stat.writeMatrix import writeMatrix

n_random = 5
df = 5.0
randomSeedSet(123457)
r = randomChiSquared(n_random, df)
writeMatrix("Chi-Squared random deviates", r,
            noColLabels=True)

Output

 
                  Chi-Squared random deviates
      12.09         0.48         1.80        14.87         1.75