python - How to truncate a numpy/scipy exponential distribution in an efficient way? -
I'm currently building a neurological experiment, in fact, an excitation 3 seconds every x second ( X = inter-trial interval ). I would like to reduce x to ( mean = 2.5 ) and unexpected.
I think that 1 (bottom bound) and 10 (upper limit). I would like the result of the forced exponential distr. 2.5 is expected to expect how can I do this in an efficient manner?
There are two ways to do this:
Firstly, a fast distributed random In order to generate the variable and then limit it to value (1,10)
in [14]: import matplotlib.pyplot plt import scipy as statistics as ss lambda = SciPy's parameterization size = 1000 trc_ex_rv = ss.expon.rvs (scale = lambda, size = size) in 2.5 #expected means of exponential distribution is trc_ex_rv = trc_ex_rv [(trc_ex_rv> 1) lambda & amp; (Trc_ex_rv & lt; 10]] in [15]: plt.hist (trc_ex_rv) plt.xlim (0, 12) outside [15]: (0, 12)
in [16]: trc_ex_rv out [16]: Array ([...]) # Many numbers Of course, the problem is that you do not get the exact number of random numbers (defined by size )
The second way to do this is to use it, and you will find the eX number of replication is specified:
In [17]: NP DEF Import numpy as trunc_exp_rv (lower, high, scale, size): rnd_cdf = np.random.uniform (ss.expon.cdf (x = less, scale = scale), ssexchange cdf (x = hai) , Scale = scale), size = size) returns in ss.expon.ppf (q = rnd_cdf, scale = scale) [18]: plt.hist (trunc_exp_rv (1, 10, lambda, size)) plt.xlim (0 , 12) Out [18]: (0, 12)
You want to mean that the bounded distribution is an expectation of a given value, say that <. Code> 2.5 , you need to solve for the scale parameter that means expectation.
import scipy.optimize so def solve_for_l (lower, high, ept_mean) : A = np.array ([Low, High]) Return 1 / so.fmin (Lambda L: ((np.diff (np.exp (-a * L) * (A * L + 1) / L) / Np.diff (np.exp (-A * L)) - ept_mean ** 2, x0 = 0.5, foo ll_output = false, disp = false) def F (lower, high, ept_mean, size): return trunc_exp_rv (less , High, solve_for_l (lower, high, ept_mean), size) rv_data = F (1, 10, 2.5, 1E5) plt.hist (rv_data, bins = 50) plt.xlim (0, 12) print rv_data.mean () Result:
2.50386617882
Comments
Post a Comment