高误差华宇模型 - Python

数据挖掘 Python 有马 统计模型
2022-02-15 00:42:40

我有一个时间序列数据。它有每日频率。

我想用 ARIMA 模型预测下周或下个月的数据。

在此处输入图像描述

这是我的时间序列数据的图表:

在此处输入图像描述

首先,我使用 stats 模型中的方法seasonal_decompose 来检查趋势/会话性/残差,如下所示:

from statsmodels.tsa.seasonal import seasonal_decompose
result = seasonal_decompose(df['n_transactions'], model='add')
result.plot(); 

在此处输入图像描述

我检查我的数据是否是固定的:

from statsmodels.tsa.stattools import adfuller

def adf_test(series,title=''):
    """
    Pass in a time series and an optional title, returns an ADF report
    """
    print(f'Augmented Dickey-Fuller Test: {title}')
    result = adfuller(series.dropna(),autolag='AIC') # .dropna() handles differenced data

    labels = ['ADF test statistic','p-value','# lags used','# observations']
    out = pd.Series(result[0:4],index=labels)

    for key,val in result[4].items():
        out[f'critical value ({key})']=val

    print(out.to_string())          # .to_string() removes the line "dtype: float64"

    if result[1] <= 0.05:
        print("Strong evidence against the null hypothesis")
        print("Reject the null hypothesis")
        print("Data has no unit root and is stationary")
    else:
        print("Weak evidence against the null hypothesis")
        print("Fail to reject the null hypothesis")
        print("Data has a unit root and is non-stationary")

adf_test(df['n_transactions'])

Augmented Dickey-Fuller Test: 
ADF test statistic       -3.857922
p-value                   0.002367
# lags used              12.000000
# observations          737.000000
critical value (1%)      -3.439254
critical value (5%)      -2.865470
critical value (10%)     -2.568863
Strong evidence against the null hypothesis
Reject the null hypothesis
Data has no unit root and is stationary

我使用 auto_arima 来获得模型的最佳参数:

from pmdarima import auto_arima      
auto_arima(df['n_transactions'],seasonal=True, m = 7).summary()

在此处输入图像描述

我用这个参数训练我的模型:

train = df.loc[:'2020-05-12']
test = df.loc['2020-05-13':]

model = SARIMAX(train['n_transactions'],order=(1, 1, 1))
results = model.fit()
results.summary()

我计算预测:

start=len(train)
end=len(train)+len(test)-1
predictions = results.predict(start=start, end=end, dynamic=False, typ='levels').rename('SARIMA(0,1,3)(1,0,1,12) Predictions')


ax = test['n_transactions'].plot(legend=True,figsize=(12,6),title=title)
predictions.plot(legend=True)
ax.autoscale(axis='x',tight=True)
ax.set(xlabel=xlabel, ylabel=ylabel);

在此处输入图像描述

但是模型不能得到好的结果,为什么?

编辑

正如您建议的那样,我使用而不是计算我为此获得的收入,这可能是问题所在:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

但是该模型并没有获得好的结果:

在此处输入图像描述

我可以从这里得出什么结论?

2个回答

你似乎有一个时间序列的计数引用上面的书:

本书中讨论的所有方法都假设数据具有连续的样本空间。但数据通常以计数的形式出现。例如,我们可能希望预测每天进入商店的顾客数量。我们可以有 0, 1, 2, , , 客户,但我们不能有 3.45693 客户。

作者提出了一种使用 Croston 方法的方法,通常应用于具有大量零的时间序列。

您的数据看起来像一个计数过程。默认的 ARIMA 参数假定一个正态分布的连续误差项。所以标准的 ARIMA 模型使用这个假设。据我所知,有一些 ARIMA 模型将泊松分布作为错误术语,但我想你应该在谷歌上搜索计数过程的时间序列。像 pyflux 这样的东西可以工作。