【问题标题】:minimize alpha in exponential smoothing最小化指数平滑中的 alpha
【发布时间】:2014-03-09 23:14:15
【问题描述】:

我是在 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

【问题讨论】:

  • 你试过scipy.optimize.minimize吗?有关详细信息,请参阅scipy.optimize tutorial
  • @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[0]x[3]
  • @askewchan 没错。如果 x = [3, 4, 5, 6],则 y[i+1] = x[i]*alpha + y[i]*(alpha-1)。 x 是给定值的列表,y 是拟合值的列表。最小化 (y[i]- x[i])/x[i] 会给我最好的 alpha。

标签: python math numpy scipy


【解决方案1】:

这应该可行。我认为没有比我制作的递归循环更好的方法来找到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

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

【讨论】:

  • 您必须将x 传递给最小化器,因为mape 依赖于xy(而y 又依赖于alpha)。当然,您可以将 x 设为在 mape 中定义为常量的全局变量,但函数式风格建议它应该作为参数传递。
  • 此外,当您执行print result 时,它会显示x: ... 并给出alpha 的最佳值,它被称为x 不是因为您的x,而是因为这就是最优参数被称为。您的输入也称为 x 纯属巧合。
【解决方案2】:
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)

【讨论】:

    猜你喜欢
    • 2014-09-26
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 2015-10-07
    • 2010-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多