Python 指数平滑中的最小化alpha

Python 指数平滑中的最小化alpha,python,math,numpy,scipy,Python,Math,Numpy,Scipy,我对在python上使用scipy和numpy是新手 我的问题:如何使用最佳alpha(水平平滑常数)最小化误差函数(平均绝对百分比误差,具体为MAPE)?因此,我试图通过MAPE获得最佳alpha 这是我的数学: x = [ 3, 4, 5, 6] y0 = x0 y1 = x0*alpha+ (1-alpha)*y0 MAPE = (y-x)/x [ This is an objective function and I am trying to solve for alpha here]

我对在python上使用scipy和numpy是新手

我的问题:如何使用最佳alpha(水平平滑常数)最小化误差函数(平均绝对百分比误差,具体为MAPE)?因此,我试图通过MAPE获得最佳alpha

这是我的数学:

x = [ 3, 4, 5, 6]
y0 = x0
y1 = x0*alpha+ (1-alpha)*y0

MAPE = (y-x)/x [ This is an objective function and I am trying to solve for alpha here]

Constraints1: alpha<1
Constrants2 : alpha>0
x=[3,4,5,6]
y0=x0
y1=x0*alpha+(1-alpha)*y0
MAPE=(y-x)/x[这是一个目标函数,我在这里尝试求解alpha]
约束1:alpha0

这应该行得通。我认为没有比我所做的递归循环更好的方法找到
y
。基本思想是,您需要将要最小化的对象转化为最小化参数(
alpha
)和任何其他(
x
)的函数。这就是我所说的
mape
。将
alpha
和额外参数(
x
)的初始猜测传递到最小值。由于您的约束只是边界,如果您使用
method='SLSQP'
,这很容易

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

def y(alpha, x):
    y = np.empty(len(x), float)
    y[0] = x[0]
    for i in xrange(1, len(x)):
        y[i] = x[i-1]*alpha + y[i-1]*(1-alpha)
    return y

def mape(alpha, x):
    diff = y(alpha, x) - x
    return np.mean(diff/x)

x = np.array([ 3, 4, 5, 6])
guess = .5
result = minimize(mape, guess, (x,), bounds=[(0,1)], method='SLSQP')
要获取您的信息,您可以执行以下操作:

print result
[alpha_opt] = result.x

如果有任何令人困惑的地方,请发表评论

你试过了吗?有关详细信息,请参阅@askewchan。我正在研究最小二乘拟合和单变量函数极小化。我一点运气都没有。你能指出正确的功能吗?谢谢你的回复我相信我的第一个链接就是你想要的功能。构建你想要最小化w.r.t.
alpha
的函数,将它和第一个猜测传递给
scipy.optimize.minimize
,它将返回你的最优
alpha
。我会提供更多帮助,但我不完全理解你所说的问题。
y
应该是长度为4的向量,其中
y[i+1]=x[i]*alpha+y[i]*(alpha-1)
?我只看到了
y[0]
y[1]
的定义,但没有看到
y[2]
y[3]
,但通过
x[3]
@askewchan可以看到
x[0]
,这是正确的。如果x=[3,4,5,6],那么y[i+1]=x[i]*alpha+y[i]*(alpha-1)。x为给定值列表,y为拟合值列表。最小化(y[i]-x[i])/x[i]会给我最好的alpha。你必须将
x
传递给最小化,因为
mape
依赖于
x
y
(而
y
又依赖于
alpha
)。当然,您可以使
x
成为一个全局变量,该变量在
mape
中定义为一个常量,但函数样式建议将其作为参数传递。此外,当您打印结果时,它会显示
x:…
,并给出
alpha
的最佳值,它被称为
x
,这不是因为您的
x
,而是因为这就是最佳参数的名称。您的输入也被称为
x
,这只是巧合。
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)
    #if xlen % c !=0:
    #    return None
    fc =float(c)
    xbar2 =sum([x[i] for i in range(c, 2 * c)])/ fc
    xbar1 =sum([x[i] for i in range(c)]) / fc
    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)
    a0 =xbar1  - b0 * tbar
    if debug: print ("a0 = ", a0)

    #Compute for initial indices
    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

    #Normalize so S[i] for i in [0, c)  will add to c.
    tS =c / sum([S[i] for i in range(c)])
    for i in range(c):
        S[i] *=tS
        if debug: print ("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
        Btm1 =Bt
        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


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("test : ",test)

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

opt = result.x
print("opt : ", result.x)