11"""
22 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.
4+ The *log- logistic distribution* with scale `α` and shape `β` is the distribution of a random variable whose logarithm has a [`Logistic`](@ref) distribution.
55If ``X \\ sim \\ operatorname{LogLogistic}(\\ alpha, \\ beta)`` then ``log(X) \\ sim \\ operatorname{Logistic}(log(\\ alpha), 1/\\ beta)``. The probability density function is
66
77```math
@@ -21,7 +21,6 @@ External links
2121
2222* [Log logistic distribution on Wikipedia](https://en.wikipedia.org/wiki/Log-logistic_distribution)
2323"""
24-
2524struct LogLogistic{T<: Real } <: ContinuousUnivariateDistribution
2625 α:: T
2726 β:: T
@@ -42,70 +41,81 @@ LogLogistic() = LogLogistic(1.0, 1.0, check_args=false)
4241# ### Coversions
4342convert (:: Type{LogLogistic{T}} , d:: LogLogistic{T} ) where {T<: Real } = d
4443convert (:: Type{LogLogistic{T}} , d:: LogLogistic ) where {T<: Real } = LogLogistic {T} (T (d. α), T (d. β))
45- # ### Parameters
4644
45+ # ### Parameters
4746params (d:: LogLogistic ) = (d. α, d. β)
4847partype (:: LogLogistic{T} ) where {T} = T
4948
5049# ### Statistics
5150
5251median (d:: LogLogistic ) = d. α
53- function mean (d:: LogLogistic{T} ) where T<: Real
54- if d. β ≤ 1
55- ArgumentError (" mean is defined only when β > 1" )
52+ function mean (d:: LogLogistic )
53+ (; α, β) = d
54+ if ! (β > 1 )
55+ ArgumentError (" the mean of a log-logistic distribution is defined only when its shape β > 1" )
5656 end
57- return d . α/ sinc (1 / d . β )
57+ return α/ sinc (inv (β) )
5858end
5959
60- function mode (d:: LogLogistic{T} ) where T<: Real
61- if d. β ≤ 1
62- ArgumentError (" mode is defined only when β > 1" )
63- end
64- return d. α* ((d. β- 1 )/ (d. β+ 1 ))^ (1 / d. β)
60+ function mode (d:: LogLogistic )
61+ (; α, β) = d
62+ return α* (max (β - 1 , 0 ) / (β + 1 ))^ inv (β)
6563end
6664
67- function var (d:: LogLogistic{T} ) where T<: Real
68- if d. β ≤ 2
69- ArgumentError (" var is defined only when β > 2" )
65+ function var (d:: LogLogistic )
66+ (; α, β) = d
67+ if ! (β > 2 )
68+ ArgumentError (" the variance of a log-logistic distribution is defined only when its shape β > 2" )
7069 end
71- b = π / d . β
72- return d . α^ 2 * (2 * b / sin ( 2 * b) - b ^ 2 / ( sin (b ))^ 2 )
70+ invβ = inv (β)
71+ return α^ 2 * (inv ( sinc ( 2 * invβ)) - inv ( sinc (invβ ))^ 2 )
7372end
7473
74+ entropy (d:: LogLogistic ) = log (d. α / d. β) + 2
7575
7676# ### Evaluation
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)
83- end
8477
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 )
78+ function pdf (d:: LogLogistic , x:: Real )
79+ (; α, β) = d
80+ insupport = x > 0
81+ _x = insupport ? x : zero (x)
82+ xoαβ = (_x / α)^ β
83+ res = (β / x) / ((1 + xoαβ) * (1 + inv (xoαβ)))
84+ return insupport ? res : zero (res)
8785end
88-
89- function cdf (d:: LogLogistic{T} , x:: Real ) where T<: Real
90- x >= 0 ? cdf (Logistic (log (d. α), 1 / d. β), log (x)) : zero (T)
86+ function logpdf (d:: LogLogistic , x:: Real )
87+ (; α, β) = d
88+ insupport = x > 0
89+ _x = insupport ? x : zero (x)
90+ βlogxoα = β * log (_x / α)
91+ res = log (β / x) - (log1pexp (βlogxoα) + log1mexp (βlogxoα))
92+ return insupport ? res : oftype (res, - Inf )
9193end
9294
93- function logcdf (d:: LogLogistic{T} , x:: Real ) where T<: Real
94- x >= 0 ? logcdf (Logistic (log (d. α), 1 / d. β), log (x)) : - T (Inf )
95- end
95+ cdf (d:: LogLogistic , x:: Real ) = inv (1 + (max (x, 0 ) / d. α)^ (- d. β))
96+ ccdf (d:: LogLogistic , x:: Real ) = inv (1 + (max (x, 0 ) / d. α)^ d. β)
9697
97- function ccdf (d:: LogLogistic{T} , x:: Real ) where T<: Real
98- x >= 0 ? ccdf (Logistic (log (d. α), 1 / d. β), log (x)) : one (T)
99- end
98+ logcdf (d:: LogLogistic , x:: Real ) = - log1pexp (- d. β * log (max (x, 0 ) / d. α))
99+ logccdf (d:: LogLogistic , x:: Real ) = - log1pexp (d. β * log (max (x, 0 ) / d. α))
100100
101- function logccdf (d:: LogLogistic{T} , x:: Real ) where T<: Real
102- x >= 0 ? logccdf (Logistic (log (d. α), 1 / d. β), log (x)) : zero (T)
103- end
101+ quantile (d:: LogLogistic , p:: Real ) = d. α * (p / (1 - p))^ inv (d. β)
102+ cquantile (d:: LogLogistic , p:: Real ) = d. α * ((1 - p) / p)^ inv (d. β)
104103
104+ invlogcdf (d:: LogLogistic , lp:: Real ) = d. α * expm1 (- lp)^ (- inv (d. β))
105+ invlogccdf (d:: LogLogistic , lp:: Real ) = d. α * expm1 (- lp)^ inv (d. β)
105106
106107# ### Sampling
108+
107109function rand (rng:: AbstractRNG , d:: LogLogistic )
108110 u = rand (rng)
109- r = u / (1 - u)
110- return r^ (1 / d. β)* d. α
111+ return d. α * (u / (1 - u))^ (inv (d. β))
112+ end
113+ function rand! (rng:: AbstractRNG , d:: LogLogistic , A:: AbstractArray{<:Real} )
114+ rand! (rng, A)
115+ let α = d. α, invβ = inv (d. β)
116+ map! (A, A) do u
117+ return α * (u / (1 - u))^ invβ
118+ end
119+ end
120+ return A
111121end
0 commit comments