

function [BIAS] = boot_firststage(A,U,q,p,T,V,hh,IRF,B,X,R,prior);


N = R;

EE = U(1:q,:);

[bootstat11,bootsam11] = bootstrp(N,'corrcoef',EE(1,:)); % Note that statistic has no meaning here.  We're just getting indexes for the bootstrap samples
[n,m] = size(bootsam11);

m = R;
iter = 0;
for i = 1:m     
    for j = 1:(T-p)    
        ee(:,j) = EE(:,bootsam11(j,i)); 
    end
    
    Y = [];
    for j = 1:p
        Y = [X(j,:)';Y];
    end
    
    
    for j = 1:T-p
        ysim(:,j) = V(1:q,1) + A(1:q,:)*Y(1:p*q) + ee(:,j);
        Y = [ysim(:,j);Y];
    end    
    z = ysim';    
    [AA,SIGMAA,UU,VV]=olsvarc(z,p); % estimates VAR
%     if max(abs(eig(A)))<0.999
        iter = iter + 1;
        I(:,:,iter) = AA; 
%     end
end

for v = 1:(p*q)
    for j = 1:p*q
        II(v,j) = mean(I(v,j,:));
    end
end
BIAS = A-II;

