clear

%
% simple case with three points
%
c=2.5

x0 = [-1,0,1]';
y0=[-1,0.5,-1]' ;

%x0 = [-1, 1,1,-1]';
%y0 = [-1,-1,1, 1]' ;
nt=length(x0);

h=plot(x0,y0,'o');
set(h,'markersize',10)
set(gca,'xlim',[-c,c],'ylim',[-c,c])
grid
xlabel('x');ylabel('y')
pause

b0 = regress(y0,[x0,ones(nt,1)]),
xx = [-c:0.2:c]';
hold on
plot(xx,[xx,ones(size(xx))]*b0,'--')
hold off
xlabel('x');ylabel('y')
pause


yp = zeros(size(y0))+NaN;

for ii=1:nt
  ind = [1:nt]~=ii
  x = x0(ind);
  y = y0(ind);
  b = regress(y,[x,ones(nnz(ind),1)])
  
  h=plot(x0,y0,'o');
  set(h,'markersize',10)
  set(gca,'xlim',[-c,c],'ylim',[-c,c])
  grid
  hold on
  h=plot(x0(1:(ii-1)),yp(1:(ii-1)),'ro');
  set(h,'markersize',10)
  plot(xx,[xx,ones(size(xx))]*b,'r--')
  hold off
  xlabel('x');ylabel('y')
  pause

  yp(ii) = [x0(ii),1]*b;
  
  hold on
  h=plot(x0(1:ii),yp(1:ii),'ro');
  set(h,'markersize',10)
  hold off
  xlabel('x');ylabel('y')
  pause
end


h=plot(x0,y0,'o');
set(h,'markersize',10)
set(gca,'xlim',[-c,c],'ylim',[-c,c])
grid
hold on
h=plot(x0(1:ii),yp(1:ii),'ro');
set(h,'markersize',10)
hold off
xlabel('x');ylabel('y')


%
% what is the behavior when the sample size is larger?
%
