%var_shares_data_mxns.m
% Computes  output variance shares explained  by terms-of-trade shocks predicted   by a 7-variable  SVAR and  the MXN model with spreads. The denominator of the shares is common in the SVAR and MXN models and is given by the population SVAR total variances. 
% The SVAR and MXN models are  developed in  the paper  ``How Important Are Terms of Trade Shocks?,'' by Stephanie Schmitt-Grohé and Martín Uribe, submitted to the International Economic Review. 
% © Martín Uribe and Stephanie Schmitt-Grohé, May 2015.

clear all

%read empirical  share of variances explained by tot shocks in SVAR
load sgu_tot_run_baaff.mat country_name ncou var_svar var_svar_tot
%produced with sgu_tot_run_baaff.m in directory: Z:\uribe\tot\IER\mfiles

%MXN model's prediciton of variance shares explained by tot shocks
var_model_tot = zeros(2,ncou);
load phi_pssi_cbc.mat PHIM_cbc PHIX_cbc PHINT_cbc PSSI_cbc RHO_TOTTOT_cbc RHO_TOTS_cbc RHO_STOT_cbc RHO_SS_cbc  STD_TOTTOT_cbc  STD_TOTS_cbc STD_STOT_cbc STD_SS_cbc ncou
%produced by running 
%phi_pssi_cbc.m in
%z:\uribe\tot\IER\mfiles\mxn_with_spread

%allow mxns_ss to read parameters fed externally
externallyset_parameters = 1;

for k=1:ncou
PHIM = PHIM_cbc(k);
PHIX = PHIX_cbc(k);
PHINT = PHINT_cbc(k); 
PSSI = PSSI_cbc(k);
RHO_TOTTOT = RHO_TOTTOT_cbc(k); 
RHO_TOTS = RHO_TOTS_cbc(k); 
RHO_STOT = RHO_STOT_cbc(k); 
RHO_SS = RHO_SS_cbc(k); 

STD_TOTTOT = STD_TOTTOT_cbc(k); 
STD_TOTS = STD_TOTS_cbc(k); 
STD_STOT =STD_STOT_cbc(k); 
STD_SS = STD_SS_cbc(k); 

mxns_ss
mxns_num_eval %this .m script was created by running mxns.m 
%in z:\uribe\tot\ier\mfiles\mxn_with_spread

%The linearized equilibrium system is of the form
%y_t=gx x_t
%x_t+1 = hx x_t + ETASHOCK epsilon_t+1
[gx, hx, exitflag] = gx_hx(nfy, nfx, nfyp, nfxp); %this step uses the toolbox for first-order approximations of  DSGE models produced by S. Schmitt-Grohe and M. Uribe 
%available on line at  
%http://www.columbia.edu/~mu2166/1st_order/1st_order.htm

%variance-covariance matrices conditional on tot shocks only 
%Block out all shocks other than the tot shock: 
nETASHOCK(:,2:end) = 0; 
varshock = nETASHOCK*nETASHOCK';

[sigy0,sigx0]=mom(gx,hx,varshock); %this step uses the toolbox for first-order approximations of  DSGE models produced by S. Schmitt-Grohe and M. Uribe 
%available on line at  
%http://www.columbia.edu/~mu2166/1st_order/1st_order.htm

var_model_tot(1:ncontrol,k) = diag(sigy0);  %unconditional variances conditional on tot shocks

end

%share of output measured  at constant prices and in current units of final goods explained by tot shocks implied by the MXN model
share_model = var_model_tot([noutput_constant_prices noutput],:)./repmat(var_svar(4,:),2,1);
median_model  = median( share_model,2)';
mad_model  = mad( share_model,1,2)';

%share of variance of oput  explained by tot shocks implied by the 7-variable SVAR model
share_data = var_svar_tot(4,:)./ var_svar(4,:);
median_data  = median( share_data);
mad_data  = mad( share_data,1);


table = [[median_data median_model]; [mad_data mad_model]]*100
