Skip to content
Advertisement

Binomial distributions (Bernoulli trials) with different probabilities

I want to speed up the code below – namely the for loop. Is there a way to do it in numpy?

JavaScript

More explanations – I want to generate N sequences of Bernoulli trials (ie. 0s and 1s, called throws) where each sequence has a mean (probability p) given by values in another array (mu_throws). This can be sampled from a normal distribution or in this case, I took it for simplicity to be a sequence of N=10 numbers from 0 to 1. The approach above works but is slow. I expect N to be at least $10^4$ and total_n_trials then can be anything in the order of hundreds to (tens of) thousands (and this is run several times). I have checked the following post but didn’t find an answer. I also know that numpy random choice can generate multidimensional arrays but I didn’t find a way to set a different set of ps for different rows. Basically getting the same as what I do above just reshaped:

JavaScript
JavaScript

I also thought about this trick but haven’t figured how to use it for Bernoulli trials. Thanks for any tips.

Advertisement

Answer

JavaScript

output:

JavaScript

Basically, what I’m doing is generate random real numbers in the interval [0, 1) and convert them in boolean results in function of a given probability (in mu_throws).

if you compare the mu_throws (actual probability mean) and the probability estimated in all_trials you have:

JavaScript

output:

JavaScript

For N and total_n_trials values from your example the time required on my machine is 0.00014019012451171875 sec, vs 0.0012738704681396484 sec of your loop.

Advertisement