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()
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?
Aucun commentaire:
Enregistrer un commentaire