Time series code not quite right, thoughts?

1 view (last 30 days)
Olivia
Olivia on 21 Sep 2015
Commented: Rena Berman on 24 Jan 2017
AR(1) process:
T=10000;
e1 = randn(T/4,1);
e2 = randn(T/4,1);
e3 = randn(T/4,1);
e4 = randn(T/4,1);
e=[e1,e2,e3,e4];
a1=ones(T/4,1);
a2=ones(T/4,1);
a3=ones(T/4,1);
a4=ones(T/4,1);
a=[a1,a2,a3,a4];
rho = 1.5;
time = 1:T-1; % for plotting purposes only
Y(1) = 0; %Initial condition
for i=2:T
Y(i,1) = a + e(i) + rho*Y(i-1,1);
end
plot(time, Y)
_
Am getting the error: Subscripted assignment dimension mismatch. How should I correct my for loop to generate the attached?

Answers (2)

Jae Song
Jae Song on 21 Sep 2015
I don't know if this is the plot you were trying to generate. I made the following correction to make the plot to work.
1) a and e seem same size; so I made a and e using the same index. 2) y seems an array 3) plot: time and y should have same size.
T=10000;
e1 = randn(T/4,1); e2 = randn(T/4,1); e3 = randn(T/4,1); e4 = randn(T/4,1);
e=[e1,e2,e3,e4];
a1=ones(T/4,1); a2=ones(T/4,1); a3=ones(T/4,1); a4=ones(T/4,1);
a=[a1,a2,a3,a4];
rho = 1.5; time = 1:T-1; % for plotting purposes only
Y(1) = 0; %Initial condition
for i=2:T
Y(i) = a(i) + e(i) + rho*Y(i-1);
end
plot(time, Y(2:end))
  1 Comment
Joseph Cheng
Joseph Cheng on 22 Sep 2015
the rho of 1.5 really makes it out of hand. as the e(i) cannot compensate for a 150% change from the previous number. are you sure you want a rho of 1.5?

Sign in to comment.


Joseph Cheng
Joseph Cheng on 22 Sep 2015
are you looking to something more like this?
T=10000;
%why generate e it 4 times when randn can generate it for you
e = randn(T/4,4);
%generate some offset within the 4 different sections
a=[randi(10,1,4)];
a = repmat(a,T/4,1);
rho = 1.5/100; %rho is probably in percentage
time = 1:T; % for plotting purposes only
%preallocate Y
Y = zeros(size(a));
%generate a new initial condition for each section or keep Y =0;'
Y(1,:) = randi(10,1,4); %Initial condition
%perform this if each segment isn't influenced by each other
for i=2:T/4
Y(i,:) = a(i,:) + e(i,:) + rho*Y(i-1,:);
end
% perform this if each segment is influenced by their predecessor.
for i=2:T
Y(i) = a(i) + e(i) + rho*Y(i-1);
end
plot(time, Y(:))

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!