y=csvread('Task_1.csv');
figure(1)
plot(y);

Y=y(1,4:end)';
X=[y(1,3:end-1)', y(1,2:end-2)', y(1,1:end-3)'];

N = length(y);

t0 = 300;

P_1 = inv(X'*X);
theta_1 = (P_1*X'*Y);
lambda=0.8;
theta_array=zeros(3,N);
current_index=t0;

for i = (t0+1):N-3
    current_index=current_index+1;
    phi = X(i,:)';
    y_current = y(i);
    P_1 = (1/lambda)*(P_1 - (P_1*(phi*phi')*P_1)/(lambda + phi'*P_1*phi));
    theta_1 = theta_1 + P_1*phi*(y_current - theta_1'*phi);
    theta_array(1:3,current_index)=theta_1;
end

figure(2)
subplot(1,3,1);
plot(theta_array(1,:));
subplot(1,3,2);
plot(theta_array(2,:));
subplot(1,3,3);
plot(theta_array(3,:));

sim_x=zeros(1,N);
sim_x(1:6)=y(1:6);

for i=7:N
    %theta_array(1:3,i);
    sim_x(i)=theta_array(1,t0+1)*sim_x(i-1)+theta_array(2,t0+1)*sim_x(i-2)+theta_array(3,t0+1)*sim_x(i-3);
end

figure(3)
plot(sim_x(1:800));