Skip to content

Commit f3533d1

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

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
@@ -47,7 +47,7 @@ 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(Δ, xs::T=mode(d), tol::Real=1e-12) where {T}
50+
function newton_impl(Δ, xs::T=mode(d), tol::Real=1e-12) where {T}
5151
x = xs - Δ(xs)
5252
@assert typeof(x) === T
5353
x0 = T(xs)
@@ -58,13 +58,20 @@ function newton(Δ, xs::T=mode(d), tol::Real=1e-12) where {T}
5858
return x
5959
end
6060

61+
function newton((f,df), xs::T=mode(d), tol::Real=1e-12) where {T}
62+
Δ(x) = f(x)/df(x)
63+
return newton_impl(Δ, xs, tol)
64+
end
65+
6166
function quantile_newton(d::ContinuousUnivariateDistribution, p::Real, xs::Real=mode(d), tol::Real=1e-12)
62-
Δ(x) = -((p - cdf(d, x)) / pdf(d, x))
67+
f(x) = -(p - cdf(d, x))
68+
df(x) = pdf(d, x)
6369
# FIXME: can this be expressed via `promote_type()`? Test coverage missing.
70+
Δ(x) = f(x)/df(x)
6471
x = xs - Δ(xs)
6572
T = typeof(x)
6673
if 0 < p < 1
67-
return newton(Δ, T(xs), tol)
74+
return newton((f, df), T(xs), tol)
6875
elseif p == 0
6976
return T(minimum(d))
7077
elseif p == 1
@@ -75,12 +82,14 @@ function quantile_newton(d::ContinuousUnivariateDistribution, p::Real, xs::Real=
7582
end
7683

7784
function cquantile_newton(d::ContinuousUnivariateDistribution, p::Real, xs::Real=mode(d), tol::Real=1e-12)
78-
Δ(x) = -((ccdf(d, x)-p) / pdf(d, x))
85+
f(x) = -(ccdf(d, x)-p)
86+
df(x) = pdf(d, x)
7987
# FIXME: can this be expressed via `promote_type()`? Test coverage missing.
88+
Δ(x) = f(x)/df(x)
8089
x = xs - Δ(xs)
8190
T = typeof(x)
8291
if 0 < p < 1
83-
return newton(Δ, T(xs), tol)
92+
return newton((f, df), T(xs), tol)
8493
elseif p == 1
8594
return T(minimum(d))
8695
elseif p == 0
@@ -97,9 +106,9 @@ function invlogcdf_newton(d::ContinuousUnivariateDistribution, lp::Real, xs::Rea
97106
if -Inf < lp < 0
98107
x0 = T(xs)
99108
if lp < logcdf(d,x0)
100-
return newton(Δ_ver0, T(xs), tol)
109+
return newton_impl(Δ_ver0, T(xs), tol)
101110
else
102-
return newton(Δ_ver1, T(xs), tol)
111+
return newton_impl(Δ_ver1, T(xs), tol)
103112
end
104113
return x
105114
elseif lp == -Inf
@@ -118,9 +127,9 @@ function invlogccdf_newton(d::ContinuousUnivariateDistribution, lp::Real, xs::Re
118127
if -Inf < lp < 0
119128
x0 = T(xs)
120129
if lp < logccdf(d,x0)
121-
return newton(Δ_ver0, T(xs), tol)
130+
return newton_impl(Δ_ver0, T(xs), tol)
122131
else
123-
return newton(Δ_ver1, T(xs), tol)
132+
return newton_impl(Δ_ver1, T(xs), tol)
124133
end
125134
return x
126135
elseif lp == -Inf

0 commit comments

Comments
 (0)