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