在单变量时间序列建模中生成观测值

我得到了单变量时间序列建模(自回归模型)的过程。在这个过程中,我想使用 while 循环生成 100 个观察结果。


import numpy as np 

np.random.seed(17)

T = 200

alpha1 = 0.8

alpha2 = 0.15

u = np.random.randn(T)

y = np.zeros (T)


for t in np.arange(2,200):

    y[t] = alpha1*y[t-1] + alpha2*y[t-2] + u[t]

    

y = y[100:]


# Write a while loop which generates 100 observations from AR(2) defined above

# This is my first try. However, it seems that I am initiating an infinite loop for some reason. Does anybody have a suggestion on how to generate 100 observations from the process described above?


import numpy as np


np.random.seed(17)

T = 200

alpha1 = 0.8

alpha2 = 0.15

u = np.random.randn(T)

y = np.zeros (T)

t = 2

while t <=102 :   

 y[t] = alpha1*y[t-1] + alpha2*y[t-2] + u[t]

    y = y[:]

    print(y)


临摹微笑
浏览 83回答 1
1回答

catspeake

您应该while通过递增t变量来结束循环,否则它将始终等于 2。因此,您的while循环将如下所示:# initialize y, alpha1y, alpha2y and ut = 2while t <= 102: # I prefer t < 103 (=102 + 1) for no reason at all&nbsp; &nbsp; y[t] = alpha1y[t-1] + alpha2y[t-2] + u[t]&nbsp; &nbsp; t += 1 # assuming you are moving one step at a time
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python