17. RandomArray Reference

The RandomArray.py module (in conjunction with the ranlibmodule.c file) provides a high-level interface to the ranlib module, which provides a good quality C implementation of a random-number generator.

Python Interface
seed(x=0, y=0)

The seed() function takes two integers and sets the two seeds of the random number generator to those values. If the default values of 0 are used for both x and y, then a seed is generated from the current time, providing a pseudo-random seed.

get_seed()

The get_seed() function returns the two seeds used by the current random-number generator. It is most often used to find out what seeds the seed() function chose at the last iteration. [thread-safety issue?]

random(shape= ReturnFloat )

The random() function takes a shape, and returns an array of double-precision floatings point numbers between 0.0 and 1.0. Neither 0.0 nor 1.0 is ever returned by this function. If no argument is specified, the function returns a single floating point number (not an array). The array is filled from the generator following the canonical array organization (see discussion of the .flat attribute)

uniform(minimum, maximum, shape=ReturnFloat)

The uniform() function returns an array of the specified shape and containing double-precision floating point random numbers strictly between minimum and maximum. If no shape is specified, a single number is returned.

randint(minimum, maximum, shape=ReturnFloat)

The randint() function returns an array of the specified shape and containing random (standard) integers greater than or equal to minimum and strictly less than maximum . If no shape is specified, a single number is returned.

permutation(n)

The permutation() function returns an array of the integers between 0 and n-1 , in an array of shape (n,) , and with its elements randomly permuted.

An example use of the RandomArray module (exact output will be different each time!):

>>> from RandomArray import *

>>> seed() # Set seed based on current time

>>> print get_seed() # Find out what seeds were used

(897800491, 192000)

>>> print random()

0.0528018975065

>>> print random((5,2))

[[ 0.14833829 0.99031458]

[ 0.7526806 0.09601787]

[ 0.1895229 0.97674777]

[ 0.46134511 0.25420982]

[ 0.66132009 0.24864472]]

>>> print uniform(-1,1,(10,))

[ 0.72168852 -0.75374185 -0.73590945 0.50488248 -0.74462822 0.09293685

-0.65898308 0.9718067 -0.03252475 0.99611011]

>>> print randint(0,100, (12,))

[28 5 96 19 1 32 69 40 56 69 53 44]

>>> print permutation(10)

[4 2 8 9 1 7 3 6 5 0]

>>> seed(897800491, 192000) # resetting the same seeds

>>> print random() # yields the same numbers

0.0528018975065

Floating point random arrays
standard_normal (shape=ReturnFloat)

The standard_normal () function returns an array of the specified shape that contains double precision floating point numbers normally (Gaussian) distributed with mean zero and variance and standard deviation one. If no shape is specified, a single number is returned.

normal (mean, stddev, shape=ReturnFloat)

The normal () function returns an array of the specified shape that contains double precision floating point numbers normally distributed with the specified mean and standard deviation. If no shape is specified, a single number is returned.

multivariate_normal (mean, covariance) or
multivariate_normal (mean, covariance, leadingAxesShape)

The multivariate_normal () function takes a one dimensional array argument mean and a two dimensional array argument covariance. Suppose the shape of mean is (n,). Then the shape of covariance must be (n,n). The multivariate_normal () function returns a double precision floating point array. The effect of the leadingAxesShape parameter is:

In either case, the behavior of multivariate_normal () is undefined if covariance is not symmetric and positive definite.

exponential (mean, shape=ReturnFloat)

The exponential () function returns an array of the specified shape that contains double precision floating point numbers exponentially distributed with the specified mean. If no shape is specified, a single number is returned.

beta (a, b, shape=ReturnFloat)

The beta () function returns an array of the specified shape that contains double precision floating point numbers beta distributed with alpha parameter a and beta parameter b. If no shape is specified, a single number is returned.

gamma (a, r, shape=ReturnFloat)

The gamma () function returns an array of the specified shape that contains double precision floating point numbers beta distributed with location parameter a and distribution shape parameter r. If no shape is specified, a single number is returned.

chi_square (df, shape=ReturnFloat)

The chi_square() function returns an array of the specified shape that contains double precision floating point numbers with the chi square distribution with df degrees of freedom. If no shape is specified, a single number is returned.

noncentral_chi_square (df, nonc, shape=ReturnFloat)

The noncentral_chi_square() function returns an array of the specified shape that contains double precision floating point numbers with the chi square distribution with df degrees of freedom and noncentrality parameter nconc. If no shape is specified, a single number is returned.

F (dfn, dfd, shape=ReturnFloat)

The F () function returns an array of the specified shape that contains double precision floating point numbers with the F distribution with dfn degrees of freedom in the numerator and dfd degrees of freedom in the denominator. If no shape is specified, a single number is returned.

noncentral_F (dfn, dfd, nconc, shape=ReturnFloat)

The noncentral_F () function returns an array of the specified shape that contains double precision floating point numbers with the F distribution with dfn degrees of freedom in the numerator, dfd degrees of freedom in the denominator, and noncentrality parameter nconc. If no shape is specified, a single number is returned.

Integer random arrays
binomial (trials, prob, shape=ReturnInt)

