Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, SimPy: How to generate a value from a triangular probability distribution?

I want to run a simulation that uses as parameter a value generated from a triangular probability distribution with lower limit A, mode B and and upper limit C. How can I generate this value in Python? Is there something as simple as expovariate(lambda) (from random) for this distribution or do I have to code this thing?

like image 997
andandandand Avatar asked May 03 '09 00:05

andandandand


3 Answers

If you download the NumPy package, it has a function numpy.random.triangular(left, mode, right[, size]) that does exactly what you are looking for.

like image 167
erik Avatar answered Sep 20 '22 03:09

erik


Since, I was checking random's documentation from Python 2.4 I missed this:

random.triangular(low, high, mode)¶ Return a random floating point number N such that low <= N <= high and with the specified mode between those bounds. The low and high bounds default to zero and one. The mode argument defaults to the midpoint between the bounds, giving a symmetric distribution. New in version 2.6.

like image 27
andandandand Avatar answered Sep 20 '22 03:09

andandandand


Let's say that your distribution wasn't handled by NumPy or the Python Standard Library.

In situations where performance is not very important, rejection sampling is a useful hack for getting draws from a distribution you don't have using one you do have.

For your triangular distribution, you could do something like

from random import random, uniform

def random_triangular(low, high, mode):
    while True:
        proposal = uniform(low, high)
        if proposal < mode:
            acceptance_prob = (proposal - low) / (mode - low)
        else:
            acceptance_prob = (high - proposal) / (high - mode)
        if random() < acceptance_prob: break
    return proposal

You can plot some samples

pylab.hist([random_triangular(1, 6, 5) for t in range(10000)])

to make sure that everything looks okay.

like image 23
othercriteria Avatar answered Sep 21 '22 03:09

othercriteria