使用scipy.optimize优化3个系数

时间:2014-03-16 00:32:49

标签: python numpy scipy mathematical-optimization

我一直在尝试优化三个系数。但我无法解决我面临的错误。 以下是代码。 'y'是预测函数,它采用时间序列数据列表和系数列表,然后在计算后返回预测列表。 x是历史数据。和coeffList是具有3个系数作为值的列表。

'mape'函数计算实际数据和预测数据之间的差异。 在优化中,我试图最小化'mape'函数的输出。 作为约束,所有三个系数都大于0且小于1.

from __future__ import division
import numpy as np
from scipy.optimize import minimize



#coeffList[0] = alpha
#coeffList[1] = beta
#coeffList[2] =gamma

def mape(x, coeffList):
    diff = abs(y(x,coeffList)-x)
    print("np.mean(diff/x) : ", np.mean(diff/x))
    return np.mean(diff/x)


#Holt Winters-Multiplicative



def y(x, coeffList , debug=True):

    c =4 
    #Compute initial b and intercept using the first two complete c periods.
    xlen =len(x)
    print("xlen : ", xlen)
    #if xlen % c !=0:
    #    return None
    fc =float(c)
    xbar2 =sum([x[i] for i in range(c, 2 * c)])/ fc
    print("xbar2 : ",xbar2)

    xbar1 =sum([x[i] for i in range(c)]) / fc


    print("xbar1 : ", xbar1)
    b0 =(xbar2 - xbar1) / fc
    if debug: print ("b0 = ", b0)

    #Compute for the level estimate a0 using b0 above.
    tbar  =sum(i for i in range(1, c+1)) / fc
    print("tbar : ",tbar)
    a0 =xbar1  - b0 * tbar
    if debug: print ("a0 = ", a0)

    #Compute for initial indices - seasonality
    I =[x[i] / (a0 + (i+1) * b0) for i in range(0, xlen)]
    if debug: print ("Initial indices = ", I)

    S=[0] * (xlen+ c)

    for i in range(c):
        S[i] =(I[i] + I[i+c]) / 2.0
        print ("S[",i,"]=", S[i])

    #Normalize so S[i] for i in [0, c)  will add to c.
    tS =c / sum([S[i] for i in range(c)])
    print("tS : ", tS)
    for i in range(c):
        S[i] *=tS
        if debug: print ("Normalized S[",i,"]=", S[i])

    # Holt - winters proper ...
    if debug: print( "Use Holt Winters formulae")


    At =a0
    Bt =b0
    #y =[0] * (xlen) 
    y = np.empty(len(x),float)
    for i in range(xlen):
        Atm1 =At # a[0] = a0
        Btm1 =Bt # b[0] = b0

        At =coeffList[0] * x[i] / S[i] + (1.0-coeffList[0]) * (Atm1 + Btm1)


        Bt =coeffList[1] * (At - Atm1) + (1- coeffList[1]) * Btm1

        S[i+c] =coeffList[2] * x[i] / At + (1.0 - coeffList[2]) * S[i]




        y[i]=(a0 + b0 * (i+1)) * S[i]

    return y

# the time-series data.
coeff = [0.2, 0.3, 0.4]

x =[146, 96, 59, 133, 192, 127, 79, 186, 272, 155, 98, 219]
test = y(x,coeff)
print("x : ", x)
print("y : ",test)


#optimization


result = minimize(mape, coeff, (x,), bounds =[(0,1),(0,1), (0,1)], method='SLSQP')
opt = result.x
print("opt : ", result.x)

这是我的错误信息:

Traceback (most recent call last):
  File "C:\Users\SEC\Desktop\HDWeathProgram\testing_Optimization_HWM.py", line 135, in <module>
    result = minimize(mape, coeff, (x,), bounds =[(0,1),(0,1), (0,1)], method='SLSQP')
  File "C:\Python27\lib\site-packages\scipy\optimize\_minimize.py", line 364, in minimize
    constraints, **options)
  File "C:\Python27\lib\site-packages\scipy\optimize\slsqp.py", line 354, in _minimize_slsqp
    fx = func(x)
  File "C:\Python27\lib\site-packages\scipy\optimize\optimize.py", line 261, in function_wrapper
    return function(x, *args)
  File "C:\Users\SEC\Desktop\HDWeathProgram\testing_Optimization_HWM.py", line 12, in mape
    diff = abs(y(x,coeffList)-x)
  File "C:\Users\SEC\Desktop\HDWeathProgram\testing_Optimization_HWM.py", line 30, in y
    xbar2 =sum([x[i] for i in range(c, 2 * c)])/ fc
IndexError: index 4 is out of bounds for axis 0 with size 3

我有什么问题?我非常感谢您的任何评论

1 个答案:

答案 0 :(得分:1)

生成IndexError: index 4 is out of bounds for axis 0 with size 3的呼叫是最小化呼叫。这是因为您正在最小化的目标函数尝试最小化第一个参数。

您的边界似乎想要最小化coeff(三维事物),但目前您正在尝试最小化mape的第一个参数x。因此,您的函数的输入参数顺序错误。您可以快速解决此问题(或重新定义mape(将第一行更改为def mape(x, coeffList):)。

def mape_reversed(coeffList, x):                                                         
    return mape(x, coeffList)

result = minimize(mape_reversed, coeff, (x,),  bounds =[(0,1),(0,1), (0,1)], method='SLSQP')