%plot_mxn_ir
%Produce impulse responses to a 10-percent terms of trade shock implied by the  MXN model developed in the paper ``How Important Are Terms Of Trade Shocks? by  S. Schmitt-Grohe and M. Uribe (IER, 2017).

clear all
clf
orient tall 


clear all



%MXN model's prediciton of variance shares explained by tot shocks
load phi_pssi_cbc.mat PHIM_cbc PHIX_cbc PHINT_cbc PSSI_cbc RHO_TOTTOT_cbc RHO_STOT_cbc RHO_TOTS_cbc  RHO_SS_cbc STD_TOTTOT_cbc STD_STOT_cbc STD_TOTS_cbc  STD_SS_cbc 
%produced by running 
%phi_pssi_cbc.m in
%z:\uribe\tot\IER\mfiles\match_ir

load mxn_var_ir.mat n*
%produced by running 
%mxn_var_ir.m in
%z:\uribe\tot\IER\mfiles\match_ir



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

Tir = 11;  


pick_controls = [nptau; 
                         npn;
                         ntby;
                         nm; 
                         nx;
                         noutput_constant_prices;
                         nc_constant_prices;
                         nivv_constant_prices;
                         nym;
                         nyx;
                         nyn;
                         nim;
                         nix;
                         nivn];
pick_states = ntot; 
nv = length(pick_controls)+length(pick_states); 
IR = zeros(Tir, nv, ncou); 

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(:,:,k) = [ir_state(:,pick_states) ir_control(:, pick_controls)];

end %for k=1:ncou

%Compute cross-country medians of impluse responses
IR = median(IR,3); 
IR = 10*IR; 

rows =5;
cols = 3;

thick = 2;
thin = 1; 

t=0:Tir-1;


%note y, c, ivv, and tby are at constant prices, 

TITLE = {'Terms of Trade',
    'Real Exchange Rate', 
    'Rel Price of Nontradables',
    'Trade Balance', 
    'Imports', 
    'Exports', 
    'Output', 
    'Consumption',
    'Investment',
    'Output in Import Sector', 
    'Output in Export Sector', 
    'Output in Nontraded Sector',
    'Investment in Import Sector',
    'Investment in Export Sector',
    'Investment in Nontraded Sector'};
    

for i = 1:15
        subplot(rows,cols,i)
        plot(t, IR(:,i),'linewidth',thick)
        title(TITLE{i})
        hold on
end

shg