function y = genar1(alpha, rho, sig2, T, y0, repeat); % input: alpha - constatnt % rho - coefficient on y(t-1) term % sig2 - variance on error term for your process % T - number of observations in the series % y0 - first value in the generated sequence % repeat - how many series of length T to generate % output: y - the simulated ar(1) process if (rho > 1); error('You asked for an explosive AR(1) series!'); end %initialize the y vector and fill it with 9999 so we know when we've %replaced the original values. y = 9999*ones(T,repeat); y(1,1:repeat) = y0; for r = 1:1:repeat for t = 2:1:T; y(t,r) = alpha + rho*y(t-1,r) + normrnd(0,sqrt(sig2)); end end