Skip to content

Commit

Permalink
improvements suggested by David Widmann
Browse files Browse the repository at this point in the history
  • Loading branch information
salbert83 committed Apr 21, 2021
1 parent 059a237 commit 857debf
Showing 1 changed file with 6 additions and 32 deletions.
38 changes: 6 additions & 32 deletions src/univariate/continuous/laplace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,36 +114,10 @@ rand(rng::AbstractRNG, d::Laplace) =

#### Fitting

function fit_mle(::Type{<:Laplace}, x::AbstractArray{T}) where {T<:Real}
μ = median(x)
θ = zero(T)
for v x
θ += abs(v - μ)
end
θ /= length(x)
return Laplace(μ, θ)
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(μ, θ)
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

0 comments on commit 857debf

Please sign in to comment.