%sgu_tot_run.m estimates country-by-country  SVARs for the vector x_t = [tot_t tb_t gdp_t c_t i_t rer_t] with an intercept. The program delivers the matrices hx and PI in  x_t = hx x_{t-1} + PI eps_t, (constant omitted) where eps_t~(0,I).  It also computes the share of the variances of these 6 variables explained by tot shocks. 
%(c) Martín Uribe, December 2014

clear all

%Load detrended data. Baseline case: Quadraticly detrended data
load detrend_data.mat  DD country_name   ndate ntot ntb ngdp nc ni nrer
%DD contains the detrended data. It is a cell of order 1xncou, where ncou is the number of countries (38 in this case).  The order of countries is given in the string country_name. Each element of DD is a matrix. For each of these matrices, each row  is an observation (yearly) and each column is a variable: date, tot, trade balance,  gdp, consumption, investment, rer, and BAA-FF spread. The variables ndate, ntot, ngdp, nc, ni, nrer, and  nbaaff give the column number for the corresponding time series (e.g., ny=4 means that the 4th column of each matrix of DD is detrended GDP. Note: For each country, all time series have the same length (dictated by the series with the shortest number of observations available). However, each variable was individually detrended using the longest sample available.  Thenondetrended data are available as follows:
%All data except the BAA-FF spread: 
%these data is available in two formats 
%sgu_tot_data.mat  is in Matlab's .mat format. The same information is in spread sheet format in the file
%sgu_tot_data.xls
%In the file sgu_tot_data.mat, the variable datamatrix is a 52x8x38 matrix with rows-year, columns=[date tot tb gdp c i rer baaff], pages=countries.  country_name is a cell with country names in the same order as the pages of datamatrix.
%Data on the  BAA-FF spread: monthly information is in the file 
%BAAFFM.xls
%BAA-FF data converted to annual frequency are  stored in the file 
% baaff_annual.mat . In this file 
%the variable baaff_annual is the spread between  Moodys Seasoned Baa Corporate Bonds and the Federal Funds Rate. Frequency: Annual.  Sample 1955-2015. Units: percent. Raw data: FRED St Louis Fed

ncou = length(DD);
nv = size(DD{1},2)-2; %one eliminated  column is the year and the other is the baaff spread
v_share = zeros(ncou,nv);
var_svar_tot = zeros(nv,ncou);
var_svar = zeros(nv,ncou); %Total population variance implies by SVAR model 
sigx = zeros(nv,nv,ncou); %
ssigx = zeros(nv,nv,ncou); %

R2 = zeros(nv,ncou);
U = cell(0);
R = ones(nv);
R(1,2:end) = 0; %restriction on hx (tot univariate)
y0 = zeros(ncou,nv);
T = 11; %length of impulse responses;

for k=1:ncou
%construct current and lagged data for country k
d = DD{k}(:,[ntot ntb ngdp nc ni nrer]);
y0(k,:) = d(1,:);
 [hx(:,:,k),PI(:,:,k),R2(:,k),A(:,:,k),B(:,:,k),stdu(:,k),U{k}] = svar_estim(d,R,0);
%estimates by OLS the system 
%x_t = Bx_t-1 + Ax_t + u_t, and set PI = (I-A)\B and PI= (I-A)\diag(stdu) (note: by construction, the var-cov of u_t is diagonal), to get x_t = hx x_t-1 +PIe_t, where u_t = PI e_t

[~,vdx]=variance_decomposition(eye(nv),hx(:,:,k),PI(:,:,k)); %the program variance_decomposition.m is available online at http://www.columbia.edu/~mu2166/1st_order/1st_order.htm
v_share(k,:) = vdx(1,:)*100;

%Population variance
[~,sigx(:,:,k)]=mom(eye(nv),hx(:,:,k),PI(:,:,k)*PI(:,:,k)'); %the program mom.m is available online at http://www.columbia.edu/~mu2166/1st_order/1st_order.htm
PI1 = PI(:,:,k);
PI1(:,2:end) = 0;
[~,ssigx(:,:,k)]=mom(eye(nv),hx(:,:,k),PI1*PI1');
var_svar_tot(:,k) = diag(ssigx(:,:,k)); %variances conditional on ttot shocks implied by empirical SVAR model
var_svar(:,k) = diag(sigx(:,:,k)); %total variances  implied by empirical SVAR model

%Impulse Responses
x0 = PI(:,1,k)/PI(1,1,k);
x =ir(eye(nv),hx(:,:,k),x0,T); %the program ir.m is available online at http://www.columbia.edu/~mu2166/1st_order/1st_order.htm
IR(:,:,k) = x(:,1:nv);

end
save sgu_tot_run.mat U PI hx y0 v_share IR country_name R2 var_svar var_svar_tot ncou
