%
%
%
%
function [NewsImpBoot,SurpriseImpBoot,FEVDNewsBoot,FEVDSurpriseBoot,Imp] = FAVARNewsBoot(Data, Z,variables,k,h,nrepli);
[T N] = size(Data);
r = size(Z, 2);
NewsImpBoot = zeros( N, 1, h+1,nrepli);
SurpriseImpBoot = NewsImpBoot;
[VarPa C X u] = VarParameters(Z,k,1);

W = [ones(T,1) Z];
AA = inv(W'*W)*W'*Data;
chi = W*AA;

Idio = Data - chi;

%plot(Idio(:,96))
%pause

for j=1:nrepli
    Z_boot = GenerateNewSeries(VarPa,C,X,u,k);
    W_boot = [ones(T-k,1) Z_boot];
    
    X_boot = W_boot*AA + Idio(k+1:end,:);
    
    [NewsImp,SurpriseImp,Nsh,Ssh,imp]  = FAVARNewsImp(X_boot, Z_boot, variables,k, h);
    %j
     mse = diag(imp(:,:,1)*imp(:,:,1)');
     for t = 2:h;
         temp = diag(imp(:,:,t)*imp(:,:,t)');
         mse(:,t) = mse(:,t-1)+temp;
     end
     var_news = diag(NewsImp(:,:,1)*NewsImp(:,:,1)');
     for t = 2:h;
         temp = diag(NewsImp(:,:,t)*NewsImp(:,:,t)');
         var_news(:,t) = var_news(:,t-1)+temp;
     end
     var_tech = diag(imp(:,1,1)*imp(:,1,1)');
     for t = 2:h;
         temp = diag(imp(:,1,t)*imp(:,1,t)');
         var_tech(:,t) = var_tech(:,t-1)+temp;
     end
     var_dec_news = 100*(var_news./mse);
     var_dec_tech = 100*(var_tech./mse);
%     
     %(var_dec_news(v_sel,[1 5 9 17 25 41]))
%     
     FEVDNewsBoot(:,:,j) = [0];var_dec_news;
     FEVDSurpriseBoot(:,:,j) = [0];var_dec_tech;  
     NewsImpBoot( :, :, :, j) = NewsImp;
     SurpriseImpBoot( :, :, :, j) = SurpriseImp;
     Imp(:,:,:,j) = imp;
end



