clear all
close all
clc

P=2;
x(1:P)=zeros(1,P);
a(1:P)=0.5*rand(1,P);
a(1:P)=[0.5 0.5]
sig=2;
d=[];
N=2000;

for i=1:N
  v=x(i:i+P-1);
  x(i+P)=a*v'+sig*randn(1,1);
  d=[d; x(i+P)];
  v2(i,:)=v;
end


aest=inv(v2'*v2)*v2'*d

polos=roots([1 -a])
disp('modulo polos')
abs(polos)
plot(x)

xest=aest'*v2';

hold on
plot(xest,'r')

varErr_est=mean((x(P+1:end)-xest).^2)
disp('varianza real')
sig^2