Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast arbitrary distribution random sampling (inverse transform sampling)

The random module (http://docs.python.org/2/library/random.html) has several fixed functions to randomly sample from. For example random.gauss will sample random point from a normal distribution with a given mean and sigma values.

I'm looking for a way to extract a number N of random samples between a given interval using my own distribution as fast as possible in python. This is what I mean:

def my_dist(x):     # Some distribution, assume c1,c2,c3 and c4 are known.     f = c1*exp(-((x-c2)**c3)/c4)     return f  # Draw N random samples from my distribution between given limits a,b. N = 1000 N_rand_samples = ran_func_sample(my_dist, a, b, N) 

where ran_func_sample is what I'm after and a, b are the limits from which to draw the samples. Is there anything of that sort in python?

like image 308
Gabriel Avatar asked Jan 13 '14 20:01

Gabriel


People also ask

How do you generate random numbers from an arbitrary distribution?

If we want to generate a random sample according to a distribution F, we can generate a uniform random number on (0,1) and invert it by F. This is due to the fact that, if U is uniform on (0,1), then X=F−1(U) is a random variable that follows F.

What is inverse transformation technique explain?

Inverse transformation sampling takes uniform samples of a number between 0 and 1, interpreted as a probability, and then returns the largest number from the domain of the distribution such that . For example, imagine that. is the standard normal distribution with mean zero and standard deviation one.

Is inverse transform also applicable to generate random variates?

One of the methods that can be used to generate the random variables is the Inverse Transform method. In this article, I will show you how to generate random variables (both discrete and continuous case) using the Inverse Transform method in Python.


1 Answers

You need to use Inverse transform sampling method to get random values distributed according to a law you want. Using this method you can just apply inverted function to random numbers having standard uniform distribution in the interval [0,1].

After you find the inverted function, you get 1000 numbers distributed according to the needed distribution this obvious way:

[inverted_function(random.random()) for x in range(1000)] 

More on Inverse Transform Sampling:

  • http://en.wikipedia.org/wiki/Inverse_transform_sampling

Also, there is a good question on StackOverflow related to the topic:

  • Pythonic way to select list elements with different probability
like image 70
Igor Chubin Avatar answered Oct 08 '22 02:10

Igor Chubin