我現(xiàn)在深深的發(fā)現(xiàn)我老公就是個(gè)杠精,我說啥他都要杠,不杠就活不了了。
生氣。
這里不多說指數(shù)平滑的原理,直接給一個(gè)結(jié)論。
指數(shù)平滑預(yù)測模型的使用場合
| 預(yù)測模型選擇 | 長期趨勢 | 季節(jié)效應(yīng) |
|---|---|---|
| 一次指數(shù)平滑 | 無 | 無 |
| 二次指數(shù)平滑 | 有 | 無 |
| 三次指數(shù)平滑 | 有/無 | 有 |
上述結(jié)論出自王燕的《應(yīng)用時(shí)間序列分析》,第四版,196頁。
請王越不要在跟我抬杠,千恩萬謝。
一次指數(shù)平滑預(yù)測值恒為常數(shù),所以最好只做1期預(yù)測。
最近我司又讓我做時(shí)間序列了,就目前我的水平而言,做出來的效果最好的是xgboost算法。
出于興趣,自己研究了holt-winter的使用方法。
首先,這里使用的是統(tǒng)計(jì)學(xué)模型的python庫。
from statsmodels.tsa.holtwinters import ExponentialSmoothing
其次,下面貼出代碼實(shí)現(xiàn)方法。
# -*- encoding:utf-8 -*-
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.holtwinters import ExponentialSmoothing
# 1、對數(shù)據(jù)的預(yù)處理
input_data = open("ftproot.txt", mode='r').read().split("\n")
time_data = []
for i in range(len(input_data)):
time_data.append(input_data[i].split(","))
# 全部數(shù)據(jù)
all_data = []
for i in range(len(time_data)):
all_data.append(float(time_data[i][1]))
# 分一部分出來作為train數(shù)據(jù)
train_data = []
test_data = []
train_data.extend([all_data[i] for i in range(0, 1334)])
test_data.extend([all_data[i] for i in range(1334, len(all_data))])
# 2、模型參數(shù)
ets3 = ExponentialSmoothing(train_data, trend='add', seasonal='add', seasonal_periods=24)
# 3、擬合模型
r3 = ets3.fit()
# 4、預(yù)測
pred3 = r3.predict(start=len(train_data), end=len(all_data)-1)
# 5、畫圖,可以忽略
pd.DataFrame({
'origin': test_data,
'pred': pred3
}).plot(legend=True)
plt.show()
print(pred3)

數(shù)據(jù)樣式如下,一小時(shí)一個(gè)點(diǎn),逗號(hào)前面是時(shí)間,后面是數(shù)值。
第三,下面給出代碼的具體含義。
1、先拿到數(shù)據(jù),一小時(shí)一個(gè)點(diǎn),去除時(shí)間標(biāo)記,拿到數(shù)值,按照時(shí)間順序,存入數(shù)組y3中。
2、設(shè)置模型的參數(shù)
看一下源碼里怎么說的
Holt Winter's Exponential Smoothing
Parameters
----------
endog : array-like
Time series
trend : {"add", "mul", "additive", "multiplicative", None}, optional
Type of trend component.
damped : bool, optional
Should the trend component be damped.
seasonal : {"add", "mul", "additive", "multiplicative", None}, optional
Type of seasonal component.
seasonal_periods : int, optional
The number of seasons to consider for the holt winters.
Returns
-------
results : ExponentialSmoothing class
Notes
-----
This is a full implementation of the holt winters exponential smoothing as
per [1]. This includes all the unstable methods as well as the stable methods.
The implementation of the library covers the functionality of the R
library as much as possible whilst still being pythonic.
參數(shù):
第一個(gè)endog,當(dāng)然是時(shí)間序列的數(shù)據(jù)了,array-like的形式。
第二個(gè)trend是趨勢,有三種可選項(xiàng),就是加法趨勢和乘法趨勢還有None。
第三個(gè)damped是衰減,Boolean,是否對趨勢進(jìn)行衰減。
第四個(gè)seasonal是季節(jié)性(周期),也是三種選項(xiàng),加法、乘法還有None。
第五個(gè)seasonal_periods,季節(jié)性周期,int型,holt-winter要考慮的季節(jié)的數(shù)量。簡單來說,多少點(diǎn)是一個(gè)周期?你可以設(shè)定為一天,一星期,一個(gè)月,一年等等
參數(shù)比較簡單,一目了然。
我這邊設(shè)置的趨勢和季節(jié)都是加性的。因?yàn)閿?shù)據(jù)是一小時(shí)一個(gè)點(diǎn)的,所以周期我給的是24,即一天為一個(gè)周期。
3、擬合模型
4、預(yù)測

看這個(gè)效果還不錯(cuò)是吧,那是因?yàn)榍懊娑荚谟?xùn)練集里面,真正展現(xiàn)實(shí)力的是后面分叉的部分。效果很差。
如果只是把后面的部分截出來看,是不是對比更明顯一點(diǎn)。

下圖是我用同樣的數(shù)據(jù),holt-winter和xgboost預(yù)測效果以及真實(shí)值的對比效果
