Reputation: 1280
I want to simulate a second order difference equation from the book "Time Series Analysis" of James Hamilton.In page 13 there is an example :
Y[t] = 0.6Y[t-1] + 0.2Y[t-2] + w[t]
where w[t] is a random standard normal error.
My effort is in the code below.But I have two questions:
1) is that correct ?
2) how the range(n) changes ? For example 1:n or 2:n or (0,n-1) and (0,n-2) ? I cannot understand completely how these ranges affect the for loop.
Any help?
import numpy as np
np.random.seed(123)
n = 1000
phi1 = 0.6
phi2 = 0.2
y = w = np.random.normal(size=n)
for t in range(n):
y[t] = phi1* y[t-1] +phi2* y[t-2] + w[t]
Upvotes: 1
Views: 244
Reputation: 61
At first glance, the for loop you have created starts from the first index, when you try to access t-2 when t = 0 the pointer moves to the second value from the end, which I don't think is what you intend to do, to fix this, try starting from 2. as in ==> for t in range(2,n). Furthermore, I did not quite understand your second question, but from what I understood your asking how the number T is incremented, which would be in the order {1,2,3,...,n}
Upvotes: 1