简体   繁体   中英

Random numbers from conditional probability distribution in Python

The probability distribution function PDF of a, say, Weibull distribution may look somewhat like the black graph on the following plot.

from scipy.stats import exponweib
import matplotlib.pyplot as plt
import numpy as np

def condwbull_pdf(x, k, lmb, cond):
    return (x >= cond) * exponweib.pdf(x, 1, k, scale=lmb, loc=0) / exponweib.sf(cond, 1, k, scale=lmb, loc=0)

k = 5
lmb = 100.
cond = 100
x = np.linspace(0, 200, 100)
plt.plot(x, exponweib.pdf(x, 1, k, scale=lmb, loc=0), 'k')
plt.plot(x, condwbull_pdf(x, k, lmb, cond), 'r')
plt.show()

PDF文件

The red graph illustrates a conditional probability distribution for a condition that x = 100. See condwbull_pdf() .

Normally to sample random numbers from the above Weibull distribution I could do:

import random
random.weibullvariate(lmb, k)

Now, I would like to draw random numbers from the conditional function. One way to do that would be:

def cond_rnd(lmb, k, cond):
    stop = 0
    while stop < cond:
        stop = random.weibullvariate(lmb, k)
    return stop

However, this becomes extremely inefficient for large conditional values. Can you think of something more elegant / faster?

In general, there is no universally applicable method for sampling efficiently from the conditional distribution of an arbitrary random variable. However, since the distribution function of the Weibull distribution is known analytically, you can use the method of Inverse transform sampling for your specific example.

More precisely, you could compute cond_rnd(lmb, k, cond) using the transformation lmb*(-log (sf*u))**(1.0/k) , where sf=exponweib.sf(cond, 1, k, scale=lmb, loc=0) and u=uniform(0,1)

For accessible treatment of general rare-event simulation techniques, you might have a look at http://onlinelibrary.wiley.com/doi/10.1002/9781118014967.ch10/summary

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