Skip to content

Commit fa89dda

Browse files
committed
[c]quantile_newton(): explicitly pass function and derivative into the newton()
1 parent 02b0f4a commit fa89dda

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/quantilealgs.jl

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ quantile_bisect(d::ContinuousUnivariateDistribution, p::Real) =
4949
# Distribution, with Application to the Inverse Gaussian Distribution
5050
# http://www.statsci.org/smyth/pubs/qinvgaussPreprint.pdf
5151

52-
function newton(Δ, xs::T=mode(d), tol::Real=1e-12) where {T}
52+
function newton_impl(Δ, xs::T=mode(d), tol::Real=1e-12) where {T}
5353
x = xs - Δ(xs)
5454
@assert typeof(x) === T
5555
x0 = T(xs)
@@ -60,13 +60,20 @@ function newton(Δ, xs::T=mode(d), tol::Real=1e-12) where {T}
6060
return x
6161
end
6262

63+
function newton((f,df), xs::T=mode(d), tol::Real=1e-12) where {T}
64+
Δ(x) = f(x)/df(x)
65+
return newton_impl(Δ, xs, tol)
66+
end
67+
6368
function quantile_newton(d::ContinuousUnivariateDistribution, p::Real, xs::Real=mode(d), tol::Real=1e-12)
64-
Δ(x) = -((p - cdf(d, x)) / pdf(d, x))
69+
f(x) = -(p - cdf(d, x))
70+
df(x) = pdf(d, x)
6571
# FIXME: can this be expressed via `promote_type()`? Test coverage missing.
72+
Δ(x) = f(x)/df(x)
6673
x = xs - Δ(xs)
6774
T = typeof(x)
6875
if 0 < p < 1
69-
return newton(Δ, T(xs), tol)
76+
return newton((f, df), T(xs), tol)
7077
elseif p == 0
7178
return T(minimum(d))
7279
elseif p == 1
@@ -77,12 +84,14 @@ function quantile_newton(d::ContinuousUnivariateDistribution, p::Real, xs::Real=
7784
end
7885

7986
function cquantile_newton(d::ContinuousUnivariateDistribution, p::Real, xs::Real=mode(d), tol::Real=1e-12)
80-
Δ(x) = -((ccdf(d, x)-p) / pdf(d, x))
87+
f(x) = -(ccdf(d, x)-p)
88+
df(x) = pdf(d, x)
8189
# FIXME: can this be expressed via `promote_type()`? Test coverage missing.
90+
Δ(x) = f(x)/df(x)
8291
x = xs - Δ(xs)
8392
T = typeof(x)
8493
if 0 < p < 1
85-
return newton(Δ, T(xs), tol)
94+
return newton((f, df), T(xs), tol)
8695
elseif p == 1
8796
return T(minimum(d))
8897
elseif p == 0
@@ -99,9 +108,9 @@ function invlogcdf_newton(d::ContinuousUnivariateDistribution, lp::Real, xs::Rea
99108
if -Inf < lp < 0
100109
x0 = T(xs)
101110
if lp < logcdf(d,x0)
102-
return newton(Δ_ver0, T(xs), tol)
111+
return newton_impl(Δ_ver0, T(xs), tol)
103112
else
104-
return newton(Δ_ver1, T(xs), tol)
113+
return newton_impl(Δ_ver1, T(xs), tol)
105114
end
106115
return x
107116
elseif lp == -Inf
@@ -120,9 +129,9 @@ function invlogccdf_newton(d::ContinuousUnivariateDistribution, lp::Real, xs::Re
120129
if -Inf < lp < 0
121130
x0 = T(xs)
122131
if lp < logccdf(d,x0)
123-
return newton(Δ_ver0, T(xs), tol)
132+
return newton_impl(Δ_ver0, T(xs), tol)
124133
else
125-
return newton(Δ_ver1, T(xs), tol)
134+
return newton_impl(Δ_ver1, T(xs), tol)
126135
end
127136
return x
128137
elseif lp == -Inf

0 commit comments

Comments
 (0)