I am trying to optimize SSE (sum of squared error) of a function using scipy.optimize
. To test with, I created a simple problem as below code.
But the optimized parameters output by scipy never makes SSE=0. Can someone help me to understand, where am I going wrong.
I tried to cross check with the SSE calculated by my code with the one computed in excel. It matched. Then I used minimize function to minimize that SSE function, the ones computed by Scipy is not matching with the hand calculated ones. The function I used to is of form (y=ax+b). Below is the code
import numpy as np
from scipy.optimize import minimize
e=np.array([0,2])
sig1=np.array([0,200])
k = [10,10]
#n = 0.2
coe=np.array([k[0],k[1]])
def sig2(e):
v=(k[0]*e)+ k[1]
SEzip = zip(sig1, v)
sse = 0
for y in SEzip:
sse += np.power((y[0] - y[1]),2)
return sse
print (sig2(e))
def f(coe):
print(coe)
return f
result = minimize(sig2,coe,method='Nelder-Mead',callback=(f),options={'xtol': 1e-6,'ftol':1e-06,'maxiter':50000,'disp': True,'adaptive' : True})
print(result)