Python - 'numpy.float64' object is not callable using minimize function for alpha optimization for Simple Exponential Smoothing
Question: 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')
    


Answer: Its hard to tell exactly what the problem is here. I assume that `minimize` is
actually [Scipy's
minimize](http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.optimize.minimize.html).

If so the first argument should be a function. Instead, you are passing the
output of the `rmse` function, which is a double precision number.

    
    
    error=rmse(d[0], fit) # <--- returns a number
    

You should have:

    
    
    result=minimize(<some function here>, alpha, (fit,), bounds=[(0,1)], method='SLSQP')
    

When `minimize` is called, it attempts to call `error`, thus throwing a
`TypeError: 'numpy.float64' object is not callable`

There is a straightforward tutorial
[here](http://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html) that
walks through exactly how to use `minimize` with the sequential least squares
programming optimization algorithm.

I would hazard a guess that you actually want to be passing `rmse` as the
first argument:

    
    
    result=minimize(rmse, alpha, (fit,), bounds=[(0,1)], method='SLSQP')
    

After all, the `rmse` function is giving you the error value and that is what
you are minimising in such an optimisation.

