import math
from scipy.optimize import fsolve
def sigma(s, Bpu):
return s - math.sin(s) - math.pi * Bpu
def jac_sigma(s):
return 1 - math.cos(s)
if __name__ == '__main__':
Bpu = 0.5
sig_r = fsolve(sigma, x0=[math.pi], args=(Bpu), fprime=jac_sigma)
Running the above script throws the following error,
Traceback (most recent call last):
File "C:\Users\RP12808\Desktop\_test_fsolve.py", line 12, in <module>
sig_r = fsolve(sigma, x0=[math.pi], args=(Bpu), fprime=jac_sigma)
File "C:\Users\RP12808\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\optimize\minpack.py", line 146, in fsolve
res = _root_hybr(func, x0, args, jac=fprime, **options)
File "C:\Users\RP12808\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\optimize\minpack.py", line 226, in _root_hybr
_check_func('fsolve', 'fprime', Dfun, x0, args, n, (n, n))
File "C:\Users\RP12808\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\optimize\minpack.py", line 26, in _check_func
res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
TypeError: jac_sigma() takes 1 positional argument but 2 were given
I am unsure how to pass jacobian to fsolve function... how do solve this?
Thanks in advance..RP
The function that computes the Jacobian matrix must take the same arguments as the function to be solved, and it must return an array:
def jac_sigma(s, Bpu):
return np.array([1 - math.cos(s)])
In general, the Jacobian matrix is a two-dimensional array, but
when the variable is a scalar (as it is here) and the Jacobian "matrix" is 1x1, the code accepts a one- or two-dimensional value. (It might be nice if it also accepted a scalar in this case, but it doesn't.)
Actually, it is sufficient that the return value be "array-like"; e.g. a list is also acceptable:
def jac_sigma(s, Bpu):
return [1 - math.cos(s)]
Related
I'm trying to fit the next function into some data using the Scipy Curve_fit function:
def sinugauss(x, A, B, C):
exponente = A*(np.sin(x-B))**2
return np.array([C/(np.exp(exponente))])
I have a data set of 33 points but I keep getting this error:
Traceback (most recent call last):\
File "D:Es_periodico_o_no.py", line 35, in <module>\
res, cov = curve_fit(sinugauss,datos['x'],datos['y'])\
File "D:\lib\site-packages\scipy\optimize\minpack.py", line 789, in curve_fit\
res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)\
File "D:\lib\site-packages\scipy\optimize\minpack.py", line 414, in leastsq
raise TypeError(f"Improper input: func input vector length N={n} must"\
TypeError: Improper input: func input vector length N=3 must not exceed func output vector length M=1
This is the full code:
def sinugauss(x, Ventas, Inicio, Desv):
exponente = Desv*(np.sin(x-Inicio))**2
return np.array([Ventas/(np.exp(exponente))])
for index, row in real_df.iterrows():
datos_y = np.array([row]).transpose()
datos_x = np.array([range(len(datos_y))]).transpose()
datos = pd.DataFrame(np.column_stack([datos_x,datos_y]),columns=['x','y'])
res, cov = curve_fit(sinugauss,datos['x'],datos['y'])
print(res)
print(cov)
The error raises since the first iteration, all the rows has 33 not nan points. There may be zeros
Thank you
In the function sinugauss, change the return statement to:
return C/np.exp(exponente)
When you write np.array([C/(np.exp(exponente))]), you are wrapping the expression C/np.exp(exponente), which might be an array with shape, say, (3,), in a 2-d array with shape (1, 3). That is not the shape that curve_fit expects from your function.
I know there are plenty of subject on this error and I've been on many of them trying to understand what is going on with sush a simple system. Here is my code, solving a very simple equation to test the efficiency of solve_ivp vs odeint.
import numpy as np
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
def fun(t,x,a) :
return -x/a
t = np.linspace(0,10,1000)
tspan = [t[0], t[-1]]
x0 = [10]
sol = solve_ivp(fun, tspan, x0, t_eval = t, args = 1)
plt.plot(t,sol.y.T)
plt.show()
And this is the full error report :
/home/anthony/.local/lib/python3.8/site-packages/scipy/integrate/_ivp/common.py:40:
/home/anthony/.local/lib/python3.8/site-packages/scipy/integrate/_ivp/common.py:40:
UserWarning: The following arguments have no effect for a chosen solver: `args`.
warn("The following arguments have no effect for a chosen solver: {}."
Traceback (most recent call last):
File "test.py", line 25, in <module>
sol = solve_ivp(fun, tspan, x0, t_eval = t, args = 1)
File "/home/anthony/.local/lib/python3.8/site-packages/scipy/integrate/_ivp/ivp.py", line
477, in solve_ivp
solver = method(fun, t0, y0, tf, vectorized=vectorized, **options)
File "/home/anthony/.local/lib/python3.8/site-packages/scipy/integrate/_ivp/rk.py", line
100, in __init__
self.f = self.fun(self.t, self.y)
File "/home/anthony/.local/lib/python3.8/site-packages/scipy/integrate/_ivp/base.py", line
139, in fun
return self.fun_single(t, y)
File "/home/anthony/.local/lib/python3.8/site-packages/scipy/integrate/_ivp/base.py", line
21, in fun_wrapped
return np.asarray(fun(t, y), dtype=dtype)
TypeError: fun() missing 1 required positional argument: 'a'
To me, the error is pretty clear but it is also very obvious that I put in the right place my argument according to the documentation of this solver doc scipy.integrate.solve_ivp
I also upgraded to the latest my scipy version, any advices would be very helful.
args is supposed to be a tuple
In [281]: sol = solve_ivp(fun, tspan, x0, t_eval = t, args = (1,))
In [282]: sol
Out[282]:
message: 'The solver successfully reached the end of the integration interval.'
nfev: 80
njev: 0
nlu: 0
sol: None
status: 0
success: True
....
Messing up the args is one of the most common scipy.integrate (and optimize) SO errors.
I'm trying to estimate a maximum likelihood model in python. I set up both the likelihood function and the analytic jacobian. When I run scipy minimize, I get a bizarre error (displayed below). This error doesn't seem to occur when I omit the jacobian, but I can't figure out why.
from numpy import log,sum,var
from numba import njit
#njit
def log_likelihood(params,surg_fx,surg_fx_ses):
mu_var = params[0]
exp_var = mu_var + surg_fx_ses**2
log_lik = -((surg_fx)**2 / (2*exp_var)) - .5*log(exp_var)
neg_sum_log_lik = -sum(log_lik)
print(mu_var)
print(neg_sum_log_lik)
if np.isnan(neg_sum_log_lik):
return 1e20
else:
return neg_sum_log_lik
#njit
def log_lik_jac(params,surg_fx,surg_fx_ses):
mu_var = params[0]
exp_var = mu_var + surg_fx_ses**2
jc = -sum(((surg_fx)**2 / (2*(exp_var**2))) - (.5/exp_var))
print(mu_var)
print(jc)
return jc
x0 = [np.var(cost_params3)]
shrinkage_est = minimize(log_likelihood,x0,args=(cost_params3,cost_SEs3),jac=log_lik_jac,options={'disp':True},method='BFGS')
cost_params3 and cost_SEs3 are (205,)-shaped numpy arrays.
And the return is:
0.10423462356390442
-580.1534424527905
0.10423462356390442
-67.02947836460727
[ 1.11423462]
26.84532144252225
[ 1.11423462]
77.95606471086792
[ 0.3741784]
-54.28224588483895
[ 0.3741784]
150.90730570822998
[ 0.19152581]
-79.19268133113846
[ 0.19152581]
68.81484893304786
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/anaconda3/lib/python3.5/site-packages/scipy/optimize/_minimize.py", line 444, in minimize
return _minimize_bfgs(fun, x0, args, jac, callback, **options)
File "/usr/local/anaconda3/lib/python3.5/site-packages/scipy/optimize/optimize.py", line 973, in _minimize_bfgs
A1 = I - sk[:, numpy.newaxis] * yk[numpy.newaxis, :] * rhok
TypeError: 'float' object is not subscriptable
I'm not really sure why this runs for a few iterations and just fails, especially given that nothing is being subscripted here. I'm also not sure why it seems to become a list after the first iteration? I tried running it without numba but it stopped at the same place with a different error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/anaconda3/lib/python3.5/site-packages/scipy/optimize/_minimize.py", line 444, in minimize
return _minimize_bfgs(fun, x0, args, jac, callback, **options)
File "/usr/local/anaconda3/lib/python3.5/site-packages/scipy/optimize/optimize.py", line 973, in _minimize_bfgs
A1 = I - sk[:, numpy.newaxis] * yk[numpy.newaxis, :] * rhok
IndexError: invalid index to scalar variable.
Any help would be much appreciated!
I have a question about how to use the Levenberg-Marquardt optimize method in Python. In the library SCIPY there are many optimization methods.
I tried two methods (Nelder-Mead and Basin-hopping) and both work well with the follow command:
# Nelder-Mead
res0_10 = optimize.minimize(f0_10, x0, method='Nelder-Mead', options={'disp': True, 'maxiter': 2000})
# Basin-hopping
res0_10 = optimize.basinhopping(f0_10, x0, niter=100, disp=True)
The problem emerge when I use the Levenberg-Marquardt (I copy only the part of error, because the program is long)
def f0_10(x):
m, u, z, s = x
for i in range(alt_max):
if i==alt_min: suma=0
if i > alt_min:
suma = suma + (B(x, i)-b0_10(x, i))**2
return np.sqrt(suma/alt_max)
x0 = np.array([40., 0., 500., 50.])
res0_10 = root(f0_10, x0, jac=True, method='lm')
I only change the last sentence (res0_10 = root...). The program compile well, but when I execute the program:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Quini SB\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.7.4.3348.win-x86_64\lib\lib-tk\Tkinter.py", line 1536, in __call__
return self.func(*args)
File "C:\Users\Quini SB\Desktop\tfg\Steyn - levmar.py", line 384, in askopenfilename
res0_10 = root(f0_10, x0, jac=True, method='lm')
File "C:\Users\Quini SB\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\_root.py", line 188, in root
sol = _root_leastsq(fun, x0, args=args, jac=jac, **options)
File "C:\Users\Quini SB\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\_root.py", line 251, in _root_leastsq
factor=factor, diag=diag)
File "C:\Users\Quini SB\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\minpack.py", line 377, in leastsq
shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
File "C:\Users\Quini SB\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\minpack.py", line 26, in _check_func
res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
File "C:\Users\Quini SB\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\optimize.py", line 64, in __call__
self.jac = fg[1]
IndexError: invalid index to scalar variable.
Why does this error happen?
From documentation:
jac : bool or callable, optional
If jac is a Boolean and is True, fun is assumed to return the value
of Jacobian along with the objective function. If False, the
Jacobian will be estimated numerically. jac can also be a callable
returning the Jacobian of fun. In this case, it must accept the
same arguments as fun.
So, your function 'f0_10' needs to return two values, because you set jac to True
I seem to be getting an error when I use the root-finder in scipy. I was wondering if anyone could point out what I'm doing wrong.
The function I'm finding the root of is just an easy example, and not particularly important.
If I run this code with scipy 0.9.0:
import numpy as np
from scipy.optimize import fsolve
tmpFunc = lambda xIn: (xIn[0]-4)**2 + (xIn[1]-5)**2 + (xIn[2]-7)**3
x0 = [3,4,5]
xFinal = fsolve(tmpFunc, x0 )
print xFinal
I get the following error message:
Traceback (most recent call last):
File "tmpStack.py", line 7, in <module>
xFinal = fsolve(tmpFunc, x0 )
File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 115, in fsolve
_check_func('fsolve', 'func', func, x0, args, n, (n,))
File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 26, in _check_func
raise TypeError(msg)
TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument '<lambda>'.
Well it looks like I was trying to use this routine incorrectly. This routine requires the same number of equations and variables vs. the one equation with three variables I gave it. So if the input to the function to be minimized is a 3-D array the output should be a 3-D array. This code works:
import numpy as np
from scipy.optimize import fsolve
tmpFunc = lambda xIn: np.array( [(xIn[0]-4)**2 + xIn[1], (xIn[1]-5)**2 - xIn[2]) \
, (xIn[2]-7)**3 + xIn[0] ] )
x0 = [3,4,5]
xFinal = fsolve(tmpFunc, x0 )
print xFinal
Which represents solving three equations simultaneously.