function [Xm,pai,year, XI_tT] = plot_xm
%function [Xm,pai,year, XI_tT] = plot_xm plots the inferred path of the nonstationary monetary shock Xm.
%Replication materials for: ``What Do Long Data Tell Us About the Permanent Component of Inflation?''
%© Stephanie Schmitt-Grohé and Martín Uribe,  2023. 

year = [];
create_structure

load bayes.mat Y mean_param X  %produced by running run_bayes.m in the directory 1900_2022 (takes 19 hours to run)

tryparam = mean_param;

evaluate_structure

if nlags==2 & max(max(abs(B(:,ny+1:end))))==0
%Transform the system as follows
%The original system is
%xi_t+1 = F xi_t + PSI v_t+1
%o_t = H' xi_t + measurement error
%where xi_t = [y_t;y_t-1;u_t];
%Let eta_t = [y_t-1;u_t];
%Then
%xi_t = G eta_t
%eta_t = K xi_t
%eta_t+1 = nF eta_t + nPSI v_t+1
%o_t = nH' eta_t + measurement error

G = [B(1:ny,1:ny) C
        eye(ny) zeros(ny,nshocks)
        zeros(nshocks,ny) eye(nshocks)];

aux = size(F,1);
K = zeros(aux-ny,aux);
K(:,ny+1:end) = eye(aux-ny);
F = K*F*G;
Hp = Hp*G;
PSI = K*PSI;
Q = PSI*PSI';
end %if nlags==2 & max(max(abs(B(:,ny+1:end))))==0

H = Hp';
A = Ap';
[XI_tT,P_tT,XI_tt,P_tt,XI_ttm1,P_ttm1] = smoothing_kalman(Y,F,Q,H,R,X,A);

xm = XI_tT(:,4); 
Xm = cumsum(xm);
Xm = Xm- mean(Xm) + mean(pai);

fsize = 12;
dicke = 2; 
set(0,'defaulttextinterpreter','latex')
set(0, 'DefaultAxesFontSize', fsize)
set(0, 'DefaultTextFontSize', fsize) 
plot(year,pai,'r:','linewidth',dicke)
hold on
plot(year,Xm,'b','linewidth',dicke)
ylabel('percent per year')
xlim([1900 2025])
ylim([-12 17])
hold off
h=legend('Inflation','$X^m_t$');
set(h,'interpreter','latex')

shg
disp('pai(year==2021)-pai(year==2019)')
disp(pai(year==2021)-pai(year==2019))


disp('Xm(year==2021)-Xm(year==2019)')
disp(Xm(year==2021)-Xm(year==2019))

disp('pai(year==2022)-pai(year==2019)')
disp(pai(year==2022)-pai(year==2019))

disp('Xm(year==2022)-Xm(year==2019)')
disp(Xm(year==2022)-Xm(year==2019))

