Multiple inputs into Statsmodels ARIMA in Python

2019-06-13 23:19发布

问题:

I am trying to fit a ARIMA model with multiple inputs. As long as the input was a single array it worked fine.

Here, I was adviced to put input arrays into a multidimensional array-like structure. So I did:

import numpy as np
from statsmodels.tsa.arima_model import ARIMA

a = [1, 2, 3]
b = [4, 5, 6]

data = np.dstack([a, b])
for p in range(6):
    for d in range(2):
        for q in range(4):
            order = (p,d,q)         
            try:
                model = ARIMA(data, order=(p,d,q))
                print("this works:{}, {}, {} ".format(p,d,q))
            except:
                pass

However, the output of this script was this:

   this works:0, 0, 0

Obviously, there is something wrong (if p,d,q are all 0 then it is not working at all). Does anyone know what I am doing wrong?

Any advice that would point me to the right direction would be much appreciated.

回答1:

You need to have enough 'degrees of freedom' when modeling using ARIMA.

So, the issue with your code is that np.dstack produces the shape of the array as (1,3,2) which means it has only one data element. You need a minimum number of 6 data elements to be able to run the ARIMA model with p-value 5.

Example on array operations. I used np.vstack to produce as many as rows as possible.

Please run the code snippet below and you will understand.

import numpy as np
from statsmodels.tsa.arima_model import ARIMA

a = [1, 2]
b = [3, 4]
c = [5, 6]
d = [7, 8]

data = np.vstack([a, b, c, d])

print(data.shape)
print(data)

for p in range(4):
    for d in range(1):
        for q in range(2):
            order = (p,d,q)    
            try:
                model = ARIMA(data, order=(p,d,q))
                print("this works:{}, {}, {} ".format(p,d,q))
            except:
                print(order)
                print('reached exception')
                pass