使用 python 仅在趋势和残差中分解时间序列

Decompose a time series only in trend and residual with python

我只想分解趋势和残差(没有季节性)的时间序列。到目前为止,我知道我可以使用 statsmodels 来分解时间序列,但这包括季节性。有没有没有季节性分解它的方法?

我查看了文档 (https://www.statsmodels.org/dev/generated/statsmodels.tsa.seasonal.seasonal_decompose.html) seasonal_decompose 允许不同类型的季节性 ("additive", "multiplicative"}) 但我没有看到关键字排除季节性的论点。

下面是我的问题的玩具模型。具有趋势但没有季节性的时间序列。如果我们要删除季节性因素,我认为我们会更合适。

import numpy as np
import pandas as pd
import statsmodels.api as sm
from statsmodels.tsa.arima_model import ARMA
from matplotlib import pylab as plt

#defining the trend function
def trend(t, amp=1):
    return amp*(1 + t)

n_time_steps = 100
amplitud=1
#initializing the time series
time_series = np.zeros(n_time_steps)
time_series[0] = trend(0, amplitud)

alpha = 0.1
#making the time series
for t in range(1,n_time_steps):
    time_series[t] = (1 - alpha)*time_series[t - 1] + alpha*trend(t, amp=amplitud) + alpha*np.random.normal(0,25)

#passing the time series to a pandas format
dates = sm.tsa.datetools.dates_from_range('2000m1', length=len(time_series))
time_series_pd= pd.Series(time_series, index=dates)
#decomposing the time series
res = sm.tsa.seasonal_decompose(time_series_pd)
res.plot()

我认为 seasonal_decompose 功能不能在没有季节性组件的情况下使用。

您是否考虑过使用其他功能,例如 statsmodels.tsa.tsatools.detrend?这可以通过多项式拟合完成您想要的操作。