我正在尝试使用ARMA模型来建立一个预测能源生产的模型。
我可以用于训练的数据如下所示;
ID Label House Year Month Temperature Daylight EnergyProduction0 0 1 2011 7 26.2 178.9 7401 1 1 2011 8 25.8 169.7 7312 2 1 2011 9 22.8 170.2 6943 3 1 2011 10 16.4 169.1 6884 4 1 2011 11 11.4 169.1 6505 5 1 2011 12 4.2 199.5 763...............11995 19 500 2013 2 4.2 201.8 63811996 20 500 2013 3 11.2 234 77811997 21 500 2013 4 13.6 237.1 75811998 22 500 2013 5 19.2 258.4 83811999 23 500 2013 6 22.7 122.9 586
如上所示,我可以使用2011年7月至2013年5月的数据进行训练。通过这些训练数据,我希望预测2013年6月每500户的能源生产情况。
问题在于时间序列数据不是平稳的,并且具有趋势成分和季节性成分(我通过以下方式进行了检查)。
import csvimport numpy as npimport pandas as pdimport matplotlib.pyplot as pltdata_train = pd.read_csv('../../data/training_dataset_500.csv')rng=pd.date_range('7/1/2011', '6/1/2013', freq='M')house1 = data_train[data_train.House==1][['EnergyProduction','Daylight','Temperature']].set_index(rng)fig, axes = plt.subplots(nrows=1, ncols=3)for i, column in enumerate(house1.columns): house1[column].plot(ax=axes[i], figsize=(14,3), title=column)plt.show()
使用这些数据,我无法通过ARMA模型获得良好的预测结果。因此,我希望去除趋势成分和季节性成分,使时间序列数据变得平稳。我尝试解决这个问题,但无法去除这些成分并使其平稳。
回答:
我建议使用Hodrick-Prescott(HP)滤波器,这在宏观计量经济学中被广泛用于将长期趋势成分与短期波动分离。它在statsmodels.api.tsa.filters.hpfilter
中实现。
import pandas as pdimport matplotlib.pyplot as pltimport statsmodels.api as smdf = pd.read_csv('/home/Jian/Downloads/data.csv', index_col=[0])# 获取部分数据x = df.loc[df.House==1, 'Daylight']# hp-filter,根据月度数据的建议设置参数lamb=129600x_smoothed, x_trend = sm.tsa.filters.hpfilter(x, lamb=129600)fig, axes = plt.subplots(figsize=(12,4), ncols=3)axes[0].plot(x)axes[0].set_title('原始数据')axes[1].plot(x_trend)axes[1].set_title('趋势')axes[2].plot(x_smoothed)axes[2].set_title('平滑数据')