Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Maximum Likelihood Estimation for Beta Distribution (fit_mle) #1267

Merged
merged 1 commit into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/fit.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The `fit_mle` method has been implemented for the following distributions:
**Univariate:**

- [`Bernoulli`](@ref)
- [`Beta`](@ref)
- [`Binomial`](@ref)
- [`Categorical`](@ref)
- [`DiscreteUniform`](@ref)
Expand Down
30 changes: 29 additions & 1 deletion src/univariate/continuous/beta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,36 @@ function rand(rng::AbstractRNG, d::Beta{T}) where T
end

#### Fit model
"""
fit_mle(::Type{<:Beta}, x::AbstractArray{T})

# TODO: add MLE method (should be similar to Dirichlet)
Maximum Likelihood Estimate of `Beta` Distribution via Newton's Method
"""
function fit_mle(::Type{<:Beta}, x::AbstractArray{T};
maxiter::Int=1000, tol::Float64=1e-14) where T<:Real

α₀,β₀ = params(fit(Beta,x)) #initial guess of parameters
g₁ = mean(log.(x))
g₂ = mean(log.(one(T) .- x))
θ= [α₀ ; β₀ ]

converged = false
t=0
while !converged && t < maxiter #newton method
t+=1
temp1 = digamma(θ[1]+θ[2])
temp2 = trigamma(θ[1]+θ[2])
grad = [g₁+temp1-digamma(θ[1])
temp1+g₂-digamma(θ[2])]
hess = [temp2-trigamma(θ[1]) temp2
temp2 temp2-trigamma(θ[2])]
Δθ = hess\grad #newton step
θ .-= Δθ
converged = dot(Δθ,Δθ) < 2*tol #stopping criterion
end

return Beta(θ[1], θ[2])
end

"""
fit(::Type{<:Beta}, x::AbstractArray{T})
Expand Down
6 changes: 6 additions & 0 deletions test/fit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ end
@test isa(d, dist)
@test isapprox(d.α, 1.3, atol=0.1)
@test isapprox(d.β, 3.7, atol=0.1)

d = fit_mle(dist, func[2](dist(1.3, 3.7), N))
@test isa(d, dist)
@test isapprox(d.α, 1.3, atol=0.1)
@test isapprox(d.β, 3.7, atol=0.1)

end
end

Expand Down