diff --git a/src/univariate/continuous/laplace.jl b/src/univariate/continuous/laplace.jl index ebd3945ea8..3d3a43154d 100644 --- a/src/univariate/continuous/laplace.jl +++ b/src/univariate/continuous/laplace.jl @@ -121,3 +121,27 @@ function fit_mle(::Type{<:Laplace}, x::AbstractArray{<:Real}) xc .= abs.(x .- m) return Laplace(m, mean(xc)) end + +function fit_mle(::Type{<:Laplace}, x::AbstractArray{T}, w::AbstractArray{T}) where {T <: Real} + sp = sortperm(x) + n = length(x) + sw = sum(w) + highsum = sw + lowsum = zero(T) + idx = 0 + for i = 1:n + lowsum += w[sp[i]] + highsum -= w[sp[i]] + if lowsum >= highsum + idx = sp[i] + break + end + end + μ = x[idx] + θ = zero(T) + for i = 1:length(x) + θ += w[i] * abs(x[i] - μ) + end + θ /= sw + return Laplace(μ, θ) +end diff --git a/test/fit.jl b/test/fit.jl index 4cf299c6aa..5652c1be25 100644 --- a/test/fit.jl +++ b/test/fit.jl @@ -362,6 +362,15 @@ end end end +@testset "Testing fit for Laplace with weights" begin + for func in funcs, dist in (Laplace, Laplace{Float64}) + d = fit(dist, func[2](dist(5.0, 3.0), N + 1), ones(Float64, N+1)) + @test isa(d, dist) + @test isapprox(location(d), 5.0, atol=0.01) + @test isapprox(scale(d) , 3.0, atol=0.01) + end +end + @testset "Testing fit for Pareto" begin for func in funcs, dist in (Pareto, Pareto{Float64}) x = func[2](dist(3., 7.), N)