具有线性趋势的时间序列分解

时间:2019-03-21 11:08:05

标签: python time-series regression decomposition

我想将我的时间序列分解为线性趋势(趋势将是线性回归模型的结果),然后分解季节性和残差。

我探索的软件包与我的期望不符:

我可以使用这些函数中的参数吗? 还是我需要建立自己的分解?

同时,我编写了分解函数。请毫不犹豫地对其进行优化:

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

0 个答案:

没有答案