Fitting a Weibull distribution to values

Hello again

I would like to fit a set of values to a Weibull distribution but the fitting is still not implemented in the Distributions.jl package for this type of Distribution https://juliastats.org/Distributions.jl/stable/fit/.

Any suggestions?

Here’s what I hacked together when I needed it a couple weeks ago. Feel free to use it under CC0:

using Distributions, Roots

function implicit_k_estimator(x, k::Number)
    xᵏ = x.^k
    lnx = log.(x)
    sum(xᵏ .* lnx) / sum(xᵏ) - inv(k) - sum(lnx) / length(x)
end#function

explicit_λ_estimator(k::Number, x) = (sum(x.^k) / length(x))^(1/k)

function Distributions.fit_mle(::Type{Weibull}, x::AbstractArray)
    f = k -> implicit_k_estimator(x, k)
    k = find_zero(f, oneunit(eltype(x)), Order2())
    λ = explicit_λ_estimator(k, x)
    Weibull(k, λ)
end#function

No guarantees about correctness of course; I followed the section on
estimators from the Weibull Wikipedia article, and it worked for my
purposes. After defining the above functions, the fit function
should work for Weibull distribution.

2 Likes

Thanks, I´ll give it a try.