-
Notifications
You must be signed in to change notification settings - Fork 431
Add log-logistic distribution #2008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devmotion
wants to merge
15
commits into
master
Choose a base branch
from
dmw/loglogistic
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8f927ce
add loglogistic distribution
yunzli bf205c5
resolve issues
yunzli bcc7819
using sinc
yunzli 025c8a8
remove tests with a single argument
yunzli 15333a9
Merge branch 'master' into loglogistic_distribution
devmotion c82b8b8
Fix docs
devmotion 4afe626
Simplify test/runtests.jl diff
devmotion 28d0b37
Fixes
devmotion 864a498
More tests
devmotion 6b2e02c
More fixes and tests
devmotion 4a5f3d5
More tests
devmotion f50f5a6
Add tests against R (VGAM)
devmotion c7e2e4b
Fix docstring
devmotion f0b84b6
Merge branch 'master' into dmw/loglogistic
devmotion 4d24b07
Update version to 0.25.123
devmotion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| [deps] | ||
| Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" | ||
| Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" | ||
| GR = "28b8d3ca-fb5f-59d9-8090-bfdbd6d07a71" | ||
|
|
||
| [compat] | ||
| Documenter = "1" | ||
| GR = "0.72.1, 0.73" | ||
|
|
||
| [sources.Distributions] | ||
| path = ".." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| """ | ||
| LogLogistic(α, β) | ||
|
|
||
| The *log-logistic distribution* with scale ``\\alpha`` and shape ``\\beta`` is the distribution of a random variable whose logarithm has a [`Logistic`](@ref) distribution. | ||
|
|
||
| If ``X \\sim \\operatorname{LogLogistic}(\\alpha, \\beta)`` then ``\\log(X) \\sim \\operatorname{Logistic}(\\log(\\alpha), 1/\\beta)``. | ||
| The probability density function is | ||
|
|
||
| ```math | ||
| f(x; \\alpha, \\beta) = \\frac{(\\beta / \\alpha){(x/\\alpha)}^{\\beta - 1}}{{(1 + {(x/\\alpha)}^\\beta)}^2}, \\qquad \\alpha > 0, \\beta > 0. | ||
| ``` | ||
|
|
||
| ```julia | ||
| LogLogistic(α, β) # Log-logistic distribution with scale α and shape β | ||
|
|
||
| params(d) # Get the parameters, i.e. (α, β) | ||
| scale(d) # Get the scale parameter, i.e. α | ||
| shape(d) # Get the shape parameter, i.e. β | ||
| ``` | ||
|
|
||
| External links | ||
|
|
||
| * [Log-logistic distribution on Wikipedia](https://en.wikipedia.org/wiki/Log-logistic_distribution) | ||
| """ | ||
| struct LogLogistic{T<:Real} <: ContinuousUnivariateDistribution | ||
| α::T | ||
| β::T | ||
| LogLogistic{T}(α::T,β::T) where {T} = new{T}(α,β) | ||
| end | ||
|
|
||
| function LogLogistic(α::T, β::T; check_args::Bool=true) where {T <: Real} | ||
| check_args && @check_args(LogLogistic, α > zero(α) && β > zero(β)) | ||
| return LogLogistic{T}(α, β) | ||
| end | ||
|
|
||
| LogLogistic(α::Real, β::Real; check_args::Bool = true) = LogLogistic(promote(α, β)...; check_args) | ||
|
|
||
| @distr_support LogLogistic 0.0 Inf | ||
|
|
||
| #### Coversions | ||
| convert(::Type{LogLogistic{T}}, d::LogLogistic{T}) where {T<:Real} = d | ||
| convert(::Type{LogLogistic{T}}, d::LogLogistic) where {T<:Real} = LogLogistic{T}(T(d.α), T(d.β)) | ||
|
|
||
| #### Parameters | ||
| params(d::LogLogistic) = (d.α, d.β) | ||
| partype(::LogLogistic{T}) where {T} = T | ||
|
|
||
| #### Statistics | ||
|
|
||
| median(d::LogLogistic) = d.α | ||
| function mean(d::LogLogistic) | ||
| (; α, β) = d | ||
| if !(β > 1) | ||
| throw(ArgumentError("the mean of a log-logistic distribution is defined only when its shape β > 1")) | ||
| end | ||
| return α/sinc(inv(β)) | ||
| end | ||
|
|
||
| function mode(d::LogLogistic) | ||
| (; α, β) = d | ||
| return α*(max(β - 1, 0) / (β + 1))^inv(β) | ||
| end | ||
|
|
||
| function var(d::LogLogistic) | ||
| (; α, β) = d | ||
| if !(β > 2) | ||
| throw(ArgumentError("the variance of a log-logistic distribution is defined only when its shape β > 2")) | ||
| end | ||
| invβ = inv(β) | ||
| return α^2 * (inv(sinc(2 * invβ)) - inv(sinc(invβ))^2) | ||
| end | ||
|
|
||
| entropy(d::LogLogistic) = log(d.α / d.β) + 2 | ||
|
|
||
| #### Evaluation | ||
|
|
||
| function pdf(d::LogLogistic, x::Real) | ||
| (; α, β) = d | ||
| insupport = x > 0 | ||
| _x = insupport ? x : zero(x) | ||
| xoαβ = (_x / α)^β | ||
| res = (β / _x) / ((1 + xoαβ) * (1 + inv(xoαβ))) | ||
| return insupport ? res : zero(res) | ||
| end | ||
| function logpdf(d::LogLogistic, x::Real) | ||
| (; α, β) = d | ||
| insupport = x > 0 | ||
| _x = insupport ? x : zero(x) | ||
| βlogxoα = β * log(_x / α) | ||
| res = log(β / _x) - (log1pexp(βlogxoα) + log1pexp(-βlogxoα)) | ||
| return insupport ? res : oftype(res, -Inf) | ||
| end | ||
|
|
||
| cdf(d::LogLogistic, x::Real) = inv(1 + (max(x, 0) / d.α)^(-d.β)) | ||
| ccdf(d::LogLogistic, x::Real) = inv(1 + (max(x, 0) / d.α)^d.β) | ||
|
|
||
| logcdf(d::LogLogistic, x::Real) = -log1pexp(-d.β * log(max(x, 0) / d.α)) | ||
| logccdf(d::LogLogistic, x::Real) = -log1pexp(d.β * log(max(x, 0) / d.α)) | ||
|
|
||
| quantile(d::LogLogistic, p::Real) = d.α * (p / (1 - p))^inv(d.β) | ||
| cquantile(d::LogLogistic, p::Real) = d.α * ((1 - p) / p)^inv(d.β) | ||
|
|
||
| invlogcdf(d::LogLogistic, lp::Real) = d.α * expm1(-lp)^(-inv(d.β)) | ||
| invlogccdf(d::LogLogistic, lp::Real) = d.α * expm1(-lp)^inv(d.β) | ||
|
|
||
| #### Sampling | ||
|
|
||
| function rand(rng::AbstractRNG, d::LogLogistic) | ||
| u = rand(rng) | ||
| return d.α * (u / (1 - u))^(inv(d.β)) | ||
| end | ||
| function rand!(rng::AbstractRNG, d::LogLogistic, A::AbstractArray{<:Real}) | ||
| rand!(rng, A) | ||
| let α = d.α, invβ = inv(d.β) | ||
| map!(A, A) do u | ||
| return α * (u / (1 - u))^invβ | ||
| end | ||
| end | ||
| return A | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| LogLogistic <- R6Class("LogLogistic", | ||
| inherit = ContinuousDistribution, | ||
| public = list( | ||
| names = c("alpha", "beta"), | ||
| alpha = NA, | ||
| beta = NA, | ||
| initialize = function(a, b) { | ||
| self$alpha <- a | ||
| self$beta <- b | ||
| }, | ||
| supp = function() { c(0, Inf) }, | ||
| properties = function() { }, | ||
| pdf = function(x, log=FALSE) { VGAM::dfisk(x, scale=self$alpha, shape=self$beta, log=log) }, | ||
| cdf = function(x) { VGAM::pfisk(x, scale=self$alpha, shape=self$beta) }, | ||
| quan = function(v) { VGAM::qfisk(v, scale=self$alpha, shape=self$beta) } | ||
| ) | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if the order of parameters should be reversed to mimic e.g.
GammaandWeibullwhere the shape is the first and the scale parameter is the second argument. On the other hand, probably we don't have a completely consistent and clear convention currently - to solve such ambiguities it might be best to use keyword arguments (e.g.,LogLogistic(; shape::Real, scale::Real)).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having shape and scale keywords would be wonderful. I look up the documentation every time I use
Gamma. Then we could also havemean/dispersion, which in many cases would be even simpler to interpret. I think this was discussed in #823There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried something in #1405 but I didn't pursue it further since there was no response when I opened the PR. Probably, for most distributions exposing the somewhat arbitrary parameter names might also not be the best solution, maybe more meaningful names (such as
shapeandscale) might be better.