@@ -47,7 +47,7 @@ quantile_bisect(d::ContinuousUnivariateDistribution, p::Real) =
47
47
# Distribution, with Application to the Inverse Gaussian Distribution
48
48
# http://www.statsci.org/smyth/pubs/qinvgaussPreprint.pdf
49
49
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}
51
51
x = xs - Δ (xs)
52
52
@assert typeof (x) === T
53
53
x0 = T (xs)
@@ -58,13 +58,20 @@ function newton(Δ, xs::T=mode(d), tol::Real=1e-12) where {T}
58
58
return x
59
59
end
60
60
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
+
61
66
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)
63
69
# FIXME : can this be expressed via `promote_type()`? Test coverage missing.
70
+ Δ (x) = f (x)/ df (x)
64
71
x = xs - Δ (xs)
65
72
T = typeof (x)
66
73
if 0 < p < 1
67
- return newton (Δ , T (xs), tol)
74
+ return newton ((f, df) , T (xs), tol)
68
75
elseif p == 0
69
76
return T (minimum (d))
70
77
elseif p == 1
@@ -75,12 +82,14 @@ function quantile_newton(d::ContinuousUnivariateDistribution, p::Real, xs::Real=
75
82
end
76
83
77
84
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)
79
87
# FIXME : can this be expressed via `promote_type()`? Test coverage missing.
88
+ Δ (x) = f (x)/ df (x)
80
89
x = xs - Δ (xs)
81
90
T = typeof (x)
82
91
if 0 < p < 1
83
- return newton (Δ , T (xs), tol)
92
+ return newton ((f, df) , T (xs), tol)
84
93
elseif p == 1
85
94
return T (minimum (d))
86
95
elseif p == 0
@@ -97,9 +106,9 @@ function invlogcdf_newton(d::ContinuousUnivariateDistribution, lp::Real, xs::Rea
97
106
if - Inf < lp < 0
98
107
x0 = T (xs)
99
108
if lp < logcdf (d,x0)
100
- return newton (Δ_ver0, T (xs), tol)
109
+ return newton_impl (Δ_ver0, T (xs), tol)
101
110
else
102
- return newton (Δ_ver1, T (xs), tol)
111
+ return newton_impl (Δ_ver1, T (xs), tol)
103
112
end
104
113
return x
105
114
elseif lp == - Inf
@@ -118,9 +127,9 @@ function invlogccdf_newton(d::ContinuousUnivariateDistribution, lp::Real, xs::Re
118
127
if - Inf < lp < 0
119
128
x0 = T (xs)
120
129
if lp < logccdf (d,x0)
121
- return newton (Δ_ver0, T (xs), tol)
130
+ return newton_impl (Δ_ver0, T (xs), tol)
122
131
else
123
- return newton (Δ_ver1, T (xs), tol)
132
+ return newton_impl (Δ_ver1, T (xs), tol)
124
133
end
125
134
return x
126
135
elseif lp == - Inf
0 commit comments