|
| 1 | +""" |
| 2 | + LogLogistic(θ, ϕ) |
| 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 |
| 6 | +
|
| 7 | +```math |
| 8 | +f(x; \\theta, \\phi) = \\frac{(\\phi / \\theta)x/\\theta()^(\\phi - 1)}{(1 + (x/\\theta)^\\phi)^2}, \\theta > 0, \\phi > 0 |
| 9 | +``` |
| 10 | +
|
| 11 | +```julia |
| 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 ϕ |
| 15 | +
|
| 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. ϕ |
| 19 | +``` |
| 20 | +
|
| 21 | +External links |
| 22 | +
|
| 23 | +* [Log logistic distribution on Wikipedia](https://en.wikipedia.org/wiki/Log-logistic_distribution) |
| 24 | +
|
| 25 | +""" |
| 26 | + |
| 27 | + |
| 28 | +struct LogLogistic{T<:Real} <: ContinuousUnivariateDistribution |
| 29 | + θ::T |
| 30 | + ϕ::T |
| 31 | + LogLogistic{T}(θ::T,ϕ::T) where {T} = new{T}(θ,ϕ) |
| 32 | +end |
| 33 | + |
| 34 | +function LogLogistic(θ::T, ϕ::T; check_args=true) where {T <: Real} |
| 35 | + check_args && @check_args(LogLogistic, θ > zero(θ) && ϕ > zero(ϕ)) |
| 36 | + return LogLogistic{T}(θ, ϕ) |
| 37 | +end |
| 38 | + |
| 39 | +LogLogistic(θ::Real, ϕ::Real) = LogLogistic(promote(θ,ϕ)...) |
| 40 | +LogLogistic(θ::Integer, ϕ::Integer) = LogLogistic(float(θ), float(ϕ)) |
| 41 | +LogLogistic(θ::T) where {T<:Real} = LogLogistic(θ, 1.0) |
| 42 | +LogLogistic() = LogLogistic(1.0, 1.0, check_args=false) |
| 43 | + |
| 44 | +@distr_support LogLogistic 0.0 Inf |
| 45 | + |
| 46 | +#### 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 | + |
| 50 | +#### Parameters |
| 51 | + |
| 52 | +params(d::LogLogistic) = (d.θ, d.ϕ) |
| 53 | +partype(::LogLogistic{T}) where {T} = T |
| 54 | + |
| 55 | +#### Statistics |
| 56 | + |
| 57 | +median(d::LogLogistic) = d.θ |
| 58 | +function mean(d::LogLogistic) |
| 59 | + if d.ϕ ≤ 1 |
| 60 | + error("mean is defined only when ϕ > 1") |
| 61 | + end |
| 62 | + return d.θ*π/d.ϕ/sin(π/d.ϕ) |
| 63 | +end |
| 64 | + |
| 65 | +function mode(d::LogLogistic) |
| 66 | + if d.ϕ ≤ 1 |
| 67 | + error("mode is defined only when ϕ > 1") |
| 68 | + end |
| 69 | + return d.θ*((d.ϕ-1)/(d.ϕ+1))^(1/d.ϕ) |
| 70 | +end |
| 71 | + |
| 72 | +function var(d::LogLogistic) |
| 73 | + if d.ϕ ≤ 2 |
| 74 | + erros("var is defined only when ϕ > 2") |
| 75 | + end |
| 76 | + b = π/d.ϕ |
| 77 | + return d.θ^2 * (2*b/sin(2*b)-b^2/(sin(b))^2) |
| 78 | +end |
| 79 | + |
| 80 | + |
| 81 | +#### 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 |
| 93 | +end |
| 94 | + |
| 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 |
| 102 | +end |
| 103 | + |
| 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 |
| 110 | +end |
| 111 | + |
| 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 |
| 118 | +end |
| 119 | + |
| 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 |
| 126 | +end |
| 127 | + |
| 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 |
| 134 | +end |
| 135 | + |
| 136 | + |
| 137 | +#### Sampling |
| 138 | +function rand(rng::AbstractRNG, d::LogLogistic) |
| 139 | + u = rand(rng) |
| 140 | + r = u / (1 - u) |
| 141 | + return r^(1/d.ϕ)*d.θ |
| 142 | +end |
0 commit comments