Skip to content

Commit b97bfca

Browse files
committed
newton(): more closely match text-book method, which performs subtraction,
1 parent db6bf0a commit b97bfca

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/quantilealgs.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,20 @@ quantile_bisect(d::ContinuousUnivariateDistribution, p::Real) =
4848
# http://www.statsci.org/smyth/pubs/qinvgaussPreprint.pdf
4949

5050
function newton(f, xs::T=mode(d), tol::Real=1e-12) where {T}
51-
x = xs + f(xs)
51+
x = xs - f(xs)
5252
@assert typeof(x) === T
5353
x0 = T(xs)
5454
while abs(x-x0) > max(abs(x),abs(x0)) * tol
5555
x0 = x
56-
x = x0 + f(x0)
56+
x = x0 - f(x0)
5757
end
5858
return x
5959
end
6060

6161
function quantile_newton(d::ContinuousUnivariateDistribution, p::Real, xs::Real=mode(d), tol::Real=1e-12)
62-
f(x) = (p - cdf(d, x)) / pdf(d, x)
62+
f(x) = -((p - cdf(d, x)) / pdf(d, x))
6363
# FIXME: can this be expressed via `promote_type()`? Test coverage missing.
64-
x = xs + f(xs)
64+
x = xs - f(xs)
6565
T = typeof(x)
6666
if 0 < p < 1
6767
return newton(f, T(xs), tol)
@@ -75,9 +75,9 @@ function quantile_newton(d::ContinuousUnivariateDistribution, p::Real, xs::Real=
7575
end
7676

7777
function cquantile_newton(d::ContinuousUnivariateDistribution, p::Real, xs::Real=mode(d), tol::Real=1e-12)
78-
f(x) = (ccdf(d, x)-p) / pdf(d, x)
78+
f(x) = -((ccdf(d, x)-p) / pdf(d, x))
7979
# FIXME: can this be expressed via `promote_type()`? Test coverage missing.
80-
x = xs + f(xs)
80+
x = xs - f(xs)
8181
T = typeof(x)
8282
if 0 < p < 1
8383
return newton(f, T(xs), tol)
@@ -92,8 +92,8 @@ end
9292

9393
function invlogcdf_newton(d::ContinuousUnivariateDistribution, lp::Real, xs::Real=mode(d), tol::Real=1e-12)
9494
T = typeof(lp - logpdf(d,xs))
95-
f_a(x) = -exp(lp - logpdf(d,x) + logexpm1(max(logcdf(d,x)-lp,0)))
96-
f_b(x) = exp(lp - logpdf(d,x) + log1mexp(min(logcdf(d,x)-lp,0)))
95+
f_a(x) = exp(lp - logpdf(d,x) + logexpm1(max(logcdf(d,x)-lp,0)))
96+
f_b(x) = -exp(lp - logpdf(d,x) + log1mexp(min(logcdf(d,x)-lp,0)))
9797
if -Inf < lp < 0
9898
x0 = T(xs)
9999
if lp < logcdf(d,x0)
@@ -113,8 +113,8 @@ end
113113

114114
function invlogccdf_newton(d::ContinuousUnivariateDistribution, lp::Real, xs::Real=mode(d), tol::Real=1e-12)
115115
T = typeof(lp - logpdf(d,xs))
116-
f_a(x) = exp(lp - logpdf(d,x) + logexpm1(max(logccdf(d,x)-lp,0)))
117-
f_b(x) = -exp(lp - logpdf(d,x) + log1mexp(min(logccdf(d,x)-lp,0)))
116+
f_a(x) = -exp(lp - logpdf(d,x) + logexpm1(max(logccdf(d,x)-lp,0)))
117+
f_b(x) = exp(lp - logpdf(d,x) + log1mexp(min(logccdf(d,x)-lp,0)))
118118
if -Inf < lp < 0
119119
x0 = T(xs)
120120
if lp < logccdf(d,x0)

0 commit comments

Comments
 (0)