Mô hình ARIMA với Python
James Fulton
Climate informatics researcher
# Tạo mô hình model = ARIMA(df, order=(1,0,1))# Fit mô hình results = model.fit()# In tóm tắt kết quả 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
# Tạo mô hình model = ARIMA(df, order=(1,0,1))# Fit mô hình results = model.fit()# In AIC và BIC print('AIC:', results.aic) print('BIC:', results.bic)
AIC: 2806.36
BIC: 2821.09
# Lặp theo bậc AR for p in range(3): # Lặp theo bậc MA for q in range(3):# Fit mô hình model = ARIMA(df, order=(p,0,q)) results = model.fit()# in bậc mô hình và giá trị AIC/BIC 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
...
order_aic_bic =[] # Lặp theo bậc AR for p in range(3): # Lặp theo bậc MA for q in range(3):# Fit mô hình model = ARIMA(df, order=(p,0,q)) results = model.fit()# Thêm bậc và điểm vào danh sách order_aic_bic.append((p, q, results.aic, results.bic))
# Tạo DataFrame chứa bậc mô hình và điểm AIC/BIC
order_df = pd.DataFrame(order_aic_bic, columns=['p','q', 'aic', 'bic'])
# Sắp xếp theo 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
...
# Sắp xếp theo 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
...
# Fit mô hình
model = ARIMA(df, order=(2,0,1))
results = model.fit()
ValueError: Tham số tự hồi quy khởi tạo không dừng
được phát hiện khi `enforce_stationarity` đặt là True.
# Lặp theo bậc AR for p in range(3): # Lặp theo bậc MA for q in range(3):# Fit mô hình model = ARIMA(df, order=(p,0,q)) results = model.fit() # In bậc mô hình và giá trị AIC/BIC print(p, q, results.aic, results.bic)
# Lặp theo bậc AR for p in range(3): # Lặp theo bậc MA for q in range(3):try: # Fit mô hình model = ARIMA(df, order=(p,0,q)) results = model.fit() # In bậc mô hình và giá trị AIC/BIC print(p, q, results.aic, results.bic)except: # In AIC và BIC là None khi lỗi print(p, q, None, None)
Mô hình ARIMA với Python