我得到了单变量时间序列建模(自回归模型)的过程。在这个过程中,我想使用 while 循环生成 100 个观察结果。import numpy as np np.random.seed(17)T = 200alpha1 = 0.8alpha2 = 0.15u = 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 npnp.random.seed(17)T = 200alpha1 = 0.8alpha2 = 0.15u = np.random.randn(T)y = np.zeros (T)t = 2while t <=102 : y[t] = alpha1*y[t-1] + alpha2*y[t-2] + u[t] y = y[:] print(y)
1 回答
catspeake
TA贡献1111条经验 获得超0个赞
您应该while通过递增t变量来结束循环,否则它将始终等于 2。因此,您的while循环将如下所示:
# initialize y, alpha1y, alpha2y and u
t = 2
while t <= 102: # I prefer t < 103 (=102 + 1) for no reason at all
y[t] = alpha1y[t-1] + alpha2y[t-2] + u[t]
t += 1 # assuming you are moving one step at a time
添加回答
举报
0/150
提交
取消