From 2b00d941009a78ec73dbd74bbe53e719431db411 Mon Sep 17 00:00:00 2001 From: salbert83 Date: Wed, 21 Apr 2021 18:39:14 -0400 Subject: [PATCH] fix fit for Laplace distribution (#1309) Co-authored-by: David Widmann --- Project.toml | 2 +- src/univariate/continuous/laplace.jl | 10 ++++++---- test/fit.jl | 6 +++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Project.toml b/Project.toml index 8f3b50ac91..32ec76710a 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Distributions" uuid = "31c24e10-a181-5473-b8eb-7969acd0382f" authors = ["JuliaStats"] -version = "0.24.17" +version = "0.24.18" [deps] FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b" diff --git a/src/univariate/continuous/laplace.jl b/src/univariate/continuous/laplace.jl index b47fb0cede..ebd3945ea8 100644 --- a/src/univariate/continuous/laplace.jl +++ b/src/univariate/continuous/laplace.jl @@ -114,8 +114,10 @@ rand(rng::AbstractRNG, d::Laplace) = #### Fitting -function fit_mle(::Type{<:Laplace}, x::Array) - xc = copy(x) - a = median!(xc) - Laplace(a, StatsBase.mad!(xc, center=a)) +function fit_mle(::Type{<:Laplace}, x::AbstractArray{<:Real}) + xc = similar(x) + copyto!(xc, x) + m = median!(xc) + xc .= abs.(x .- m) + return Laplace(m, mean(xc)) end diff --git a/test/fit.jl b/test/fit.jl index 54be858878..4cf299c6aa 100644 --- a/test/fit.jl +++ b/test/fit.jl @@ -355,10 +355,10 @@ end @testset "Testing fit for Laplace" begin for func in funcs, dist in (Laplace, Laplace{Float64}) - d = fit(dist, func[2](dist(5.0, 3.0), N)) + d = fit(dist, func[2](dist(5.0, 3.0), N + 1)) @test isa(d, dist) - @test isapprox(location(d), 5.0, atol=0.1) - @test isapprox(scale(d) , 3.0, atol=0.2) + @test isapprox(location(d), 5.0, atol=0.02) + @test isapprox(scale(d) , 3.0, atol=0.02) end end