okkhoy
okkhoy

Reputation: 1338

random sampling from a list based on a distribution

I have a list of numbers, and would like to sample from that list. The sampling however is based on some distribution which is specified by a user. I am not sure if alias method (here) using weights is the right one; my limited knowledge says alias method uses uniform distribution alone (correct me on that please!). What if I need to specify something like sample using log-normal as the underlying distribution or poisson for that matter? How do I do this kind of sampling?

A related question, if my list is say [1, 2, 3, 4, 5] and mean is 3, how do I get the sampling?

Upvotes: 3

Views: 2537

Answers (2)

Kenan
Kenan

Reputation: 14094

Have you tried np.random.choice

b = np.random.choice(a=[1,2,3,4,5], p=[0.2, 0.2, 0.2, 0.2, 0.2])

b is randomly sampled from a based on the distribution p

You can also use poisson

Upvotes: 2

soupault
soupault

Reputation: 6510

You can use bunch of numpy functions from here:

http://docs.scipy.org/doc/numpy/reference/routines.random.html

Standard python random library does not provide various distrubutions RNG. And yes, default one is an uniform.

Upvotes: 0

Related Questions