I'm getting the TypeError: 'numpy.float64' object is not callable error for the following code:
import numpy as np
from scipy.optimize import minimize
def ses(data, alpha):
fit=[]
fit.append(alpha*data[1] + (1-alpha)*data[0])
for i in range(2, len(data)):
fit.append(data[i]*alpha + fit[i-2]*(1-alpha))
return fit
def rmse(data, fit):
se=[]
for i in range(2,len(data)):
se.append((data[i]-fit[i-2])*(data[i]-fit[i-2]))
mse=np.mean(se)
return np.sqrt(mse)
alpha=0.1555 # starting value
fit=ses(d[0], alpha)
error=rmse(d[0], fit)
result=minimize(error, alpha, (fit,), bounds=[(0,1)], method='SLSQP')
I've tried many alternatives and its just not working. Changed the lists to arrays and made the multiplications involve no exponentials (np.sqrt() as opposed to ()**0.5)
EDIT:
def ses(data, alpha):
fit=[]
fit.append(alpha*data[1] + (1-alpha)*data[0])
for i in range(2, len(data)):
fit.append(data[i]*alpha + fit[i-2]*(1-alpha))
return fit
def rmse(data, alpha):
fit=ses(data, alpha)
se=[]
for i in range(2,len(data)):
print i, i-2
se.append((data[i]-fit[i-2])*(data[i]-fit[i-2]))
mse=np.mean(se)
return np.sqrt(mse)
alpha=0.1555 # starting value
data=d[0]
result = minimize(rmse, alpha, (data,), bounds=[(0,1)], method='SLSQP')
Ok guys, thanks. Have edited to this and I have stopped the error, however now I am getting an index out of bounds error, which is strange as without the minimize line, the code runs perfectly fine.
EDIT 2:
There was a series of silly errors, most of which I didn't know were problems, but were solved by trial and error.
For some working code of optimized exponential smoothing:
def ses(data, alpha):
'Simple exponential smoothing'
fit=[]
fit.append(data[0])
fit.append(data[1]) ## pads first two
fit.append(alpha*data[1] + (1-alpha)*data[0])
for i in range(2, len(data)-1):
fit.append(alpha*data[i] + (1-alpha)*fit[i])
return fit
def rmse(alpha, data):
fit=ses(data, alpha)
se=[]
for i in range(2,len(data)):
se.append((data[i]-fit[i-2])*(data[i]-fit[i-2]))
mse=np.mean(se)
return np.sqrt(mse)
alpha=0.5
data = d[0]
result = minimize(rmse, alpha, (data,), bounds=[(0,1)], method='SLSQP')
minimize()defined? Post this function.alphaanddataarguments back-to-front in your function definitions.