Skip to content

Instantly share code, notes, and snippets.

@greeness
Created November 16, 2012 03:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greeness/126ec1249d0bb9ed3ef2 to your computer and use it in GitHub Desktop.
Save greeness/126ec1249d0bb9ed3ef2 to your computer and use it in GitHub Desktop.
holt_winter_example
import matplotlib.pyplot as plt
def holt_alg(h, y_last, y_pred, T_pred, alpha, beta):
pred_y_new = alpha * y_last + (1-alpha) * (y_pred + T_pred * h)
pred_T_new = beta * (pred_y_new - y_pred)/h + (1-beta)*T_pred
return (pred_y_new, pred_T_new)
def smoothing(t, y, alpha, beta):
# initialization using the first two observations
pred_y = y[1]
pred_T = (y[1] - y[0])/(t[1]-t[0])
y_hat = [y[0], y[1]]
# next unit time point
t.append(t[-1]+1)
for i in range(2, len(t)):
h = t[i] - t[i-1]
pred_y, pred_T = holt_alg(h, y[i-1], pred_y, pred_T, alpha, beta)
y_hat.append(pred_y)
return y_hat
data_t = range(15)
data_y = [5,6,15,20,21,22,26,42,45,60,55,58,55,50,49]
import matplotlib.pyplot as plt
plt.plot(data_t, data_y, 'x-')
plt.hold(True)
pred_y = smoothing(data_t, data_y, alpha=.8, beta=.5)
plt.plot(data_t[:len(pred_y)], pred_y, 'rx-')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment