Skip to content
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

WIP: remove Rmath dependence #138

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Distributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ include("constants.jl")

include("fallbacks.jl")
include("rmath.jl")
include("specialfuns.jl")
include("specialfuns/misc.jl")
include("specialfuns/normal.jl")
include("specialfuns/log.jl")
include("specialfuns/gammabeta.jl")
include("tvpack.jl")
include("utils.jl")

Expand All @@ -229,6 +232,7 @@ include(joinpath("univariate", "bernoulli.jl"))
include(joinpath("univariate", "beta.jl"))
include(joinpath("univariate", "betaprime.jl"))
include(joinpath("univariate", "binomial.jl"))
include(joinpath("univariate", "binomial_rand.jl"))
include(joinpath("univariate", "categorical.jl"))
include(joinpath("univariate", "cauchy.jl"))
include(joinpath("univariate", "chi.jl"))
Expand Down Expand Up @@ -262,6 +266,7 @@ include(joinpath("univariate", "normal.jl"))
include(joinpath("univariate", "normalcanon.jl"))
include(joinpath("univariate", "pareto.jl"))
include(joinpath("univariate", "poisson.jl"))
include(joinpath("univariate", "poisson_rand.jl"))
include(joinpath("univariate", "rayleigh.jl"))
include(joinpath("univariate", "skellam.jl"))
include(joinpath("univariate", "tdist.jl"))
Expand Down
3 changes: 3 additions & 0 deletions src/constants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import Base.@math_const

@math_const twoπ 6.2831853071795864769 big(2.) * π
@math_const halfπ 1.5707963267948966192 big(0.5) * π
@math_const √2 1.4142135623730950488 sqrt(big(2.))
@math_const log2π 1.8378770664093454836 log(big(2.)*π)
@math_const hlog2π 0.9189385332046727418 0.5*log(big(2.)*π)
@math_const √2π 2.5066282746310005024 sqrt(big(2.)*π)
@math_const r√2π 0.3989422804014326779 1/sqrt(big(2.)*π)
41 changes: 41 additions & 0 deletions src/fallbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,47 @@ logccdf(d::Distribution, q::Real) = log(ccdf(d,q))
invlogccdf(d::Distribution, lp::Real) = quantile(d, -expm1(lp))
invlogcdf(d::Distribution, lp::Real) = quantile(d, exp(lp))

function quantile(d::ContinuousUnivariateDistribution, α::Real)

if α < 0 || α > 1 return NaN end
if α == 0 return 0.0 end
if α == 1 return 1.0 end

cc = 10eps()
e = sqrt(eps())
x = mode(d)
while true
dx = (cdf(d, x)::Float64 - α)/max(e,pdf(d, x)::Float64)
if abs(dx) < cc
x -= dx
return x
end
t = x - dx
while !insupport(d, t)
dx *= 0.5
t = x - dx
end
x = t
end
end

function quantile(d::DiscreteUnivariateDistribution, α::Real)
if α < 0 || α > 1 return NaN end
if α == 0 return 0 end
if α == 1 return d.size end
qc = itrunc(quantile(Normal(mean(d), std(d)), α))
if α < cdf(d, qc)
qc -= 1
while α < cdf(d, qc)
qc -= 1
end
return qc + 1
end
while α > cdf(d, qc)
qc += 1
end
return qc
end

#### log likelihood ####

Expand Down
Loading