Generate Random Numbers¶
Methods for generating random numbers from a distribution based on random draws from a uniform distribution.
These were developed to debug a python script ported from MATLAB in which many random numbers were generated. Given the same random seed, one can draw the same uniform random numbers in MATLAB and python (using numpy). Unfortunately that doesn’t work for other distribution due to differences in implementation.
Functions¶
Generate random variates of various distributions based on draws from a uniform distribution See: https://www.cse.wustl.edu/~jain/books/ftp/ch5f_slides.pdf
Given the same random seed, MATLAB and numpy will generate the same draws from a uniform distribution. This isn’t the case for other distributions. Instead, it’s possible to generate other distributions based on U(0, 1) draws. In turn, one can generate the same pseudorandom numbers in MATLAB and numpy using these functions and MATLAB equivalent functions
-
rand.randombeta(a, b, size=1)¶ Generate random variates from beta distribution given shape parameters a, b
-
rand.randombetavariate(a, b)¶ Generate single random variate from beta distribution given shape parameters a, b
NOTE: only implemented for a < 0 and b < 0
-
rand.randomexponential(a, size=1)¶ Generate random variate from exponential distribution. Uses inverse CDF method
pdf: f(x) = (1/a)e^(-x/a)
Parameters: a – scale paramter (a > 0)
-
rand.randomgammaint(shape, scale=1.0, size=1)¶ draw from random gamma distribution with integer shape parameter
-
rand.randomgamma(shape, scale=1.0, size=1, tol=1e-05)¶ Generate random variates from a gamma distribution
Parameters: - shape (float or array_like of floats) – Shape of gamma distribution (should be > 0)
- scale (float or array_like of floats) – Scale of the gamma distribution (should be > 0)
- size (int) – number of variates to return
-
rand.randomnormal(mu, sigma, size=1)¶ Generate random variate from the normal distribution using the Box Muller technique
Returns:
-
rand.randombinomial(n, p)¶ Generate random variates form a binomial(n, p) distribution
Parameters: - n – number of trials
- p – probability of success
-
rand.randomdirichlet(a)¶ Python implementation of randdirichlet.m using randomgamma fucnction
Parameters: a – vector of weights (shape parameters to the gamma distribution)