Dist_reject¶
-
class
stats.
Dist_reject
(dist_ran, f_fx, f_tot=None)¶ Bases:
stats.Dist_qilum
Distribution with rejection method. Give a distribution with a known method for both random number generation and function value, and apply rejection method to keep values
- Parameters
dist_ran (distribution scipy or qilum.Dist) – we can call a random number generator
rvs()
f_fx (function) – objective
f_tot (double) – Cumulative of the function f_fx() on the whole valid range x, Only used if we want to use this distribution as part of Dist_sum
Examples
>>> import qilum.stats as qs >>> import scipy.stats >>> # gaussian function >>> def f_f(xs): ... return 3.*np.exp(-np.square(xs)/10.) >>> >>> # 1.create a function f_sum(x) above the function f_f(x). >>> # f_sum(x) = y0*exp(+a*x) if x_min < x >>> # f_sum(x) = step function if x_min < x <= x_max >>> # f_sum(x) = y0*exp(-a*x) if x_max < x >>> xs = np.linspace(-5,5, 1001) >>> ys = f_f(xs) >>> xs_inter, ys_inter = qs.f_max(xs, ys, 6) >>> ys_inter *= 1.2 # just to be sure that our step function >= f_f() >>> # histogram distribution with the step function >>> hist_dist = scipy.stats.rv_histogram((ys_inter, xs_inter)) >>> # scale this diribution >>> cumulative = ((np.roll(xs_inter,-1)-xs_inter)[:-1]*ys_inter).sum() >>> step = qs.Dist_scale(hist_dist, scale_y = cumulative) >>> >>> # exponential distribution at the left and right of the step distribution >>> exp_left = qs.Dist_scale(scipy.stats.expon(),loc_x=xs_inter[-1], scale_x=1, scale_y=ys_inter[-1], name='Exp+') >>> exp_right = qs.Dist_scale(scipy.stats.expon(),loc_x=xs_inter[0]-1e-10, scale_x=-1,scale_y=ys_inter[0], name='Exp-') >>> >>> # 2. create a sum of the known distributions for each part of f2 function >>> dist_sum = qs.Dist_sum([exp_left, step, exp_right]) >>> # 3. create the Dist_rejection distribution using this sum and the original f_f(x) function >>> dist = qs.Dist_reject(dist_sum, f_f) >>> >>> # test result with the normal distribution hypothesis >>> k2, p = scipy.stats.normaltest(dist.rvs(100000)) >>> print("Normal distribution test = ",p > 1e-3, " with ", "p = {:g}".format(p)) Normal distribution test = True with p = 0.0617403
Methods Summary
F_tot
()Cumulative of the function f() on the whole valid range x
cdf
(x)Cumulative distribution function.
f
(x)function f(x) set initialy f_fx(x)
f_underlying
(x)function dist_ran.f(x): function of the underlying distribution dist_ran set up initialy
name
()Name of the class
pdf
(x)Probability density function
ppf
(q)Percent point function (inverse of cdf) at q
rvs
(size)random numbers in ndarray of lenght size
rvs_xy
(size)random numbers rans in ndarray of lenght size and function(rans)
Methods Documentation
-
F_tot
()¶ Cumulative of the function f() on the whole valid range x
- Returns
- Return type
int
Examples
>>> # see example at the top of the class >>> print(dist.F_tot()) NotImplementedError: Dist_reject.F_tot: not implemented
-
cdf
(x)¶ Cumulative distribution function.
- Parameters
x (array_like of type(values)) –
- Returns
Cumulative distribution function evaluated at x
- Return type
ndarray
Examples
>>> # see example at the top of the class >>> x = [-3,1,2,3] >>> print('cdf(x)=',dist.cdf(x)) NotImplementedError: Dist_reject.cdf: not implemented
-
f
(x)¶ function f(x) set initialy f_fx(x)
- Parameters
x (array_like of type(values)) –
- Returns
f(x)
- Return type
ndarray
Examples
>>> # see example at the top of the class >>> x = [-3,1,2,3] >>> print('f(x)=',dist.f(x)) f(x)= [1.21970898 2.71451225 2.01096014 1.21970898]
-
f_underlying
(x)¶ function dist_ran.f(x): function of the underlying distribution dist_ran set up initialy
- Parameters
x (array_like of type(values)) –
- Returns
dist.f(x)
- Return type
ndarray
Examples
>>> # see example at the top of the class >>> x = [-3,1,2,3] >>> print('f_underlying(x)=',dist.f_underlying(x)) f_underlying(x)= [2.7056004 3.6 2.75102576 2.75102576] >>> # or similarly >>> print('f_underlying(x)=',dist.dist_ran.f(x)) f_underlying(x)= [2.7056004 3.6 2.75102576 2.75102576]
-
name
()¶ Name of the class
- Returns
‘Dist_reject’
- Return type
string
Examples
>>> # see example at the top of the class >>> dist.name() 'Dist_reject'
-
pdf
(x)¶ Probability density function
- Parameters
x (array_like of type(values)) –
- Returns
f(x)/F_tot()
- Return type
ndarray
Examples
>>> # see example at the top of the class >>> x = [-3,1,2,3] >>> print('pdf(x)=',dist.pdf(x)) NotImplementedError: Dist_reject.F_tot: not implemented
-
ppf
(q)¶ Percent point function (inverse of cdf) at q
- Parameters
q (array_like of double) – lower tail probability
- Returns
quantile corresponding to the lower tail probability q
- Return type
ndarray
Examples
>>> # see example at the top of the class >>> q = [0, 0.3,0.5,0.7,1] >>> print('ppf(q)=',dist.ppf(q)) NotImplementedError: Dist_reject.ppf: not implemented
-
rvs
(size)¶ random numbers in ndarray of lenght size
- Parameters
size (int) – number of random number
- Returns
random numbers
- Return type
ndarray
Examples
>>> # see example at the top of the class >>> print('rans=',dist.rvs(4)) rans= [-1.17789338 -0.69306554 1.1672123 -1.80663854]
-
rvs_xy
(size)¶ random numbers rans in ndarray of lenght size and function(rans)
- Parameters
size (int) – number of random number
- Returns
ndarray – random numbers
ndarray – f(random numbers)
Examples
>>> # see example at the top of the class >>> print('rans=',dist.rvs_xy(2)) rans= (array([-2.46439985, -0.96670812]), array([1.63441612, 2.73234395]))