STL (Seasonal-Trend decomposition procedure based on Loess) [1] 为时序分解中一种常见的算法,基于LOESS将某时刻的数据Yv分解为趋势分量(trend component)、周期分量(seasonal component)和余项(remainder component)
1.功能
分解函数成三部分:趋势、周期、和剩余部分(一般指噪声,均值为0)
分解就是将时序数据分离成不同的成分,分解有:长期趋势Trend、季节性seasonality和随机残差residuals
返回包含三个部分 trend(趋势部分) , seasonal(季节性部分) 和residual (残留部分)
传入:一个序列,可以是时间序列
输出:趋势、周期、和剩余部分 三部分
2.做法
乘法序列分解
乘法序列 = Trend * Seasonality * Error
result_mul = seasonal_decompose(df['value'], model='multiplicative', extrapolate_trend='freq')
plt.rcParams.update({'figure.figsize': (10, 10)})
result_mul.plot().suptitle('Multiplicative Decompose')
plt.show()
加法序列分解
加法序列 = Trend + Seasonality + Error
result_add = seasonal_decompose(df['value'], model='additive', extrapolate_trend='freq')
plt.rcParams.update({'figure.figsize': (10, 10)})
result_add.plot().suptitle('Additive Decompose')
plt.show()
如果有ValueError: You must specify a period or x must be a pandas object with a DatetimeIndex with a freq not set to None报错,可以设置period参数,比如:
seasonal_decompose(price_df['price'], model='additive', extrapolate_trend='freq', period=3)