function [Hx,Gx, nvarshock, nvarme, nETASHOCK,tby,ALFA, exitflag] = usg_noise_gx_hx_inputs(param_estim)
%
%[Hx,Gx, nvarshock, nvarme, nETASHOCK,tby,ALFA, exitflag] = usg_noise_gx_hx_inputs(param_estim)  evaluates at the deterministic steady state the equlibrium conditioins
% nvarshock \equiv nETASHOCK*nETASHOCK' of innovations associated with a DSGE model. The variance matrix of measurement errors varme is also evaluated. 
%
%input: param_estim is a vector containing parameter values
%
%This script belongs to a suite of programs that performs a Bayesian estimation of the open economy model with stationary, nonstationary, and noise shocks and imperfect information presented in the book Open Economy Macroeconomics, by Martín Uribe and Stephanie Schmitt-Grohé (Princeton University Press) in  the chapter `Business Cycles in Emerging Countries: Productivity Shocks Versus Financial Frictions.'' 
%© M. Uribe and S. Schmitt-Grohé, January 2014. 

ALFA = 0.32; %Capital elasticity of the production function

if nargin==1
SIGMAX = param_estim(1);
RHOX = param_estim(2);
SIGMAZ = param_estim(3);
RHOZ = param_estim(4);
SIGMAN = param_estim(5);
RHON = param_estim(6);
PHI = param_estim(7);
STDmey = param_estim(8);
STDmec = param_estim(9);
STDmeiv = param_estim(10);
STDmetby = param_estim(11);

elseif nargin==0

SIGMAX = 0.0106;
RHOX = 0.62;
SIGMAZ = 0.0135/(1-ALFA);
RHOZ = 0.59;
SIGMAN = 0.2003;
RHON = 0;
PHI = 1.27;
STDmey = 0;
STDmec = 0;
STDmeiv = 0;
STDmetby = 0;
end


nshocks = 3; %numer of exogenous disturbances

PSSI = 0.001; %debt elastic interest rate

SIGG = 2; %intertemporal elasticity of substitution

GAMA = 0.36; %exponent of consumption in Cobb-Douglas period utility

G =     0.0018; %average quarterly growth rate of real GDP per capita in mexico from 1980:2 to 2012:4.  

DELTA = 0.05; %Depreciation rate

BETTA = 0.98; %subjective discount factor

RSTAR= 1/BETTA* exp(-G*(GAMA*(1-SIGG)-1)) -1; %parameter of the interest rate function

DBAR = 0.1;%parameter of interest-rate function, equal to the steady state level of net foreign debt.

r = RSTAR;  %Country interest rate

k_over_h = ((RSTAR+DELTA)/(ALFA*exp((1-ALFA)*G)))^(1/(ALFA-1)); %K/(G*H)

AUX1 = exp(G) * DBAR/(1+r) - DBAR;

AUX2 = -( k_over_h * (exp(G)-1+DELTA) - exp((1-ALFA)*G) * k_over_h^(ALFA));

AUX3 = GAMA/(1-GAMA)*(1-ALFA) * exp((1-ALFA)*G) * k_over_h^ALFA;

h = (AUX3-AUX1)/(AUX3+AUX2); %hours

d = DBAR; %detrended external debt

k = k_over_h * h; %capital

ivv = (exp(G)-1+DELTA) * k; %investment

yy = exp((1-ALFA)*G) * k^ALFA * (h)^(1-ALFA); %output

c = (1-h) * AUX3; %Consumption

la = GAMA * c^(GAMA*(1-SIGG)-1) * (1-h)^((1-GAMA)*(1-SIGG)); %marginal utility of wealth 

tb = yy-c-ivv; %trade balance

tby = tb / yy; %trade-balance-to-output ratio

k1 = k; %Auxiliary variable

z = 0; %productivity shock 
zback = z;

%states and controls generated by the Kalman filter
eta1 = 0;
eta2 = 0;
eta3 = 0;
eta4 = 0;
eta5 = 0;
eta6 = 0;

ghat = 0; %Deviation steady state ofgrowth rate of TFP

gxhat = 0; %Deviation from steady state ofgrowth rate of nonstationary productivity shock 

gc = exp(G);
giv = exp(G);
yyback = yy;
cback = c;
ivvback = ivv;
ghatback = ghat;
gy = exp(G);

noise = 0; %noise
s = 0; %signal

F = [
RHOZ 0 0 0
 1 0 0 0
0 0 RHOX 0
0 0 0 RHON];
rF = size(F,1); %rows of F

Hp = [
1 -1 1 0
0 0 1 1];
rHp = size(Hp,1); %rows of Hp

H = Hp';

STDMATRIX = [
SIGMAZ  0 0
0  0 0
0  SIGMAX 0
0 0 SIGMAN];

Q = STDMATRIX*STDMATRIX';

P0 = Q;

dist = 1;
while dist>1e-8
P = F * (P0-P0*H*inv(Hp*P0*H)*Hp*P0)*F'+Q;
dist = max(abs(P(:)-P0(:)));
P0 = P;
end

K = F*P*H*inv(Hp*P*H);
K1 = P*H*inv(Hp*P*H);

k11 = K(1,1);
k12 = K(1,2);

k21 = K(2,1);
k22 = K(2,2);

k31 = K(3,1);
k32 = K(3,2);

k41 = K(4,1);
k42 = K(4,2);

usg_noise_model_num_eval; %this is a .M file produced by running usg_noise_model.m 

%First-stage policy funcitons
[gx,hx,exitflag]=gx_hx(nfy,nfx,nfyp,nfxp); 

%Construct the matrix Hx, such that 
%X_{t+1} = Hx * X_t + ETASHOCK * epsilon_{t+1}, where
%X_t [ yyback, cback, ivvback, ghatback, k, d, eta1, eta2, eta3, eta4, z_t, z_{t-1}, gxhat_, n_t]', is the vector of states. 

%Remove o_t from the last two elements of X_t and  add xi_t
h1x = hx(1:end-rHp,:); %make sure that ghat and s appear  last and in this order in statevar_cu  (same for the p versions)

h11x = h1x(:,1:end-rHp);
h12x = h1x(:,end-rHp+1:end);
I21 = zeros(rF,size(h11x,2));
Hx = [
h11x h12x*Hp 
I21 F];
rHx = size(Hx,1); %rows of Hx

%Consruct Gx such that y_t = Gx*X_t, where y_t = [ gy, gc, giv, tby, h, yy, c, ivv, k1, E_tz_t, E_tz_{t-1}, E_t gxhat_t, E_tn_t ghat_t s_t]' is the vector of controls
g1x = gx(:,1:end-rHp);
g2x = gx(:,end-rHp+1:end);
Gx = [g1x g2x*Hp];

%At this point, Gx contains rows for all controls except E_t xi_t and o_t, where xi_t = [z_t z_{t-1} gxhat_t n_t]'  is the vector of unobservable exogenous states, and o_t = [ghat_t s_t]' is the vector of observables (the growth rate of TFP and the signal). Next, we add rows to Gx to include these variables in the policy function. 

%o_t = aux1 * X_t (where o_t is the vector of observables, in this cas [ghat_t s_t]'.
aux1 = [zeros(rHp,rHx-rF) Hp];

%E_t xi_t = aux4 * X_t, where xi_t is the vector of unobserved exogenous states, in this case [z_t z_{t-1} ghatx_t n_t]'. 
aux2 = eye(rF)-K1*Hp;
aux3 = [zeros(rF,rHx-2*rF) eye(rF) zeros(rF)];
aux4 = aux2*aux3+K1*aux1;

Gx = [
Gx
aux4
aux1];

nETASHOCK = zeros(size(Hx,1),nshocks);

nETASHOCK(size(h11x,1)+1:end,:) =  STDMATRIX; 

nvarshock = nETASHOCK*nETASHOCK';
