Last answered:

09 Oct 2020

Posted on:

07 Oct 2020

0

How to define a 2d array?

There is some problem during defining 2d array:
model_arma = [[0 in x in range(10)] for y in range(10)]
results_arma = [[0 in x in range(10)] for y in range(10)]
def arma(i,j):
model_arma[i][j]=ARMA(df.market_value, order=(i,j))
results_arma[i][j]=model_arma[i][j].fit()
return print("\n ARMA(",i,")(",j,"): \t LL=",
round(results_arma[i][j].llf,3),
"\t AIC= ", round(results_arma[i][j].aic,3))


This error is coming:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-59-df8ddbd5de4e> in <module>
----> 1 model_arma = [[0 in x in range(10)] for y in range(10)]
      2 results_arma = [[0 in x in range(10)] for y in range(10)]
      3 def arma(i,j):
      4     model_arma[i][j]=ARMA(df.market_value, order=(i,j))
      5     results_arma[i][j]=model_arma[i][j].fit()

<ipython-input-59-df8ddbd5de4e> in <listcomp>(.0)
----> 1 model_arma = [[0 in x in range(10)] for y in range(10)]
      2 results_arma = [[0 in x in range(10)] for y in range(10)]
      3 def arma(i,j):
      4     model_arma[i][j]=ARMA(df.market_value, order=(i,j))
      5     results_arma[i][j]=model_arma[i][j].fit()

TypeError: argument of type 'int' is not iterable

What is my mistake?
2 answers ( 0 marked as helpful)
Instructor
Posted on:

08 Oct 2020

0
Hey Tushar,   Can you provide a little more feedback with what you're trying to achieve in the first two lines?   It looks like you're trying to set up two "empty" 10 by 10 lists rather than arrays. If you want to define two such arrays, you can do so with the help of the numpy package and add:  
import numpy as np
model_arma = np.empty(shape = (10,10))
results_arma = np.empty(shape = (10,10))
  Hope this helps! Best, 365 Vik
Instructor
Posted on:

09 Oct 2020

0

Hello again!

If you want to use lists, you can do so the following way:

model_arma = []          # Define model_arma as a list
results_arma = []        # Define results_arma as a list

def arma(i,j):
    model_arma.append(ARMA(df.ret_vol, order = (i,j))) # The append method adds the input to the back of the list
    results_arma.append(model_arma[-1].fit(start_ar_lags = 11)) # [-1] will always refer to the latest value we just added to the list
    
    return print("\n ARMA(",i,",",j,"): \t LL=",
                 round(results_arma[-1].llf,3),
                 "\t AIC= ", round(results_arma[-1].aic,3))

Hope this helps!
Best,
365 Vik

P.S. Additionally, if you want to run multiple different ARMA models simultaneously, you can check out the AutoARIMA section of the course. 

Submit an answer