Search code examples
pythonstatisticsprobabilityprobability-density

How to draw on a user-defined distribution in python?


I need some sample from the distribution p(x)=x*exp(-ax) x>0 but in python, only the exponential distribution is available for sampling. How can I draw on this distribution in python? Thanks!!!


Solution

  • Severin Pappadeux is correct in the sense that you can use random.gammavariate(alpha, beta) in this case. He, however, does not explain the necessary parameters to end up with your distribution.

    Based on the Gamma distribution and your desired distribution, it is not hard to see that in random.gammavariate(alpha, beta), we need to set alpha = 2. Then, the beta parameter can be set to a ** -1. We solely need to multiply by a ** -2 to obtain a value sampled from your desired distribution.

    Full code:

    import random
    
    
    def distr(a):
        return random.gammavariate(2, a ** -1) * (a ** -2)