%evaluate_structure.m 
%assigns values to the matrices of the empirical model 
%Note PSI here is denote P in the paper and P here is denoted \psi in the paper.
%Replication materials for: ``What Do Long Data Tell Us About the Permanent Component of Inflation?''
%© Stephanie Schmitt-Grohé and Martín Uribe, December 27, 2023. 
%© Martín Uribe, May 2017. 

RHO(aux_RHO) = tryparam(iRHO);
P(aux_P) = tryparam(iP);
B(aux_B) = tryparam(iB);
C(aux_C) = tryparam(iC);
Ap(aux_Ap) = tryparam(iAp);
R(aux_R) = tryparam(iR);
Hp(aux_Hp) = tryparam(iHp);

aux = ny*(nlags-1);
 F = [B C*RHO;
eye(aux) zeros(aux,ny+nshocks);
zeros(nshocks,ny*nlags) RHO];
PSI = [C*P;
zeros(aux,nshocks);
P];
Q = PSI*PSI';

F1 = F;
Hp1 = Hp;
Q1 = Q;

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);
F1 = K*F*G;
Hp1 = Hp*G;
PSI1 = K*PSI;
Q1 = PSI1*PSI1';
end

%check that the eigenvalues of the system are all inside the unit circle 
unit_circle = max(abs(eig(F1)))<1;



