原文链接:How to Build Exponential Smoothing Models Using Python: Simple Exponential Smoothing, Holt, and…html
今年前12个月,iPhone XS将售出多少部?在埃隆·马斯克(Elon musk)在直播节目中吸食大麻以后,特斯拉的需求趋势是什么?这个冬天会暖和吗?(我住在加拿大。)若是你对这些问题感到好奇,指数平滑法能够经过创建模型来预测将来。python
指数平滑方法为过去的观测分配指数递减的权重。获得的观测值越近,权重就越大。例如,与12个月前的观测结果相比,对上个月的观测结果给予更大的权重是合理的。函数
上图为指数平滑权值从过去到如今。优化
本文将说明如何使用Python
和Statsmodel
构建简单指数平滑
、Holt
和Holt- winters
模型。对于每一个模型,演示都按照如下方式组织。ui
模型操做方法+Python代码spa
Statsmodels是一个Python模块,它为实现许多不一样的统计模型提供了类和函数。咱们须要将它导入Python代码,以下所示。code
import matplotlib.pyplot as plt from statsmodels.tsa.holtwinters import ExponentialSmoothing, SimpleExpSmoothing, Holt
咱们示例中的源数据以下:htm
data = [253993,275396.2,315229.5,356949.6,400158.2,442431.7,495102.9,570164.8,640993.1,704250.4,767455.4,781807.8,776332.3,794161.7,834177.7,931651.5,1028390,1114914]
咱们能够先看看折线图blog
plt.plot(data);
对于没有明显趋势或季节规律的预测数据,SES是一个很好的选择。预测是使用加权平均来计算的,这意味着最大的权重与最近的观测值相关,而最小的权重与最远的观测值相关get
其中0≤α≤1是平滑参数。
权重减少率由平滑参数α控制。 若是α很大(即接近1),则对更近期的观察给予更多权重。 有两种极端状况:
这里咱们运行三种简单指数平滑变体:
fit1
中,咱们明确地为模型提供了平滑参数\(α= 0.2\)fit2
中,咱们选择\(α= 0.6\)fit3
中,咱们使用自动优化,容许statsmodels自动为咱们找到优化值。 这是推荐的方法。# Simple Exponential Smoothing fit1 = SimpleExpSmoothing(data).fit(smoothing_level=0.2,optimized=False) # plot l1, = plt.plot(list(fit1.fittedvalues) + list(fit1.forecast(5)), marker='o') fit2 = SimpleExpSmoothing(data).fit(smoothing_level=0.6,optimized=False) # plot l2, = plt.plot(list(fit2.fittedvalues) + list(fit2.forecast(5)), marker='o') fit3 = SimpleExpSmoothing(data).fit() # plot l3, = plt.plot(list(fit3.fittedvalues) + list(fit3.forecast(5)), marker='o') l4, = plt.plot(data, marker='o') plt.legend(handles = [l1, l2, l3, l4], labels = ['a=0.2', 'a=0.6', 'auto', 'data'], loc = 'best', prop={'size': 7}) plt.show()
咱们预测了将来五个点。
Holt扩展了简单的指数平滑(数据解决方案没有明确的趋势或季节性),以便在1957年预测数据趋势.Holt的方法包括预测方程和两个平滑方程(一个用于水平,一个用于趋势):
其中\(0≤α≤1\)是水平平滑参数,\(0≤β*≤1\)是趋势平滑参数。
对于长期预测,使用Holt方法的预测在将来会无限期地增长或减小。 在这种状况下,咱们使用具备阻尼参数\(0 <φ<1\)的阻尼趋势方法来防止预测“失控”。
一样,这里咱们运行Halt方法的三种变体:
fit1
中,咱们明确地为模型提供了平滑参数\(α= 0.8\),\(β* = 0.2\)。fit2
中,咱们使用指数模型而不是Holt的加法模型(默认值)。fit3
中,咱们使用阻尼版本的Holt附加模型,但容许优化阻尼参数\(φ\),同时固定\(α= 0.8\),\(β* = 0.2\)的值。data_sr = pd.Series(data) # Holt’s Method fit1 = Holt(data_sr).fit(smoothing_level=0.8, smoothing_slope=0.2, optimized=False) l1, = plt.plot(list(fit1.fittedvalues) + list(fit1.forecast(5)), marker='^') fit2 = Holt(data_sr, exponential=True).fit(smoothing_level=0.8, smoothing_slope=0.2, optimized=False) l2, = plt.plot(list(fit2.fittedvalues) + list(fit2.forecast(5)), marker='.') fit3 = Holt(data_sr, damped=True).fit(smoothing_level=0.8, smoothing_slope=0.2) l3, = plt.plot(list(fit3.fittedvalues) + list(fit3.forecast(5)), marker='.') l4, = plt.plot(data_sr, marker='.') plt.legend(handles = [l1, l2, l3, l4], labels = ["Holt's linear trend", "Exponential trend", "Additive damped trend", 'data'], loc = 'best', prop={'size': 7}) plt.show()
(彼得·温特斯(Peter Winters)是霍尔特(Holt)的学生。霍尔特-温特斯法最初是由彼得提出的,后来他们一块儿研究。多么美好而伟大的结合啊。就像柏拉图遇到苏格拉底同样。)
Holt-Winters的方法适用于具备趋势和季节性的数据,其包括季节性平滑参数\(γ\)。 此方法有两种变体:
在这里,咱们运行完整的Holt-Winters方法,包括趋势组件和季节性组件。 Statsmodels容许全部组合,包括以下面的示例所示:
fit1
中,咱们使用加法趋势,周期season_length = 4
的加性季节和Box-Cox
变换。fit2
中,咱们使用加法趋势,周期season_length = 4
的乘法季节和Box-Cox
变换。fit3
中,咱们使用加性阻尼趋势,周期season_length = 4
的加性季节和Box-Cox
变换。fit4
中,咱们使用加性阻尼趋势,周期season_length = 4
的乘法季节和Box-Cox
变换。data_sr = pd.Series(data) fit1 = ExponentialSmoothing(data_sr, seasonal_periods=4, trend='add', seasonal='add').fit(use_boxcox=True) fit2 = ExponentialSmoothing(data_sr, seasonal_periods=4, trend='add', seasonal='mul').fit(use_boxcox=True) fit3 = ExponentialSmoothing(data_sr, seasonal_periods=4, trend='add', seasonal='add', damped=True).fit(use_boxcox=True) fit4 = ExponentialSmoothing(data_sr, seasonal_periods=4, trend='add', seasonal='mul', damped=True).fit(use_boxcox=True) l1, = plt.plot(list(fit1.fittedvalues) + list(fit1.forecast(5)), marker='^') l2, = plt.plot(list(fit2.fittedvalues) + list(fit2.forecast(5)), marker='*') l3, = plt.plot(list(fit3.fittedvalues) + list(fit3.forecast(5)), marker='.') l4, = plt.plot(list(fit4.fittedvalues) + list(fit4.forecast(5)), marker='.') l5, = plt.plot(data, marker='.') plt.legend(handles = [l1, l2, l3, l4, l5], labels = ["aa", "am", "aa damped", "am damped","data"], loc = 'best', prop={'size': 7}) plt.show()
总而言之,咱们经过3个指数平滑模型的机制和python代码。 以下表所示,我提供了一种为数据集选择合适模型的方法。
总结了指数平滑方法中不一样份量形式的平滑参数。
指数平滑是当今行业中应用最普遍、最成功的预测方法之一。如何预测零售额、游客数量、电力需求或收入增加?指数平滑是你须要展示将来的超能力之一。