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 docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ makedocs(;
"Dependencies" => "set_optimize.md",
"LogNormal" => "lognormal.md",
"LogitNormal" => "logitnormal.md",
"Weibull" => "weibull.md",
#"Details" => "z_autodocs.md",
],
)
Expand Down
18 changes: 18 additions & 0 deletions docs/src/weibull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
```@meta
CurrentModule = DistributionFits
```

# Weibull distribution

The Weibull distribution has a scale and a shape parameter.
It can be fitted using two quantiles.

```jldoctest; output = false, setup = :(using DistributionFits,Optim)
d = fit(Weibull, @qp_m(0.5), @qp_uu(5));
median(d) == 0.5 && quantile(d, 0.975) ≈ 5
# output
true
```

Fitting by mean or mode is not yet implemented.

4 changes: 2 additions & 2 deletions src/univariate/continuous/exponential.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


function fit(::Type{Exponential}, m::AbstractMoments)
# https://en.wikipedia.org/wiki/Exponential_distribution
n_moments(m) >= 1 || error("Need mean to estimate exponential")
Expand All @@ -22,5 +24,3 @@ function fit_mode_quantile(::Type{Exponential}, mode::Real, qp::QuantilePoint)
θ = -qp.q/log(1-qp.p)
Exponential(θ)
end


25 changes: 25 additions & 0 deletions src/univariate/continuous/weibull.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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{Weibull}, lower::QuantilePoint, upper::QuantilePoint)
# https://www.johndcook.com/quantiles_parameters.pdf
gamma = (log(-log(1-upper.p)) - log(-log(1-lower.p)))/(log(upper.q) -log(lower.q))
beta = lower.q / (-log(1-lower.p))^(1/gamma)
Weibull(gamma, beta)
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 @@ -66,7 +66,7 @@ const continuous_distributions = [
# "triweight",
# "uniform",
# "vonmises",
# "weibull"
"weibull"
]

for dname in discrete_distributions
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const tests = [
"lognormal",
"logitnormal",
"exponential",
"weibull",
]
#tests = ["logitnormal"]

Expand Down
27 changes: 27 additions & 0 deletions test/weibull.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

# @testset "fit moments" begin
# end;

# @testset "fit to quantilepoint and mode - ignore mode" begin
# end;

# @testset "fit two quantiles same" begin
# end;

@testset "fit two quantiles" begin
D = Weibull(1,1)
#Plots.plot(D)
median(D)
qpl = @qp_m(median(D))
qpu = @qp_u(quantile(D, 0.95))
d = fit(Weibull, qpl, qpu);
@test d == D
#
qpl = @qp_m(0.5)
qpu = @qp_uu(5)
d = fit(Weibull, qpl, qpu);
@test median(d) == 0.5
@test quantile(d, 0.975) ≈ 5
#Plots.plot(d);
end;