Skip to content

Commit bf205c5

Browse files
committed
resolve issues
1 parent 8f927ce commit bf205c5

File tree

1 file changed

+50
-81
lines changed

1 file changed

+50
-81
lines changed
Lines changed: 50 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,111 @@
11
"""
2-
LogLogistic(θ, ϕ)
2+
LogLogistic(α, β)
33
4-
The *log logistic distribution* with scale `θ` and shape `ϕ` is the distribution of a random variable whose logarithm has a [`Logistic`](@ref) distribution.
5-
If ``X \\sim \\operatorname{Logistic}(\\theta, \\phi)`` then ``exp(X) \\sim \\operatorname{LogLogistic}(\\theta, \\phi)``. The probability density function is
4+
The *log logistic distribution* with scale `α` and shape `β` is the distribution of a random variable whose logarithm has a [`Logistic`](@ref) distribution.
5+
If ``X \\sim \\operatorname{LogLogistic}(\\alpha, \\beta)`` then ``log(X) \\sim \\operatorname{Logistic}(log(\\alpha), 1/\\beta)``. The probability density function is
66
77
```math
8-
f(x; \\theta, \\phi) = \\frac{(\\phi / \\theta)x/\\theta()^(\\phi - 1)}{(1 + (x/\\theta)^\\phi)^2}, \\theta > 0, \\phi > 0
8+
f(x; \\alpha, \\beta) = \\frac{(\\alpha / \\beta)x/\\beta()^(\\alpha - 1)}{(1 + (x/\\beta)^\\alpha)^2}, \\beta > 0, \\alpha > 0
99
```
1010
1111
```julia
1212
LogLogistic() # Log-logistic distribution with unit scale and unit shape
13-
LogLogistic(θ) # Log-logistic distribution with scale θ and unit shape
14-
LogLogistic(θ,ϕ) # Log-logistic distribution with scale θ and shape ϕ
13+
LogLogistic(α,β) # Log-logistic distribution with scale α and shape β
1514
16-
params(d) # Get the parameters, i.e. (θ, ϕ)
17-
scale(d) # Get the scale parameter, i.e. θ
18-
shape(d) # Get the shape parameter, i.e. ϕ
15+
params(d) # Get the parameters, i.e. (α, β)
16+
scale(d) # Get the scale parameter, i.e. α
17+
shape(d) # Get the shape parameter, i.e. β
1918
```
2019
2120
External links
2221
2322
* [Log logistic distribution on Wikipedia](https://en.wikipedia.org/wiki/Log-logistic_distribution)
24-
2523
"""
2624

27-
2825
struct LogLogistic{T<:Real} <: ContinuousUnivariateDistribution
29-
θ::T
30-
ϕ::T
31-
LogLogistic{T}(θ::T,ϕ::T) where {T} = new{T}(θ,ϕ)
26+
α::T
27+
β::T
28+
LogLogistic{T}(α::T,β::T) where {T} = new{T}(α,β)
3229
end
3330

34-
function LogLogistic(θ::T, ϕ::T; check_args=true) where {T <: Real}
35-
check_args && @check_args(LogLogistic, θ > zero(θ) && ϕ > zero(ϕ))
36-
return LogLogistic{T}(θ, ϕ)
31+
function LogLogistic(α::T, β::T; check_args=true) where {T <: Real}
32+
check_args && @check_args(LogLogistic, α > zero(α) && β > zero(β))
33+
return LogLogistic{T}(α, β)
3734
end
3835

39-
LogLogistic::Real, ϕ::Real) = LogLogistic(promote(θ,ϕ)...)
40-
LogLogistic::Integer, ϕ::Integer) = LogLogistic(float(θ), float(ϕ))
41-
LogLogistic::T) where {T<:Real} = LogLogistic(θ, 1.0)
36+
LogLogistic::Real, β::Real) = LogLogistic(promote(α,β)...)
37+
LogLogistic::Integer, β::Integer) = LogLogistic(float(α), float(β))
4238
LogLogistic() = LogLogistic(1.0, 1.0, check_args=false)
4339

4440
@distr_support LogLogistic 0.0 Inf
4541

4642
#### Coversions
47-
convert(::Type{LogLogistic{T}}, θ::S, ϕ::S) where {T <: Real, S <: Real} = LogLogistic(T(θ), T(ϕ))
48-
convert(::Type{LogLogistic{T}}, d::LogLogistic{S}) where {T <: Real, S <: Real} = LogLogistic(T(d.θ), T(d.ϕ), check_args=false)
49-
43+
convert(::Type{LogLogistic{T}}, d::LogLogistic{T}) where {T<:Real} = d
44+
convert(::Type{LogLogistic{T}}, d::LogLogistic) where {T<:Real} = LogLogistic{T}(T(d.α), T(d.β))
5045
#### Parameters
5146

52-
params(d::LogLogistic) = (d.θ, d.ϕ)
47+
params(d::LogLogistic) = (d.α, d.β)
5348
partype(::LogLogistic{T}) where {T} = T
5449

5550
#### Statistics
5651

