Generation of error matrix from AR(1) model, issue

2 views (last 30 days)
I want to generate a (t by n) matrix of errors (ε) , row by row, which are drawn from an AR(1) model ε_t = ρ*ε_t-1 + u_t. The problem I encountered is that from row t=3 until row t=T I get the same errors as is shown in the picture, which is odd. I get this for any t and n. Any help is appreciated! Thank you very much for your time! :)
t=5; n=10;
u_t = randn(1,n); %innovation term
epsilon = zeros(t,n); %preallocation
rho=0.5; %parameter ρ for the AR(1) model
epsilon(1,:) = randn(1,n)*sqrt(1/(1-rho^2)); %starting value of epsilon
%Draw epsilon in a vectorized way
T=2:t;
epsilon(T,:)=rho*epsilon(T-1,:) + u_t; %AR(1) model
  4 Comments
Ioannis Leivadiotis
Ioannis Leivadiotis on 16 Mar 2022
Thank you very much for your answer! It seems like the loop works better than the vectorization but I don't understand why.
VBBV
VBBV on 30 Mar 2022
if this solves your problem, please accept the answer, thanks :)

Sign in to comment.

Accepted Answer

VBBV
VBBV on 14 Mar 2022
Edited: VBBV on 14 Mar 2022
t=5; n=10;
u_t = randn(1,n) %innovation term
u_t = 1×10
0.1509 0.4115 -0.3629 -0.0909 0.6274 -0.3900 -1.6479 -0.4891 0.5298 -0.0713
epsilon = zeros(t,n); %preallocation
rho=0.5; %parameter ρ for the AR(1) model
epsilon(1,:) = randn(1,n)*sqrt(1/(1-rho^2)) %starting value of epsilon
epsilon = 5×10
1.2113 1.2759 -1.2414 0.4632 1.4058 -2.6131 0.3366 -0.6824 2.0854 0.4962 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
%Draw epsilon in a vectorized way
for k = 2 :t
epsilon(k,:)=rho*epsilon(k-1,:) + u_t ;%AR(1) model
end
epsilon
epsilon = 5×10
1.2113 1.2759 -1.2414 0.4632 1.4058 -2.6131 0.3366 -0.6824 2.0854 0.4962 0.7566 1.0494 -0.9836 0.1407 1.3303 -1.6966 -1.4796 -0.8303 1.5725 0.1768 0.5292 0.9362 -0.8547 -0.0206 1.2926 -1.2383 -2.3878 -0.9042 1.3160 0.0171 0.4155 0.8795 -0.7903 -0.1012 1.2737 -1.0092 -2.8418 -0.9412 1.1878 -0.0628 0.3587 0.8512 -0.7581 -0.1415 1.2643 -0.8946 -3.0688 -0.9596 1.1237 -0.1027
% in vectorized way
T = 2:t;
epsilon(2:t,:) = rho*epsilon(T-1,:) + u_t
epsilon = 5×10
1.2113 1.2759 -1.2414 0.4632 1.4058 -2.6131 0.3366 -0.6824 2.0854 0.4962 0.7566 1.0494 -0.9836 0.1407 1.3303 -1.6966 -1.4796 -0.8303 1.5725 0.1768 0.5292 0.9362 -0.8547 -0.0206 1.2926 -1.2383 -2.3878 -0.9042 1.3160 0.0171 0.4155 0.8795 -0.7903 -0.1012 1.2737 -1.0092 -2.8418 -0.9412 1.1878 -0.0628 0.3587 0.8512 -0.7581 -0.1415 1.2643 -0.8946 -3.0688 -0.9596 1.1237 -0.1027
one approach is to use loop and test it ,

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!