3

As I'm fitting a Weibull distribution, and also find in other questions as Fitting distribution with fixed parameters in SciPy

There is a difference between fit using floc=0, and loc=0

weibull_params = 1, 2.0755160030790547, 0, 16.273031221223277
data = sp.stats.exponweib.rvs(*weibull_params, size=50000)
data = data.astype(int)
x = linspace(0, 55)

weibull_params1 = sp.stats.weibull_min.fit(data)
weibull_params2 = sp.stats.weibull_min.fit(data, loc=0)
weibull_params3 = sp.stats.weibull_min.fit(data, floc=0)

for weibull_params, line_style in zip([weibull_params1, weibull_params2, weibull_params3],['-','--','-.']):
    plt.figure()
    plt.hist(data, bins=arange(0,55),alpha=0.5, normed=True)
    y_weibull = sp.stats.weibull_min.pdf(x, *weibull_params)
    plot(x, y_weibull, line_style, color='black') 
    print(weibull_params)

would produce the Weibull like these:

enter image description here

Weibull params:

(0.50240047370945606, -4.501644985259555e-28, 2.9918077253782287)
(2.0610053128948245, -0.45099484072568979, 16.299110670854041) #loc=0
(1.0, 0, 1.05) #floc=0

What is the difference? When should I use which one?

ZK Zhao
  • 19,885
  • 47
  • 132
  • 206

1 Answers1

4

The short answer is: floc (and fscale for that matter) are used to specify that the location parameter (and scale parameter respectively) are to be kept fixed at the specified value. loc and scale merely give starting values for the fit.

sp.stats.weibull_min inherits the fit method from scipy.stat.rv_continuous. The documentation of scipy.stats.rv_continuous.fit specifies the fact, that floc and fscale keep said parameters fixed. loc, scale and other keyword arguments that are recognized by derived distributions are simply used as starting values.

So if you want to fit keeping the location fixed, you should use floc=0 if you only want to provide a starting parameter, you should use loc=0.

jotasi
  • 5,077
  • 2
  • 29
  • 51
  • So it seems like the fitting method for Weibull_min is not very good? As it is the result without `loc=0` is much worse than setting it as the initial value? – ZK Zhao Dec 08 '17 at 15:54