我是新手,在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
答案 0 :(得分: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
请评论是否有任何混淆!
答案 1 :(得分:0)
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)