我得到了單變量時間序列建模(自回歸模型)的過程。在這個過程中,我想使用 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
提交
取消