scipy: BUG: unclear error for invalid bracketing
Describe your issue.
I have enclosed a minimum reproducible example with data and I am trying to use two methods in the scipy optimize package - https://docs.scipy.org/doc/scipy/reference/optimize.html
One is minimize_scalar and the other is Brent’s method. But both the outputs have large differences. Can anybody explain why ?
Also I get a runtime error while running Brent-
test.py:29: RuntimeWarning: invalid value encountered in power ls = np.sum((x1[1:] - x1[:-1]*np.sign(a)*a**dt)**2)
Additionally the Brent one has another one if I specify a triple interval ala - dum = brent(func,brack=(-1,0,1),tol=3.0e-8) ValueError : Not a bracketing interval.
https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.brent.html But docs say that I can specify a triplet in the bracketing interval.
Reproducing Code Example
`import numpy as np
from scipy.optimize import brent,minimize_scalar
import math
def main():
data = np.loadtxt("x.dat",skiprows=0)
t = data[:,0]
x = data[:,1]
dum = minls(t,x)
def minls(t,x):
a = 1/math.e
dt = np.diff(t)
def func(a):
try:
ls = np.sum((x[1:] - x[:-1]*a**dt)**2)
except Warning:
print(traceback.format_exc())
return ls
dum = brent(func,brack=(-1,0,1),tol=3.0e-8)
dum1 = minimize_scalar(func, bounds=[0, 1], method='bounded').x
print(dum,dum1)
return dum
main()
`
Error message
I encounter an error and a warning wi this
Test.py:29: RuntimeWarning: invalid value encountered in power
ValueError : Not a bracketing interval.
SciPy/NumPy/Python version information
Scipy 1.7.1 /Numpy 1.19.1/Python3.8
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 36 (30 by maintainers)
Commits related to this issue
- fixes BUG: unclear error for invalid bracketing #16272 — committed to Chinwendu20/scipy by Chinwendu20 2 years ago
Ok great ! Glad I got to do something useful.
Got it, thank you. I appreciate your time and patience with me. Expect my PR soon.
@Chinwendu20 No need for the link to the doc. The following is fine
You could create another function or use a lambda if you can write something concise. What matters in the test is that we check the message (
with pytest.raises(ValueError, match="...")
)I would like to try a PR, what should the error message be?