【问题标题】:How to invert first order differencing in Python?如何在 Python 中反转一阶差分?
【发布时间】:2022-03-09 20:01:29
【问题描述】:

我想知道一种简单有效的方法来反转 python 中的一阶(滞后 1)线性差分数据。我有一个带有 3 个 exog 变量 a、b 和 c 的多元 TS。虽然有几个关于反函数的博客,但似乎都针对复杂的场景,我无法为我的问题找到一些帮助,这并不复杂。我是 python 新手,正在为我的学术工作写一篇论文。因此,希望与社区联系以获得简单的解决方案。

我正在使用向量自动回归模型进行预测。如果我的以下编码有任何问题,请提出其他建议。

diff = originaldata.diff().dropna() 


model = VAR(diff)
result = model.fit()
fcast = result.forecast(diff.values[-1:], steps=2)
dataframe = pd.DataFrame(fcast, index=originaldata.index[-2:], 
columns = originaldata.columns

考虑到将差异预测累积添加到最后的累积观察中,我是否应该采用累积总和来回滚差异?

fcast_cs = dataframe.cumsum()

还有我如何使用这里的反函数回到原来的形式?

【问题讨论】:

    标签: python-3.x time-series inverse


    【解决方案1】:
    def invert_transformation(df_train, df_forecast, second_diff=False):
        """Revert back the differencing to get the forecast to original scale."""
        df_fc = df_forecast.copy()
        columns = df_train.columns
        for col in columns:        
            # Roll back 2nd Diff
            if second_diff:
                df_fc[str(col)+'_1d'] = (df_train[col].iloc[-1]-df_train[col].iloc[-2]) + df_fc[str(col)+'_2d'].cumsum()
            # Roll back 1st Diff
            df_fc[str(col)+'_forecast'] = df_train[col].iloc[-1] + df_fc[str(col)+'_1d'].cumsum()
        return df_fc
    

    refer link

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多