randomGeneralContinuous

../../_images/OpenMp_27.png

Generates pseudorandom numbers from a general continuous distribution.

Synopsis

randomGeneralContinuous (nRandom, ndata, table)

Required Arguments

int nRandom (Input)
Number of random numbers to generate.
int ndata (Input)
Number of points at which the CDF is evaluated for interpolation. ndata must be greater than or equal to 4.
float table (Input/Ouput)
ndata by 5 table to be used for interpolation of the cumulative distribution function. The first column of table contains abscissas of the cumulative distribution function in ascending order, the second column contains the values of the CDF (which must be strictly increasing beginning with 0.0 and ending at 1.0) and the remaining columns contain values used in interpolation. This table is set up using function continuousTableSetup.

Return Value

An array of length nRandom containing the random discrete deviates.

Description

Function randomGeneralContinuous generates pseudorandom numbers from a continuous distribution using the inverse CDF technique, by interpolation of points of the distribution function given in table, which is set up by function continuousTableSetup. A strictly monotone increasing distribution function is assumed. The interpolation is by an algorithm attributable to Akima (1970), using piecewise cubics. The use of this technique for generation of random numbers is due to Guerra, Tapia, and Thompson (1976), who give a description of the algorithm and accuracy comparisons between this method and linear interpolation. The relative errors using the Akima interpolation are generally considered very good.

Example

In this example, continuousTableSetup is used to set up a table for generation of beta pseudorandom deviates. The CDF for this distribution is computed by the function betaCdf (Chapter 11, Probability Distribution Functions and Inverses). The table contains 100 points at which the CDF is evaluated and that are used for interpolation.

from numpy import *
from pyimsl.stat.betaCdf import betaCdf
from pyimsl.stat.continuousTableSetup import continuousTableSetup
from pyimsl.stat.randomGeneralContinuous import randomGeneralContinuous
from pyimsl.stat.randomSeedSet import randomSeedSet
from pyimsl.stat.writeMatrix import writeMatrix


def cdf(x):
    return betaCdf(x, 3., 2.)


ndata = 100
iopt = 0
table = empty((100, 5), dtype='double')
x = 0.0

for i in range(0, ndata):
    table[i, 0] = x
    x = x + 0.01

continuousTableSetup(cdf, iopt, [table])

randomSeedSet(123457)

r = randomGeneralContinuous(5, table)

writeMatrix("Beta (3, 2) random deviates", r, column=True)

Output

***
*** Warning error issued from IMSL function continuousTableSetup:
*** The values of the CDF in the second column of TABLE did not begin at 0.0 and end at 1.0, but they have been adjusted.  Prior to adjustment, table[0][1] = 0.000000e+00 and table[ndata-1][1] = 9.994080e-01.
***
 
Beta (3, 2) random deviates
      1       0.9208
      2       0.4641
      3       0.7668
      4       0.6536
      5       0.8171