我正在尝试使用指数平滑法来平滑一个时间序列。
假设我的时间序列看起来像这样:
import pandas as pddata = [446.6565, 454.4733, 455.663 , 423.6322, 456.2713, 440.5881, 425.3325, 485.1494, 506.0482, 526.792 , 514.2689, 494.211 ]index= pd.date_range(start='1996', end='2008', freq='A')oildata = pd.Series(data, index)
我想得到该时间序列的平滑版本。
如果我做了类似这样的操作:
from statsmodels.tsa.api import ExponentialSmoothing fit1 = SimpleExpSmoothing(oildata).fit(smoothing_level=0.2,optimized=False)fcast1 = fit1.forecast(3).rename(r'$\alpha=0.2$')
它只输出了预测的三个值,而不是我原始时间序列的平滑版本。有什么方法可以得到我原始时间序列的平滑版本吗?
如果需要,我很乐意提供更多细节。
回答:
显然,你可以在模型的fittedvalues
属性中获取平滑值。
import pandas as pddata = [446.6565, 454.4733, 455.663 , 423.6322, 456.2713, 440.5881, 425.3325, 485.1494, 506.0482, 526.792 , 514.2689, 494.211 ]index= pd.date_range(start='1996', end='2008', freq='A')oildata = pd.Series(data, index)from statsmodels.tsa.api import SimpleExpSmoothingfit1 = SimpleExpSmoothing(oildata).fit(smoothing_level=0.2,optimized=False)# fcast1 = fit1.forecast(3).rename(r'$\alpha=0.2$')import matplotlib.pyplot as pltplt.plot(oildata)plt.plot(fit1.fittedvalues)plt.show()
它会生成:
文档中说明:
fittedvalues: ndarray
由指数平滑模型拟合的值数组。
请注意,你还可以使用fittedfcast
属性,它包含所有值加上第一个预测,或者使用fcastvalues
属性,它只包含预测值。