AIC 和 BIC

Python 中的 ARIMA 模型

James Fulton

Climate informatics researcher

AIC——赤池信息准则

  • AIC 越低,模型越好
  • AIC 倾向选择更简单、阶数更低的模型
Python 中的 ARIMA 模型

BIC——贝叶斯信息准则

  • 与 AIC 非常相似
  • BIC 越低,模型越好
  • BIC 倾向选择更简单、阶数更低的模型
Python 中的 ARIMA 模型

AIC 与 BIC 对比

  • BIC 比 AIC 更偏好简单模型
  • AIC 更擅长选择预测模型
  • BIC 更擅长选择解释性模型
Python 中的 ARIMA 模型

statsmodels 中的 AIC 与 BIC

# Create model
model = ARIMA(df, order=(1,0,1))

# Fit model results = model.fit()
# Print fit summary print(results.summary())
                            Statespace Model Results                           
==============================================================================
Dep. Variable:                      y   No. Observations:                 1000
Model:               SARIMAX(2, 0, 0)   Log Likelihood               -1399.704
Date:                Fri, 10 May 2019   AIC                           2805.407
Time:                        01:06:11   BIC                           2820.131
Sample:                    01-01-2013   HQIC                          2811.003
                         - 09-27-2015                                         
Covariance Type:                  opg
Python 中的 ARIMA 模型

statsmodels 中的 AIC 与 BIC

# Create model
model = ARIMA(df, order=(1,0,1))

# Fit model results = model.fit()
# Print AIC and BIC print('AIC:', results.aic) print('BIC:', results.bic)
AIC: 2806.36 
BIC: 2821.09
Python 中的 ARIMA 模型

按 AIC 和 BIC 搜索

# Loop over AR order
for p in range(3):
    # Loop over MA order
    for q in range(3):

# Fit model model = ARIMA(df, order=(p,0,q)) results = model.fit()
# print the model order and the AIC/BIC values print(p, q, results.aic, results.bic)
0 0 2900.13 2905.04
0 1 2828.70 2838.52
0 2 2806.69 2821.42
1 0 2810.25 2820.06
1 1 2806.37 2821.09
1 2 2807.52 2827.15
...
Python 中的 ARIMA 模型

按 AIC 和 BIC 搜索

order_aic_bic =[]
# Loop over AR order
for p in range(3):
    # Loop over MA order
    for q in range(3):

# Fit model model = ARIMA(df, order=(p,0,q)) results = model.fit()
# Add order and scores to list order_aic_bic.append((p, q, results.aic, results.bic))
# Make DataFrame of model order and AIC/BIC scores
order_df = pd.DataFrame(order_aic_bic, columns=['p','q', 'aic', 'bic'])
Python 中的 ARIMA 模型

按 AIC 和 BIC 搜索

# Sort by AIC
print(order_df.sort_values('aic'))
   p  q      aic      bic
7  2  1  2804.54  2824.17
6  2  0  2805.41  2820.13
4  1  1  2806.37  2821.09
2  0  2  2806.69  2821.42
...
# Sort by BIC
print(order_df.sort_values('bic'))
   p  q      aic      bic
3  1  0  2810.25  2820.06
6  2  0  2805.41  2820.13
4  1  1  2806.37  2821.09
2  0  2  2806.69  2821.42
...
Python 中的 ARIMA 模型

非平稳的模型阶数

# Fit model
model = ARIMA(df, order=(2,0,1))
results = model.fit()
ValueError: Non-stationary starting autoregressive parameters
found with `enforce_stationarity` set to True.
Python 中的 ARIMA 模型

当某些阶数不可用时

# Loop over AR order
for p in range(3):
    # Loop over MA order
    for q in range(3):

# Fit model model = ARIMA(df, order=(p,0,q)) results = model.fit() # Print the model order and the AIC/BIC values print(p, q, results.aic, results.bic)
Python 中的 ARIMA 模型

当某些阶数不可用时

# Loop over AR order
for p in range(3):
    # Loop over MA order
    for q in range(3):

try: # Fit model model = ARIMA(df, order=(p,0,q)) results = model.fit() # Print the model order and the AIC/BIC values print(p, q, results.aic, results.bic)
except: # Print AIC and BIC as None when fails print(p, q, None, None)
Python 中的 ARIMA 模型

¡Vamos a practicar!

Python 中的 ARIMA 模型

Preparing Video For Download...