简体   繁体   中英

scipy.stats.rv_continuous.fit - optimizer parameters

Is is possible to manipulate optimizer 's parameters when using scipy.stats.rv_continuous.fit ei relative tolerances?

In R we can use control . How about Python?

fitdist(data, "weibull", method="mle", control=list(reltol=1e-14))$estimate

The optimizer parameter of the fit method allows you to override the default optimization function (which is scipy.optimize.fmin ) and supply your own. The callable that you give as the optimizer argument must have the signature optimize(func, x0, args=(), disp=False) .

To change the default control parameters, you can use a custom optimizer that calls fmin with additional xtol and/or ftol parameters. (Note: You could use a different optimizer instead of fmin .) One that I often use is

from scipy.optimize import fmin


def optimizer(func, x0, args=(), disp=False):
    return fmin(func, x0, args=args, disp=disp, xtol=1e-13, ftol=1e-12)

For example,

from scipy.stats import weibull_min
from scipy.optimize import fmin


def optimizer(func, x0, args=(), disp=False):
    return fmin(func, x0, args=args, disp=disp, xtol=1e-13, ftol=1e-12)


data = [2457.145, 878.081, 855.118, 1157.135, 1099.82]

shape, loc, scale = weibull_min.fit(data, floc=0, optimizer=optimizer)
print(f"shape = {shape:9.7f},  scale={scale:9.7f}")

Output:

shape = 2.3078998,  scale=1463.7713354

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