Created
August 6, 2019 17:29
-
-
Save marcopeix/e6fcaaece4446a40d7ac9ca7515deb37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def double_exponential_smoothing(series, alpha, beta): | |
result = [series[0]] | |
for n in range(1, len(series)+1): | |
if n == 1: | |
level, trend = series[0], series[1] - series[0] | |
if n >= len(series): # forecasting | |
value = result[-1] | |
else: | |
value = series[n] | |
last_level, level = level, alpha * value + (1 - alpha) * (level + trend) | |
trend = beta * (level - last_level) + (1 - beta) * trend | |
result.append(level + trend) | |
return result | |
def plot_double_exponential_smoothing(series, alphas, betas): | |
plt.figure(figsize=(17, 8)) | |
for alpha in alphas: | |
for beta in betas: | |
plt.plot(double_exponential_smoothing(series, alpha, beta), label="Alpha {}, beta {}".format(alpha, beta)) | |
plt.plot(series.values, label = "Actual") | |
plt.legend(loc="best") | |
plt.axis('tight') | |
plt.title("Double Exponential Smoothing") | |
plt.grid(True) | |
plot_double_exponential_smoothing(data.CLOSE, alphas=[0.9, 0.02], betas=[0.9, 0.02]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment