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

Most upvoted comments

Ok great ! Glad I got to do something useful.

You can click on the three dots on the right corner of your comments to edit. You will see how to format code.

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

Bracketing values (xa, xb, xc) do not fulfill this requirement: (xa < xb) and (xb < xc)
Bracketing values (xa, xb, xc) do not fulfill this requirement: (f(xb) < f(xa)) and (f(xb) < f(xc))

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?