57-
median(d::LogLogistic) = d.θ
58-
function mean(d::LogLogistic)
59-
if d.ϕ 1
60-
error("mean is defined only when ϕ > 1")
52+
median(d::LogLogistic) = d.α
53+
function mean(d::LogLogistic{T}) where T<:Real
54+
if d.β 1
55+
ArgumentError("mean is defined only when β > 1")
6156
end
62-
return d.θ*π/d.ϕ/sin/d.ϕ)
57+
return d.α*π/d.β/sin/d.β)
6358
end
6459

65-
function mode(d::LogLogistic)
66-
if d.ϕ 1
67-
error("mode is defined only when ϕ > 1")
60+
function mode(d::LogLogistic{T}) where T<:Real
61+
if d.β 1
62+
ArgumentError("mode is defined only when β > 1")
6863
end
69-
return d.θ*((d.ϕ-1)/(d.ϕ+1))^(1/d.ϕ)
64+
return d.α*((d.β-1)/(d.β+1))^(1/d.β)
7065
end
7166

72-
function var(d::LogLogistic)
73-
if d.ϕ 2
74-
erros("var is defined only when ϕ > 2")
67+
function var(d::LogLogistic{T}) where T<:Real
68+
if d.β 2
69+
ArgumentError("var is defined only when β > 2")
7570
end
76-
b = π/d.ϕ
77-
return d.θ^2 * (2*b/sin(2*b)-b^2/(sin(b))^2)
71+
b = π/d.β
72+
return d.α^2 * (2*b/sin(2*b)-b^2/(sin(b))^2)
7873
end
7974

8075

8176
#### Evaluation
82-
function pdf(d::LogLogistic, x::Real)
83-
if x zero(0)
84-
z = zero(x)
85-
else
86-
# use built-in impletation to evaluate the density
87-
# of loglogistic at x
88-
# Y = log(X)
89-
# Y ~ logistic(log(θ), 1/ϕ)
90-
z = pdf(Logistic(log(d.θ), 1/d.ϕ), log(x)) / x
91-
end
92-
return z
77+
function pdf(d::LogLogistic{T}, x::Real) where T<:Real
78+
# use built-in impletation to evaluate the density
79+
# of loglogistic at x
80+
# Y = log(X)
81+
# Y ~ logistic(log(θ), 1/ϕ)
82+
x >= 0 ? pdf(Logistic(log(d.α), 1/d.β), log(x)) / x : zero(T)
9383
end
9484

95-
function logpdf(d::LogLogistic, x::Real)
96-
if x zero(0)
97-
z = log(zero(x))
98-
else
99-
z = logpdf(Logistic(log(d.θ), 1/d.ϕ), log(x)) + log(x)
100-
end
101-
return z
85+
function logpdf(d::LogLogistic{T}, x::Real) where T<:Real
86+
x >= 0 ? logpdf(Logistic(log(d.α), 1/d.β), log(x)) + log(x) : -T(Inf)
10287
end
10388

104-
function cdf(d::LogLogistic, x::Real)
105-
if x <= 0
106-
return 0.0
107-
end
108-
z = cdf(Logistic(log(d.θ), 1/d.ϕ), log(x))
109-
return z
89+
function cdf(d::LogLogistic{T}, x::Real) where T<:Real
90+
x >= 0 ? cdf(Logistic(log(d.α), 1/d.β), log(x)) : zero(T)
11091
end
11192

112-
function logcdf(d::LogLogistic, x::Real)
113-
if x <= 0
114-
-Inf
115-
end
116-
z = logcdf(Logistic(log(d.θ), 1/d.ϕ), log(x))
117-
return z
93+
function logcdf(d::LogLogistic{T}, x::Real) where T<:Real
94+
x >= 0 ? logcdf(Logistic(log(d.α), 1/d.β), log(x)) : -T(Inf)
11895
end
11996

120-
function ccdf(d::LogLogistic, x::Real)
121-
if x <= 0
122-
return 1
123-
end
124-
z = ccdf(Logistic(log(d.θ), 1/d.ϕ), log(x))
125-
return z
97+
function ccdf(d::LogLogistic{T}, x::Real) where T<:Real
98+
x >= 0 ? ccdf(Logistic(log(d.α), 1/d.β), log(x)) : one(T)
12699
end
127100

128-
function logccdf(d::LogLogistic, x::Real)
129-
if x <= 0
130-
return 0.0
131-
end
132-
z = logccdf(Logistic(log(d.θ), 1/d.ϕ), log(x))
133-
return z
101+
function logccdf(d::LogLogistic{T}, x::Real) where T<:Real
102+
x >= 0 ? logccdf(Logistic(log(d.α), 1/d.β), log(x)) : zero(T)
134103
end
135104

136105

137106
#### Sampling
138107
function rand(rng::AbstractRNG, d::LogLogistic)
139108
u = rand(rng)
140109
r = u / (1 - u)
141-
return r^(1/d.ϕ)*d.θ
110+
return r^(1/d.β)*d.α
142111
end

0 commit comments

Comments
 (0)