@@ -1212,6 +1212,25 @@ def _logexpxmexpy(x, y):
1212
1212
return res
1213
1213
1214
1214
1215
+ def _guess_bracket (xmin , xmax ):
1216
+ a = np .full_like (xmin , - 1.0 )
1217
+ b = np .ones_like (xmax )
1218
+
1219
+ i = np .isfinite (xmin ) & np .isfinite (xmax )
1220
+ a [i ] = xmin [i ]
1221
+ b [i ] = xmax [i ]
1222
+
1223
+ i = np .isfinite (xmin ) & ~ np .isfinite (xmax )
1224
+ a [i ] = xmin [i ]
1225
+ b [i ] = xmin [i ] + 1
1226
+
1227
+ i = np .isfinite (xmax ) & ~ np .isfinite (xmin )
1228
+ a [i ] = xmax [i ] - 1
1229
+ b [i ] = xmax [i ]
1230
+
1231
+ return a , b
1232
+
1233
+
1215
1234
def _log_real_standardize (x ):
1216
1235
"""Standardizes the (complex) logarithm of a real number.
1217
1236
@@ -2011,27 +2030,13 @@ def f2(x, _p, **kwargs): # named `_p` to avoid conflict with shape `p`
2011
2030
shape = xmin .shape
2012
2031
xmin , xmax = np .atleast_1d (xmin , xmax )
2013
2032
2014
- a = - np .ones_like (xmin )
2015
- b = np .ones_like (xmax )
2016
-
2017
- i = np .isfinite (xmin ) & np .isfinite (xmax )
2018
- a [i ] = xmin [i ]
2019
- b [i ] = xmax [i ]
2020
-
2021
- i = np .isfinite (xmin ) & ~ np .isfinite (xmax )
2022
- a [i ] = xmin [i ]
2023
- b [i ] = xmin [i ] + 1
2024
-
2025
- i = np .isfinite (xmax ) & ~ np .isfinite (xmin )
2026
- a [i ] = xmax [i ] - 1
2027
- b [i ] = xmax [i ]
2028
-
2033
+ xl0 , xr0 = _guess_bracket (xmin , xmax )
2029
2034
xmin = xmin .reshape (shape )
2030
2035
xmax = xmax .reshape (shape )
2031
- a = a .reshape (shape )
2032
- b = b .reshape (shape )
2036
+ xl0 = xl0 .reshape (shape )
2037
+ xr0 = xr0 .reshape (shape )
2033
2038
2034
- res = _bracket_root (f3 , xl0 = a , xr0 = b , xmin = xmin , xmax = xmax , args = args )
2039
+ res = _bracket_root (f3 , xl0 = xl0 , xr0 = xr0 , xmin = xmin , xmax = xmax , args = args )
2035
2040
# For now, we ignore the status, but I want to use the bracket width
2036
2041
# as an error estimate - see question 5 at the top.
2037
2042
xrtol = None if _isnull (self .tol ) else self .tol
@@ -4636,7 +4641,8 @@ def _invert(self, fun, p):
4636
4641
xmin , xmax = self .support ()
4637
4642
fun = getattr (self , fun )
4638
4643
f = lambda x , p : fun (x ) - p # noqa: E731 is silly
4639
- res = _bracket_root (f , xl0 = self .mean (), xmin = xmin , xmax = xmax , args = (p ,))
4644
+ xl0 , xr0 = _guess_bracket (xmin , xmax )
4645
+ res = _bracket_root (f , xl0 = xl0 , xr0 = xr0 , xmin = xmin , xmax = xmax , args = (p ,))
4640
4646
return _chandrupatla (f , a = res .xl , b = res .xr , args = (p ,)).x
4641
4647
4642
4648
def icdf (self , p , / , * , method = None ):
0 commit comments