嘿伙计我正在使用statsmodel库中的arma_order_select_ic来计算ARMA模型的(p,q)顺序,我用循环来遍历数据框每列中的不同公司。代码如下:
import pandas as pd
from statsmodels.tsa.stattools import arma_order_select_ic
df = pd.read_csv("Adjusted_Log_Returns.csv", index_col = 'Date').dropna()
main_df = pd.DataFrame()
for i in range(146):
order_selection = arma_order_select_ic(df.iloc[i].values, max_ar = 4,
max_ma = 2, ic = "aic")
ticker = [df.columns[i]]
df_aic_min = pd.DataFrame([order_selection["aic_min_order"]], index =
ticker)
main_df = main_df.append(df_aic_min)
main_df.to_csv("aic_min_orders.csv")
代码运行良好,我在结束时得到了csv文件中的所有结果,但令我困惑的是,当我为单个公司计算for循环之外的(p,q)时,我会得到不同的结果
order_selection = arma_order_select_ic(df["ABL"].values, max_ar = 4,
max_ma = 2, ic = "aic")
公司ABL的订单在for循环中计算时为(1,1),而在其外部计算时为(4,1)。
所以我的问题是我做错了什么或为什么会这样?任何帮助将不胜感激。
先谢谢
答案 0 :(得分:0)
从代码中可以清楚地看到,您正试图在列上找到ARMA模型的参数'数据,但它不是代码正在做的事情:您在循环中找到行的参数。
考虑一下:
import pandas as pd
df = pd.DataFrame({'a': [3, 4]})
>>> df.iloc[0]
a 3
Name: 0, dtype: int64
>>> df['a']
0 3
1 4
Name: a, dtype: int64
您应该将代码更改为
for c in df.columns:
order_selection = arma_order_select_ic(df[c].values, max_ar = 4,
max_ma = 2, ic = "aic")
ticker = [c]