如何 运行 使用 statsmodels 库对时间序列数据进行 ADFuller 测试?

How to run an ADFuller test on timeseries data using statsmodels library?

我对编程语言完全陌生,我选择了 Python 来回测交易策略(因为我听说它相对容易)。我在学习基础知识方面取得了一些进展,但是我目前仍停留在对时间序列数据帧执行 ADFuller 测试。

This is how my Dataframe looks

现在我需要对列进行 运行 ADF 测试 - "A-Btd"、"A- Ctd" 等等(我有 66 个这样的列)。我想进行测试statistic/output 他们每个人。

我厌倦了使用 cadfs = [ts.adfuller(df1)] 这样的行。因为,我缺乏专业知识,所以无法根据我的数据框调整代码。

如果我错过了一些我必须提供的重要信息,我提前道歉。请留言,我会尽快提供。

提前致谢!

如果你必须为这么多人做这件事,我会尝试将结果放入字典中,如下所示:

import statsmodels.tsa.stattools as tsa

df = ... #load your dataframe
adf_results = {}

for col in df.columns.values:  #or edit this for a subset of columns first
    adf_results[col] = tsa.adfuller(df[col])

显然根据需要指定其他设置,例如tsa.adfuller(df[col], autolag='BIC')。或者,如果您不想要所有输出,而只想解析每一列以了解它是否静止,则测试统计量是 adfuller() 返回的元组中的第一个条目,因此您可以只使用 tsa.adfuller(df[col])[0] 并根据您的阈值对其进行测试以获得布尔结果,然后将其设为您的字典中的值。