Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Holt Winters Forecasting - IndexError
#1
Hi All,
I'm currently working on a Holt Winters forecast, however i'm experiencing difficulty in enforcing the trend component I believe? When I try running the script, I receive the message "IndexError: list index out of range". I was wondering if anyone has had any similar problems with enforcing this forecast or encountered the same difficulties? I've been working on this for about 3 months and haven't seemed to make any progress, although I've researched online and I'm 80% sure i'm on the right lines? Wall Wall

from __future__ import division
from sys import exit
from math import sqrt
from numpy import array
from scipy.optimize import fmin_l_bfgs_b
import scipy.optimize
from datetime import datetime
import pandas as pd
import matplotlib.pyplot as pylot
from scipy import stats
import numpy
import numpy as np

data=pd.read_csv('S:/My Documents/bricks.csv', encoding = "ISO-8859-1", low_memory = False)

def RMSE(params, *args):
	Y = args[0]
	type = args[1]
	rmse = 0
	if type == 'linear':
		alpha, beta = params
		a = [Y[0]]
		b = [Y[1] - Y[0]]
		y = [a[0] + b[0]]
		for i in range(len(Y)):
			a.append(alpha * Y[i] + (1 - alpha) * (a[i] + b[i]))
			b.append(beta * (a[i + 1] - a[i]) + (1 - beta) * b[i])
			y.append(a[i + 1] + b[i + 1])
	else:
		alpha, beta, gamma = params
		m = args[2]		
		a = [sum(Y[0:m]) / float(m)]
		b = [(sum(Y[m:2 * m]) - sum(Y[0:m])) / m ** 2]
		if type == 'additive':
			s = [Y[i] - a[0] for i in range(m)]
			y = [a[0] + b[0] + s[0]]
			for i in range(len(Y)):
				a.append(alpha * (Y[i] - s[i-m]) + (1 - alpha) * (a[i] + b[i]))
				b.append(beta * (a[i + 1] - a[i]) + (1 - beta) * b[i])
				s.append(gamma * (Y[i] - a[i] - b[i]) + (1 - gamma) * s[i-m])
				y.append(a[i + 1] + b[i + 1] + s[i + 1-m])
		elif type == 'multiplicative':
			s = [Y[i] / a[0] for i in range(m)]
			y = [(a[0] + b[0]) * s[0]]
			for i in range(len(Y)):
				a.append(alpha * (Y[i] - s[i-m]) + (1 - alpha) * (a[i] + b[i]))
				b.append(beta * (a[i + 1] - a[i]) + (1 - beta) * b[i])
				s.append(gamma * (Y[i] - a[i] - b[i]) + (1 - gamma) * s[i-m])
				y.append(a[i + 1] + b[i + 1] + s[i + 1-m])		
			else:
				exit('Type must be either linear, additive or multiplicative')
	rmse = sqrt(sum([(m - n) ** 2 for m, n in zip(Y, y[:-1])]) / len(Y))
	return rmse
def additive(x, m, fc, alpha = None, beta = None, gamma = None):
	print(fc)
	Y = x[:]
	if (alpha == None or beta == None or gamma == None):
		initial_values = array([0.3, 0.1, 0.1])
		boundaries = [(0, 1), (0, 1), (0, 1)]
		type = 'additive'
		parameters = fmin_l_bfgs_b(RMSE, x0 = initial_values, args = (Y, type, m), bounds = boundaries, factr=10,approx_grad=True)
		alpha, beta, gamma = parameters[0]
	a = [sum(Y[0:m]) / float(m)]
	# b = [(sum(Y[m:2 * m]) - sum(Y[0:m])) / m ** 2]
	b = [(sum(Y[m:2 * m]) - sum(Y[0:m])) / float(m**2)]
	s = [Y[i] - a[0] for i in range(m)]
	y = [a[0] + b[0] + s[0]]
	rmse = 0
	for i in range(len(Y) + fc):
		if i == len(Y):
				Y.append(a[-1] + b[-1] + s[-m])
		a.append(alpha * (Y[i+1] - s[i+1-m]) + (1 - alpha) * (a[i] + b[i])) 
		b.append(beta * (a[i + 1] - a[i]) + (1 - beta) * b[i]) #correct
		s.append(gamma * (Y[i+1] - a[i+1] - b[i+1]) + (1 - gamma) * s[i-m+1])
		y.append(a[i + 1] + m*b[i + 1] + s[i + 2-m])	
	rmse = sqrt(sum([(m - n) ** 2 for m, n in zip(Y[:-fc], y[:-fc - 1])]) / len(Y[:-fc]))
	return Y[-fc:]

a=list(data.columns.values)
del a[0]
iterable=a
result=[additive(list(data[x]), 12,12, None, None) for x in iterable]
print(result)

Many thanks if anyone can help with this Shy

Ronnie!
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)