Skip to content

Commit

Permalink
Be more specific that tol is really a xrtol
Browse files Browse the repository at this point in the history
  • Loading branch information
LebedevRI committed Sep 13, 2024
1 parent cf55a37 commit 96786f0
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/quantilealgs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,31 @@ quantile_bisect(d::ContinuousUnivariateDistribution, p::Real) =
# Distribution, with Application to the Inverse Gaussian Distribution
# http://www.statsci.org/smyth/pubs/qinvgaussPreprint.pdf

function newton_impl(Δ, xs::T=mode(d), tol::Real=1e-12) where {T}
function newton_impl(Δ, xs::T=mode(d), xrtol::Real=1e-12) where {T}
x = xs - Δ(xs)
@assert typeof(x) === T
x0 = T(xs)
while !isapprox(x, x0, atol=0, rtol=tol)
while !isapprox(x, x0, atol=0, rtol=xrtol)
x0 = x
x = x0 - Δ(x0)
end
return x
end

function newton((f,df), xs::T=mode(d), tol::Real=1e-12) where {T}
function newton((f,df), xs::T=mode(d), xrtol::Real=1e-12) where {T}
Δ(x) = f(x)/df(x)
return newton_impl(Δ, xs, tol)
return newton_impl(Δ, xs, xrtol)
end

function quantile_newton(d::ContinuousUnivariateDistribution, p::Real, xs::Real=mode(d), tol::Real=1e-12)
function quantile_newton(d::ContinuousUnivariateDistribution, p::Real, xs::Real=mode(d), xrtol::Real=1e-12)
f(x) = cdf(d, x) - p
df(x) = pdf(d, x)
# FIXME: can this be expressed via `promote_type()`? Test coverage missing.
Δ(x) = f(x)/df(x)
x = xs - Δ(xs)
T = typeof(x)
if 0 < p < 1
return newton((f, df), T(xs), tol)
return newton((f, df), T(xs), xrtol)
elseif p == 0
return T(minimum(d))
elseif p == 1
Expand All @@ -81,15 +81,15 @@ function quantile_newton(d::ContinuousUnivariateDistribution, p::Real, xs::Real=
end
end

function cquantile_newton(d::ContinuousUnivariateDistribution, p::Real, xs::Real=mode(d), tol::Real=1e-12)
function cquantile_newton(d::ContinuousUnivariateDistribution, p::Real, xs::Real=mode(d), xrtol::Real=1e-12)
f(x) = p - ccdf(d, x)
df(x) = pdf(d, x)
# FIXME: can this be expressed via `promote_type()`? Test coverage missing.
Δ(x) = f(x)/df(x)
x = xs - Δ(xs)
T = typeof(x)
if 0 < p < 1
return newton((f, df), T(xs), tol)
return newton((f, df), T(xs), xrtol)
elseif p == 1
return T(minimum(d))
elseif p == 0
Expand All @@ -99,17 +99,17 @@ function cquantile_newton(d::ContinuousUnivariateDistribution, p::Real, xs::Real
end
end

function invlogcdf_newton(d::ContinuousUnivariateDistribution, lp::Real, xs::Real=mode(d), tol::Real=1e-12)
function invlogcdf_newton(d::ContinuousUnivariateDistribution, lp::Real, xs::Real=mode(d), xrtol::Real=1e-12)
T = typeof(lp - logpdf(d,xs))
f_ver0(x) = exp(lp - logpdf(d,x) + logexpm1(max(logcdf(d,x)-lp,0)))
f_ver1(x) = -exp(lp - logpdf(d,x) + log1mexp(min(logcdf(d,x)-lp,0)))
df(x::T) where {T} = T(1)
if -Inf < lp < 0
x0 = T(xs)
if lp < logcdf(d,x0)
return newton((f_ver0,df), T(xs), tol)
return newton((f_ver0,df), T(xs), xrtol)
else
return newton((f_ver1,df), T(xs), tol)
return newton((f_ver1,df), T(xs), xrtol)
end
return x
elseif lp == -Inf
Expand All @@ -121,17 +121,17 @@ function invlogcdf_newton(d::ContinuousUnivariateDistribution, lp::Real, xs::Rea
end
end

function invlogccdf_newton(d::ContinuousUnivariateDistribution, lp::Real, xs::Real=mode(d), tol::Real=1e-12)
function invlogccdf_newton(d::ContinuousUnivariateDistribution, lp::Real, xs::Real=mode(d), xrtol::Real=1e-12)
T = typeof(lp - logpdf(d,xs))
f_ver0(x) = -exp(lp - logpdf(d,x) + logexpm1(max(logccdf(d,x)-lp,0)))
f_ver1(x) = exp(lp - logpdf(d,x) + log1mexp(min(logccdf(d,x)-lp,0)))
df(x::T) where {T} = T(1)
if -Inf < lp < 0
x0 = T(xs)
if lp < logccdf(d,x0)
return newton((f_ver0,df), T(xs), tol)
return newton((f_ver0,df), T(xs), xrtol)
else
return newton((f_ver1,df), T(xs), tol)
return newton((f_ver1,df), T(xs), xrtol)
end
return x
elseif lp == -Inf
Expand Down

0 comments on commit 96786f0

Please sign in to comment.