clear all,clc
load sgu_tot_run_baaff.mat U hx PI y0 R
%produced by running sgu_tot_run_baaff.m in z:\uribe\tot\ier\mfiles\

nv = size(hx,1); %number of variables in the SVAR
N = 1e4; %number of replicaitons
f = 0.66; %confidence band width
n1 = round((1-f)/2*N);
n2 = round((1+f)/2*N);
ncou = length(U); %number of countries

T = 11; %length of impulse responses


for k=1:ncou
hx_k = hx(:,:,k);
PI_k = PI(:,:,k);
u = U{k};
e = u * (PI_k*inv(diag(std(u))))';
y0_k = y0(k,:)';
IR_sim = zeros(T,nv);
IRs_sim = zeros(T,nv);


for n=1:N

Y = bootstrap_simu(hx_k,e,y0_k);
[shx,sPI,sR2,sA,sB,sstdu,sU] = svar_estim(Y,R,0);
x0 = sPI(:,1)/sPI(1,1);
x0s = sPI(:,2)/sPI(2,2);

x =ir(eye(nv),shx,x0,T);
xs =ir(eye(nv),shx,x0s,T);
IR_sim(:,:,n) = x(:,1:nv);
IRs_sim(:,:,n) = xs(:,1:nv);
end
sIR_sim = sort(IR_sim,3);
sIRs_sim = sort(IRs_sim,3);

IR1(:,:,k) = sIR_sim(:,:,n1);
IR2(:,:,k) = sIR_sim(:,:,n2);

IRs1(:,:,k) = sIRs_sim(:,:,n1);
IRs2(:,:,k) = sIRs_sim(:,:,n2);
k
end %for k=1:ncou

%load impulse responses estimated on actual data
load sgu_tot_run_baaff.mat IR IRs country_name
%produced by running sgu_tot_run_baaff.m in z:\uribe\tot\ier\mfiles\

readme = 'Each IR object is a 3 dimensional matrix: row=year, column=[tot baaff-spread tby y c i rer], page=country; country_name is a cell containing country names in the order they appear in the pages of the IR matrices'

save bootstrap_ir_baaff.mat  IR IRs  IR1 IR2 IRs1 IRs2 country_name readme