简体   繁体   中英

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!!!

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)

This is, I believe, a gamma distribution, link https://en.wikipedia.org/wiki/Gamma_distribution

You could use function random.gammavariate(alpha, beta) from here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM