|
1 | 1 | """ |
2 | | - LogLogistic(θ, ϕ) |
| 2 | + LogLogistic(α, β) |
3 | 3 |
|
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 |
6 | 6 |
|
7 | 7 | ```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 |
9 | 9 | ``` |
10 | 10 |
|
11 | 11 | ```julia |
12 | 12 | 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 β |
15 | 14 |
|
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. β |
19 | 18 | ``` |
20 | 19 |
|
21 | 20 | External links |
22 | 21 |
|
23 | 22 | * [Log logistic distribution on Wikipedia](https://en.wikipedia.org/wiki/Log-logistic_distribution) |
24 | | -
|
25 | 23 | """ |
26 | 24 |
|
27 | | - |
28 | 25 | 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}(α,β) |
32 | 29 | end |
33 | 30 |
|
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}(α, β) |
37 | 34 | end |
38 | 35 |
|
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(β)) |
42 | 38 | LogLogistic() = LogLogistic(1.0, 1.0, check_args=false) |
43 | 39 |
|
44 | 40 | @distr_support LogLogistic 0.0 Inf |
45 | 41 |
|
46 | 42 | #### 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.β)) |
50 | 45 | #### Parameters |
51 | 46 |
|
52 | | -params(d::LogLogistic) = (d.θ, d.ϕ) |
| 47 | +params(d::LogLogistic) = (d.α, d.β) |
53 | 48 | partype(::LogLogistic{T}) where {T} = T |
54 | 49 |
|
55 | 50 | #### Statistics |
56 | 51 |
|
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") |
61 | 56 | end |
62 | | - return d.θ*π/d.ϕ/sin(π/d.ϕ) |
| 57 | + return d.α*π/d.β/sin(π/d.β) |
63 | 58 | end |
64 | 59 |
|
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") |
68 | 63 | end |
69 | | - return d.θ*((d.ϕ-1)/(d.ϕ+1))^(1/d.ϕ) |
| 64 | + return d.α*((d.β-1)/(d.β+1))^(1/d.β) |
70 | 65 | end |
71 | 66 |
|
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") |
75 | 70 | 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) |
78 | 73 | end |
79 | 74 |
|
80 | 75 |
|
81 | 76 | #### 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) |
93 | 83 | end |
94 | 84 |
|
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) |
102 | 87 | end |
103 | 88 |
|
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) |
110 | 91 | end |
111 | 92 |
|
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) |
118 | 95 | end |
119 | 96 |
|
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) |
126 | 99 | end |
127 | 100 |
|
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) |
134 | 103 | end |
135 | 104 |
|
136 | 105 |
|
137 | 106 | #### Sampling |
138 | 107 | function rand(rng::AbstractRNG, d::LogLogistic) |
139 | 108 | u = rand(rng) |
140 | 109 | r = u / (1 - u) |
141 | | - return r^(1/d.ϕ)*d.θ |
| 110 | + return r^(1/d.β)*d.α |
142 | 111 | end |
0 commit comments