Skip to content

Commit

Permalink
Add functionality to the logarithmic distributons
Browse files Browse the repository at this point in the history
  • Loading branch information
lrnv committed Nov 24, 2023
1 parent c78f945 commit b401385
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/UnivariateDistribution/Logarithmic.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Corresponds to https://en.wikipedia.org/wiki/Logarithmic_distribution
struct Logarithmic{T<:Real} <: Distributions.DiscreteUnivariateDistribution
α::T # in (0,1), the weight of the logarithmic distribution.
α::T # in (0,1), the weight of the logarithmic distribution, 1-p of the wikipedia parameter p.
h::T # = ln(1-α), so in (-Inf,0)
function Logarithmic(h::T) where {T <: Real}
Tf = promote_type(T,Float64)
Expand All @@ -12,6 +12,31 @@ Base.eltype(::Logarithmic{T}) where T = promote_type(T,Float64)
function Distributions.logpdf(d::Logarithmic{T}, x::Real) where T
insupport(d, x) ? x*log1p(-d.α) - log(x) - log(-log(d.α)) : log(zero(T))
end
function _my_beta_inc(α,k)
# this computes \int_{0}^(1-α) t^k/(1-t) dt, for α in [0,1] and k a positive integer.
r = zero(α)
pwα = 1
sgn = 1
for l in 0:k
r += sgn * binomial(k,l) * (1 - pwα) / l
sgn *= -1
pwα *= α
end
return r
end
function Distributions.cdf(d::Logarithmic{T}, x::Real) where T
# Needs cdf to be implemented here.
# quand even more quantile?
# ee her e: https://en.wikipedia.org/wiki/Logarithmic_distribution
# return 1 + incbeta(p,k=1,0)/log1p(-p)
k = Int(trunc(x))
return 1 + _my_beta_inc(d.α,k)/log(d.α)
# SpecialFunctions.beta_inc(k+1,0,p)/log1p(-p) *SpecialFunctions.beta(k+1,0)
end
function Distributions.quantile(d::Logarithmic{T}, p::Real) where T
return Roots.find_zero(x -> Distributions.cdf(d,x) - p, (0, Inf))
end

function Distributions.rand(rng::Distributions.AbstractRNG, d::Logarithmic{T}) where T
# Sample a Log(p) distribution with the algorithms "LK" and "LS" of Kemp (1981).
if d.h > -3
Expand Down

0 comments on commit b401385

Please sign in to comment.