我想将我的时间序列分解为线性趋势(趋势将是线性回归模型的结果),然后分解季节性和残差。
我探索的软件包与我的期望不符:
统计模型/季节性分解:将趋势视为意甲的移动平均线。 https://github.com/statsmodels/statsmodels/blob/master/statsmodels/tsa/seasonal.py
stl分解/分解:将趋势视为加权回归(权重是距离的三角函数)。我对Lowess回归的解释。 https://github.com/jrmontag/STLDecompose/blob/master/stldecompose/stl.py
我可以使用这些函数中的参数吗? 还是我需要建立自己的分解?
同时,我编写了分解函数。请毫不犹豫地对其进行优化:
import numpy as np
import pandas as pd
from pandas.core.nanops import nanmean as pd_nanmean
from sklearn.linear_model import LinearRegression
def madi_decompose(df, period=52):
lm = LinearRegression()
y = df.values.reshape((len(df),1))
X = np.array([i for i in range(len(df))]).reshape((len(df),1))
lm.fit(X,y)
trend = lm.predict(X)
detrended = y - trend
period_averages = np.array([pd_nanmean(detrended[i::period]) for i in range(period)])
period_averages -= np.mean(period_averages)
seasonal = np.tile(period_averages, len(df) // period + 1)[:len(df)]
seasonal = seasonal.reshape(len(df),1)
resid = detrended - seasonal
return trend, seasonal, resid