The binomial () function returns an array with the specified shape that contains integer numbers with the binomial distribution with trials trials and event probability prob. In other words, each value in the returned array is the number of times an event with probability prob occurred within trials repeated trials. If no shape is specified, a single number is returned.

negative_binomial (trials, prob, shape=ReturnInt)

The negative_binomial () function returns an array with the specified shape that contains integer numbers with the negative binomial distribution with trials trials and event probability prob. If no shape is specified, a single number is returned.

poisson (mean, shape=ReturnInt)

The poisson () function returns an array with the specified shape that contains integer numbers with the Poisson distribution with the specified mean. If no shape is specified, a single number is returned.

multinomial (trials, probs) or multinomial (trials, probs, leadingAxesShape)

The multinomial () function returns an array with that contains integer numbers with the multinomial distribution with trials trials and event probabilities given in probs. probs must be a one dimensional array. There are len(probs)+1 events. probs[i] is the probability of the i-th event for 0<=i<len(probs). The probability of event len(probs) is 1.-Numeric.sum(prob).

The first form returns an integer array of shape (len(probs)+1,) containing one multinomially distributed vector. The second form returns an array of shape (m, n, ..., len(probs)+1) where (m, n, ...) is leadingAxesShape. In this case, each output[i,j,...,:] is an integer array of shape (len(prob)+1,) containing one multinomially distributed vector..

Examples

Most of the functions in this package take zero or more distribution specific parameters plus an optional shape parameter. The shape parameter gives the shape of the output array:

>>> from RandomArray import *
>>> print standard_normal()
-0.435568600893
>>> print standard_normal(5)
[-1.36134553 0.78617644 -0.45038718 0.18508556 0.05941355]
>>> print standard_normal((5,2))
[[ 1.33448863 -0.10125473]
[ 0.66838062 0.24691346]
[-0.95092064 0.94168913]
[-0.23919107 1.89288616]
[ 0.87651485 0.96400219]]
>>> print normal(7., 4., (5,2)) #mean=7, std. dev.=4
[[ 2.66997623 11.65832615]
[ 6.73916003 6.58162862]
[ 8.47180378 4.30354905]
[ 1.35531998 -2.80886841]
[ 7.07408469 11.39024973]]
>>> print exponential(10., 5) #mean=10
[ 18.03347754 7.11702306 9.8587961 32.49231603 28.55408891]
>>> print beta(3.1, 9.1, 5) # alpha=3.1, beta=9.1
[ 0.1175056 0.17504358 0.3517828 0.06965593 0.43898219]
>>> print chi_square(7, 5) # 7 degrees of freedom (dfs)
[ 11.99046516 3.00741053 4.72235727 6.17056274 8.50756836]
>>> print noncentral_chi_square(7, 3, 5) # 7 dfs, noncentrality 3
[ 18.28332138 4.07550335 16.0425396 9.51192093 9.80156231]
>>> F(5, 7, 5) # 5 and 7 dfs
array([ 0.24693671, 3.76726145, 0.66883826, 0.59169068, 1.90763224])
>>> noncentral_F(5, 7, 3., 5) # 5 and 7 dfs, noncentrality 3
array([ 1.17992553, 0.7500126 , 0.77389943, 9.26798989, 1.35719634])
>>> binomial(32, .5, 5) # 32 trials, prob of an event = .5
array([12, 20, 21, 19, 17])
>>> negative_binomial(32, .5, 5) # 32 trials: prob of an event = .5
array([21, 38, 29, 32, 36])

Two functions that return generate multivariate random numbers (that is, random vectors with some known relationship between the elements of each vector, defined by the distribution). They are multivariate_normal () and multinomial (). For these two functions, the lengths of the leading axes of the output may be specified. The length of the last axis is determined by the length of some other parameter.

>>> multivariate_normal([1,2], [[1,2],[2,1]], [2,3])
array([[[ 0.14157988, 1.46232224],
[-1.11820295, -0.82796288],
[ 1.35251635, -0.2575901 ]],
[[-0.61142141, 1.0230465 ],
[-1.08280948, -0.55567217],
[ 2.49873002, 3.28136372]]])
>>> x = multivariate_normal([10,100], [[1,2],[2,1]], 10000)
>>> x_mean = sum(x)/10000
>>> print x_mean
[ 9.98599893 100.00032416]
>>> x_minus_mean = x - x_mean
>>> cov = matrixmultiply(transpose(x_minus_mean), x_minus_mean) / 9999.
>>> cov
array([[ 2.01737122, 1.00474408],
[ 1.00474408, 2.0009806 ]])

The a priori probabilities for a multinomial distribution must sum to one. The prior probability argument to multinomial () doesn't give the prior probability of the last event: it is computed to be one minus the sum of the others.

>>> multinomial(16, [.1, .4, .2]) # prior probabilities [.1, .4, .2, .3]
array([2, 7, 1, 6])
>>> multinomial(16, [.1, .4, .2], [2,3]) # output shape [2,3,4]
array([[[ 1, 9, 1, 5],
[ 0, 10, 3, 3],
[ 4, 9, 3, 0]],
[[ 1, 6, 1, 8],
[ 3, 4, 5, 4],
[ 1, 5, 2, 8]]])

Go to Main Go to Previous Go to Next