0

I'm new to Python and just doodled around to get to know the language. Basically, I wanted produce random walks and have them analyzed. However, I was not able to accomplish a list of fit objects with different ARMA model fits for different random walk realizations.

    import numpy as np
    import matplotlib.pyplot as plt
    import statsmodels.api as sm
    def randwalk(N,sigma):
        k=1
        R=np.zeros(N)
        while(k<N):
            R[k]=R[k-1]+np.random.randn(1)*sigma
            k+=1
        return R
    m=3
    N=100
    R1=np.zeros((N,m))
    for k in range(m): 
        R1[:,k]=randwalk(N,0.1)

    plt.plot(range(N),(R1))
    plt.show()
    ll= [(2,0),(2,0)]
    fit=[]
    for kk in range(len(ll)):
        fit[kk]= [sm.tsa.ARMA(R1[:,ii], order=ll[kk]).fit() for ii in range(m)]

Neither fit[kk] nor fit[kk][:] seem to work. I have looked into Creating a list of objects in Python and Nested lists python

Later, ll[1] will be different, e.g. (2,1). sm.tsa.ARMA seems to work if you run the loop body with kk=0 "by hand".

Eventually, I would like to access fit[:][:].bic values.

I'm really sorry, i feel this is a basic syntax question...

I'm using Canopy on Windows 7.

Edit: Also,

ll= [(2,0),(2,0)]
fit=[]
for kk in range(len(ll)):
    fit.append([sm.tsa.ARMA(R1[:,ii], order=ll[kk]).fit() for ii in range(m)])

doesn't work.

Community
  • 1
  • 1

1 Answers1

1

Lists, unlike matrices, have variable size and start empty, so accessing the third element of a new list is meaningless. You can either create a list of a predetermined (but not fixed) size ahead of time and then index into that, or you can append items to the end of the list:

# Create then assign
ll = [None] * some_length    # or ll = range(some_length)
for k in range(some_length):
    ll[k] = some_object[k]

# Append:
ll = list() # list() is equivalent to []
for k in range(some_length):
    ll.append(some_object[k])

I prefer the append approach, but that's just my preference.

alexkonradi
  • 750
  • 5
  • 5