Skip to content

Commit c02e6f7

Browse files
committed
quantile_newton(): extract newton()
1 parent 4a40f79 commit c02e6f7

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/quantilealgs.jl

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,24 @@ quantile_bisect(d::ContinuousUnivariateDistribution, p::Real) =
4747
# Distribution, with Application to the Inverse Gaussian Distribution
4848
# http://www.statsci.org/smyth/pubs/qinvgaussPreprint.pdf
4949

50+
function newton(f, xs::T=mode(d), tol::Real=1e-12) where {T}
51+
x = xs + f(xs)
52+
@assert typeof(x) === T
53+
x0 = T(xs)
54+
while abs(x-x0) > max(abs(x),abs(x0)) * tol
55+
x0 = x
56+
x = x0 + f(x0)
57+
end
58+
return x
59+
end
60+
5061
function quantile_newton(d::ContinuousUnivariateDistribution, p::Real, xs::Real=mode(d), tol::Real=1e-12)
5162
f(x) = (p - cdf(d, x)) / pdf(d, x)
63+
# FIXME: can this be expressed via `promote_type()`? Test coverage missing.
5264
x = xs + f(xs)
5365
T = typeof(x)
5466
if 0 < p < 1
55-
x0 = T(xs)
56-
while abs(x-x0) > max(abs(x),abs(x0)) * tol
57-
x0 = x
58-
x = x0 + f(x0)
59-
end
60-
return x
67+
return newton(f, T(xs), tol)
6168
elseif p == 0
6269
return T(minimum(d))
6370
elseif p == 1

0 commit comments

Comments
 (0)