0

In a simulation that I am running I have to draw many many values from the same beta distribution. Currently, I am using

import random

...

for i in range(n_Aa):
   h = random.betavariate(a, b) // With some values for 'a' and 'b'

...

This code, however, is very slow. I think it is because the beta distribution is evaluated over and over again, when it could just be evaluated once since it does not change over the course of the simulation. Is there some way of achieving this?

  • 1
    Why dont you move it to before the for loop? Or, do you want to cache the values for every a` and `b` the function is called for ? – UltraInstinct Jul 06 '16 at 16:34

1 Answers1

3

You can try numpy's random.beta. It seems to be a lot faster:

import random
import numpy as np
n = 10**6

%timeit [random.betavariate(2, 3) for _ in range(n)]
1 loop, best of 3: 3.83 s per loop

%timeit np.random.beta(2, 3, n)
10 loops, best of 3: 99.7 ms per loop
ayhan
  • 70,170
  • 20
  • 182
  • 203
  • Wonderful. Exactly what I was looking for. Thanks. – Reza Rohani Jul 07 '16 at 22:13
  • Hi @RezaRohani if this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This __indicates to the wider community that you've found a solution__ and gives some reputation to both the answerer and yourself. There is no obligation to do this. – OriolAbril Apr 15 '18 at 22:23