GeneralRandom

class gammapy.utils.distributions.GeneralRandom(pdf, min_range, max_range, ninversecdf=None, ran_res=1000.0)[source]

Bases: object

Fast random number generation with an arbitrary pdf of a continuous variable x. Linear interpolation is applied between points pdf(x) at which the pdf is specified.

I started with the recipy 576556, removed some unnecessary stuff and added some useful stuff. Recipe 576556: Generating random numbers with arbitrary distribution http://code.activestate.com/recipes/576556/

Note: This class can only handle 1D distributions.

Note: Should it be required the cdf could be deleted after computing to inversecdf to free memory since it is not required for random number generation.

Initialize the lookup table

Inputs: x: random number values pdf: probability density profile at that point ninversecdf: number of reverse lookup values

Lookup is computed and stored in: cdf: cumulative pdf inversecdf: the inverse lookup table delta_inversecdf: difference of inversecdf ran_res: Resolution of the PDF

Methods Summary

draw([N, random_state]) Returns an array of random numbers with the requested distribution.
make_plots([N]) Plot the pdf, cdf and inversecdf and a random distribution of sample size N.

Methods Documentation

draw(N=1000, random_state='random-seed')[source]

Returns an array of random numbers with the requested distribution.

The random numbers x are generated using the lookups inversecdf and delta_inversecdf.

Parameters:

N : int

array length

random_state : {int, ‘random-seed’, ‘global-rng’, RandomState}

Defines random number generator initialisation. Passed to get_random_state.

Returns:

x : ndarray

random numbers

make_plots(N=100000.0)[source]

Plot the pdf, cdf and inversecdf and a random distribution of sample size N.

Useful for illustrating the interpolation and debugging.