Random generating numbers for a given ratio

I need to generate numbers on a positive given interval (a, b), distributed over an exponential distribution. Using the Inverse CDF method , I created an exponential number generator. But, of course, this number is a positive number, and I want it to be in this interval. What to do to generate only on an interval?

Code for generating a number exponentially distributed using the inverse cdf method in Python

u = random.uniform(0,1)
return (-1/L)*math.log(u)

where L is a given positive parameter.

Thanks in advance

+4
source share
1 answer

x L exp (-Lx). , [a, b], x [a, b] 1/ CDF, a b: a b (L exp ( -Lt) dt) = - (exp (-Lb) - exp (-La)).

, pdf x

L exp (-Lx))/(exp (-La) - exp (-Lb),

cdf x

a x [L exp (-Lt)/(exp (-La) - exp (-Lb)) dt]

= [-exp (-Lx) + exp (-La)]/[exp (-La) - exp (-Lb)] = u

:

exp (-Lx) = exp (-La) - u [exp (-La) - exp (-Lb)]

-Lx = -La + log (1 - u [1 - exp (-Lb)/exp (-La)])

x = a + (-1/L) log (1 - u [1 - exp (-Lb)/exp (-La)])

:

u = random.uniform(0,1)
return a + (-1/L)*math.log( 1 - u*(1 - math.exp(-L*b)/math.exp(-L*a)) ) 

: L a, math.exp(-L*a) 0, ZeroDivisionError.

+2

Source: https://habr.com/ru/post/1538862/


All Articles