% mxn_var_ir.m
% Computes impulse responses and variances conditional on terms-of-trade
% shocks implied by the MXN model with spreads
% as a fraction of the population SVAR total variances. 
% The MXN model is  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 v_share country_name ncou var_svar var_svar_tot
%produced with sgu_tot_run_baaff.m in directory: Z:\uribe\tot\IER\mfiles

nv = size(v_share,2); 

%MXN model's prediciton of variance shares explained by tot shocks
var_model_tot = zeros(nv,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 mxn_ss to read parameters fed externally
externallyset_parameters = 1;

Tir = 11;  

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

%Compute impulse responses: 
%x0 = zeros(nstate,1); 
%x0(ntot,1) = 1; 
x0 = nETASHOCK*[1;0]/nETASHOCK(ntot,1);

[~,ir_control,ir_state] = ir(gx,hx,x0,Tir);  %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

IR_mxn(1:Tir,1:nv,k) = [ir_state(:,ntot) ir_state(:,ns) ir_control(:,[ntby noutput_constant_prices nc_constant_prices nivv_constant_prices nptau])];

%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

diag(sigy0);  %unconditional variances conditional on tot shocks
var_model_tot(3:end,k) = ans([ntby;  noutput_constant_prices ;nc_constant_prices ;nivv_constant_prices;nptau]'); %pick variables of interest (output, consumption, investment, trade balance divided by trend output)

%For table 6: MXN Model Units of Final Goods, replace
%noutput_constant_prices with noutput
%var_model_tot(3:end,k) = ans([ntby;  noutput ;nc_constant_prices ;nivv_constant_prices;nptau]'); %pick variables of interest (output, consumption, investment, trade balance divided by trend output)

var_model_tot(1,k) = sigx0(ntot,ntot);
var_model_tot(2,k) = sigx0(ns,ns);

end

var_model_tot_ratio_cbc = var_model_tot./ var_svar;

var_data_tot_ratio_cbc = var_svar_tot./var_svar;

var_model_tot_ratio = median(var_model_tot_ratio_cbc,2);

var_data_tot_ratio = median(var_data_tot_ratio_cbc,2);

round(median(var_model_tot_ratio_cbc')*100)
round(median(var_data_tot_ratio_cbc')*100)


readme = 'The order in IR_mxn is tot baaff  tby y c i rer'
save mxn_var_ir.mat  IR_mxn   Tir var_model_tot_ratio_cbc var_data_tot_ratio_cbc country_name readme n*