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.

3
  • What error do you get? Commented Aug 5, 2014 at 2:41
  • it just freezes until i restart the kernel Commented Aug 5, 2014 at 2:43
  • Ah, I just found out I can have a look at the details. The kernel (user Python environment) has terminated with error code -805306369. This may be due to a bug in your code or in the kernel itself. was returned on my try with .append Commented Aug 5, 2014 at 3:04

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.