Skip to content

Commit 3f1a9eb

Browse files
committed
allow missing in fitting expoential distribution
1 parent 5988d8c commit 3f1a9eb

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/univariate/continuous/exponential.jl

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,46 @@ function fit(::Type{Exponential}, m::AbstractMoments)
66
return Exponential(mean(m))
77
end
88

9-
function fit(::Type{Exponential}, lower::QuantilePoint, upper::QuantilePoint)
9+
function fit(::Type{Exponential}, lower::QuantilePoint, upper::Missing)
10+
θ_lower = -lower.q/log(1-lower.p)
11+
Exponential(θ_lower)
12+
end
13+
14+
function fit(::Type{Exponential}, lower::Missing, upper::QuantilePoint)
15+
θ_upper = -upper.q/log(1-upper.p)
16+
Exponential(θ_upper)
17+
end
18+
19+
function fit(dt::Type{Exponential}, lower::QuantilePoint, upper::QuantilePoint)
1020
# return average for the two quantiles
21+
ismissing(lower.q) && return(fit(dt, missing, upper))
22+
ismissing(upper.q) && return(fit(dt, lower, missing))
1123
θ_lower = -lower.q/log(1-lower.p)
1224
θ_upper = -upper.q/log(1-upper.p)
25+
θ_lower θ_upper || @warn("Averaging scale for lower and upper quantile " *
26+
"for fitting expoenential distribution.")
1327
θ = (θ_lower + θ_upper)/2
1428
Exponential(θ)
1529
end
1630

17-
function fit_mean_quantile(::Type{Exponential}, mean::Real, qp::QuantilePoint)
31+
function fit_mean_quantile(dt::Type{Exponential}, mean::Real, qp::QuantilePoint)
1832
# only fit to mean
33+
warning("ignoring upper quantile when fitting Exponential to mean.")
34+
fit_mean_quantile(dt, mean, missing)
35+
end
36+
37+
function fit_mean_quantile(::Type{Exponential}, mean::Real, qp::Missing)
1938
fit(Type{Exponential}, AbstractMoments(mean))
2039
end
2140

22-
function fit_mode_quantile(::Type{Exponential}, mode::Real, qp::QuantilePoint)
41+
function fit_mode_quantile(dt::Type{Exponential}, mode::Real, qp::QuantilePoint)
2342
# ignore mode (its always at 0)
43+
mode != zero(mode) && @warn("ignoring mode when fitting Exponential.")
44+
fit_mode_quantile(dt, missing, qp)
45+
end
46+
47+
48+
function fit_mode_quantile(::Type{Exponential}, mode::Missing, qp::QuantilePoint)
2449
θ = -qp.q/log(1-qp.p)
2550
Exponential(θ)
2651
end

test/exponential.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@ end;
3030
@testset "fit two quantiles" begin
3131
qpl = @qp_m(3)
3232
qpu = @qp_u(5)
33-
d = fit(Exponential, qpl, qpu);
33+
d = @test_logs (:warn,) fit(Exponential, qpl, qpu);
3434
#plot(d);
3535
#plot!(fit_mode_quantile(Exponential, NaN, qpl))
3636
#plot!(fit_mode_quantile(Exponential, NaN, qpu))
3737
#vline!([3,5])
3838
@test quantile(d, qpl.p) < qpl.q
3939
@test quantile(d, qpu.p) > qpu.q
40+
#
41+
d = fit(Exponential, missing, qpu);
42+
@test quantile.(d, [qpu.p]) [qpu.q]
43+
d = fit(Exponential, qpl, missing);
44+
@test quantile.(d, [qpl.p]) [qpl.q]
4045
end;
4146

0 commit comments

Comments
 (0)