Skip to content
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ Currently, support for the following distributios are implemented:
- Normal
- Lognormal
- Logitnormal
- Exponential

See [Documentation](https://bgctw.github.io/DistributionFits.jl/dev)
26 changes: 26 additions & 0 deletions src/univariate/continuous/exponential.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function fit(::Type{Exponential}, m::AbstractMoments)
# https://en.wikipedia.org/wiki/Exponential_distribution
n_moments(m) >= 1 || error("Need mean to estimate exponential")
return Exponential(mean(m))
end

function fit(::Type{Exponential}, lower::QuantilePoint, upper::QuantilePoint)
# return average for the two quantiles
θ_lower = -lower.q/log(1-lower.p)
θ_upper = -upper.q/log(1-upper.p)
θ = (θ_lower + θ_upper)/2
Exponential(θ)
end

function fit_mean_quantile(::Type{Exponential}, mean::Real, qp::QuantilePoint)
# only fit to mean
fit(Type{Exponential}, AbstractMoments(mean))
end

function fit_mode_quantile(::Type{Exponential}, mode::Real, qp::QuantilePoint)
# ignore mode (its always at 0)
θ = -qp.q/log(1-qp.p)
Exponential(θ)
end


2 changes: 1 addition & 1 deletion src/univariates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const continuous_distributions = [
# "chi",
# "cosine",
# "epanechnikov",
# "exponential",
"exponential",
# "fdist",
# "frechet",
# "gamma", "erlang",
Expand Down
41 changes: 41 additions & 0 deletions test/exponential.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

@testset "fit moments" begin
D = Exponential(2.1)
M = Moments(mean(D))
Dfit = fit(Exponential, M)
@test D ≈ Dfit
# handle not given any moment
@test_throws Exception fit(Exponential, Moments())
end;

@testset "fit to quantilepoint and mode - ignore mode" begin
d = Exponential(0.8)
qp = @qp(quantile(d,0.95),0.95)
dfit = fit_mode_quantile(Exponential, mode(d), qp)
@test dfit ≈ d
dfit = fit(Exponential, mode(d), qp, Val(:mode))
@test dfit ≈ d
# with lower quantile
qp = @qp(quantile(d,0.025),0.025)
dfit = fit_mode_quantile(Exponential, mode(d), qp)
@test dfit ≈ d
end;

@testset "fit two quantiles same" begin
qpl = @qp_m(3)
d = fit(Exponential, qpl, qpl);
@test quantile.(d, [qpl.p]) ≈ [qpl.q]
end;

@testset "fit two quantiles" begin
qpl = @qp_m(3)
qpu = @qp_u(5)
d = fit(Exponential, qpl, qpu);
#plot(d);
#plot!(fit_mode_quantile(Exponential, NaN, qpl))
#plot!(fit_mode_quantile(Exponential, NaN, qpu))
#vline!([3,5])
@test quantile(d, qpl.p) < qpl.q
@test quantile(d, qpu.p) > qpu.q
end;

1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const tests = [
"normal",
"lognormal",
"logitnormal",
"exponential",
]
#tests = ["logitnormal"]

Expand Down