**AR2SIM.OMS**

Script File:

# Description:
# Generates realizations of an AR(2) process.
#
# The AR(2) process has specified covariance at lags 0, 1, and 2.
# We compare the specified covariance at each lag with the average of
# x(n) * x(n + lag).
#
clear
# number of points in each simulated realization
N = 50
# number of realizations
M = 9
# covariance of process: x(n) = sqrt(.75) * w(n) + .5 * x(n - 1)
r = {1., .5, .25}
# Simulate M realizations of length N.
x = arcov(N, M, r)
# set format for printing
format real "f10.5"
# number of covariances specified
m = rowdim(r)
# initialize sum as zero for each lag
total_sum = fill(0., 1, m)
# loop over realizations
print "Sample covariances for each realization"
for j =1 to M begin
# dimension the sample covariance for this realization
s = fill(0., 1, m)
# loop over lag value
for lag = 0 to m - 1 begin
# copy of this realization from index 1 to N - lag
xb = x.blk(1, j, N - lag, 1)
# copy of this realization from index 1 + lag to N
xf = x.blk(1 + lag, j, N - lag, 1)
# sample covariance for this lag is average of xb(i) * xf(i)
s(lag + 1) = xb' * xf / (N - lag)
end
# print sample covariance for this realization
write("screen", s)
# include in the total sum
total_sum = total_sum + s
end
print "Average of sample covariances"
write("screen", total_sum / M)
print "True covariance for each lag for the AR process"
write("screen", r')

Output:

Sample covariances for each realization
0.85329 0.34820 0.14347
0.99553 0.49420 0.17096
1.20703 0.55181 0.58899
0.81702 0.17073 0.15266
1.09309 0.68059 0.40324
0.90018 0.26048 0.10037
0.75273 0.31750 0.02205
1.26920 0.79067 0.32335
1.08788 0.40272 0.07164
Average of sample covariances
0.99733 0.44632 0.21964
True covariance for each lag for the AR process
1.00000 0.50000 0.25000