简体   繁体   English

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

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

I am completely new to programming languages and I have picked up Python for backtesting a trading strategy (because I heard it is relatively easy). 我是编程语言的新手,并且选择了Python来回测交易策略(因为我听说它相对容易)。 I have made some progress in learning the basics, however I am currently stuck at performing an ADFuller tests on a timeseries dataframe. 我在学习基础知识方面取得了一些进步,但是我目前仍坚持在时间序列数据帧上执行ADFuller测试。

This is how my Dataframe looks 这就是我的数据框的外观

Now I need to run ADF test on the columns - "A-Btd", "A- Ctd" and so on (I have 66 columns like these).I would like to get the test statistic/output for each of them. 现在,我需要在“ A-Btd”,“ A-Ctd”等列上运行ADF测试(我有66列这样的列)。我想获取每个列的测试统计信息/输出。

I tired using lines such as cadfs = [ts.adfuller(df1)] . 我厌倦了使用cadfs = [ts.adfuller(df1)] Since, I lack the expertise I am not able to adjust the code as per my dataframe. 由于我缺乏专业知识,因此无法根据数据框调整代码。

I apologize in advance if I have missed out some important information I have to give out. 如果我错过了一些我必须提供的重要信息,我谨此致歉。 Please leave a comment and I will provide it asap. 请发表评论,我将尽快提供。

Thanks a lot in advance! 在此先多谢!

If you have to do it for so many, I would try putting the results in a dict, like this: 如果您必须做很多次,我会尝试将结果放入dict中,如下所示:

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])

Obviously specifying other settings as desired, eg tsa.adfuller(df[col], autolag='BIC') . 显然,可以根据需要指定其他设置,例如tsa.adfuller(df[col], autolag='BIC') Or if you don't want all the output and would rather just parse each column to find out if it's stationary or not, the test statistic is the first entry in the tuple returned by adfuller() , so you could just use tsa.adfuller(df[col])[0] and test it against your threshold to get a boolean result, then make that the value in your dict. 或者,如果您不希望所有输出,而只是解析每列以了解其是否固定,则测试统计量是adfuller()返回的元组中的第一项,因此您可以使用tsa.adfuller(df[col])[0] ,并针对您的阈值对其进行测试,以获取布尔结果,然后将该值设为dict。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM