function R = res_iscmtr1_ms(Z,param)
%RES_ISCMTR1_MS   Residuals for the international sticky price model with
%                 Taylor rule for interest rates and N=1.

%                 Ellen R. McGrattan, 9-15-99
%                 Revised, ERM, 7-24-01

%______________________________________________________________________________
%
% PARAMETERS


ns         = 50;
beta       = param(11);
ome1       = param(12);
rho        = param(13);
theta      = param(14);
alpha      = param(15);
delta      = param(16);
b          = param(17);
mu         = param(19);
irule      = param(23:32);
gs         = param(36);
phs        = param(ns);
ys         = param(ns+1);
cs         = param(ns+2);
MPs        = param(ns+3);
UC         = param(ns+4);
UM         = param(ns+5);
UL         = param(ns+6);
UCC        = param(ns+7);
UMM        = param(ns+8);
ULL        = param(ns+9);
UCM        = param(ns+10);
UCL        = param(ns+11);
ULM        = param(ns+12);
ks         = param(ns+13);
xs         = param(ns+14);
yhs        = param(ns+15);
yhas       = param(ns+16);
ls         = param(ns+17);
ws         = -UL/UC;
ps         = mu/MPs;
rs         = mu/beta-1;

alph1      = 1-alpha;
delt1      = 1-delta;
ome2       = 1-ome1;
rho1       = 1-rho;
thet1      = 1-theta;

%______________________________________________________________________________
%
% VARIABLES APPEARING IN THE RESIDUALS
%

nz         = 10;
z          = zeros(10,2);
z(:)       = Z(1:20);
z          = z(:,[2,1]);

phlag      = Z([23,22,21]) - log(phs);
pflag      = Z([26,25,24]) - log(phs);
pfalag     = Z([29,28,27]) - log(phs);
phalag     = Z([32,31,30]) - log(phs);
           % phlag = [ph_{t-4},ph_{t-3},ph_{t-2}]'

k          = [Z(33);z(5,:)'] - log(ks);
ka         = [Z(34);z(6,:)'] - log(ks);
           % k  = [k_{t-1}, k_t, k_{t+1}]'

ylag       = Z([36,35]) - log(ys);
yalag      = Z([38,37]) - log(ys);
           % ylag  = [y_{t-2},y_{t-1}]'

rlag       = Z([41,40,39]) - rs;
ralag      = Z([44,43,42]) - rs;
           % rlag  = [r_{t-3},r_{t-2},r_{t-1}]'

eps        = Z(45);
epsa       = Z(46);
           % eps  = eps_t

g          = Z([48,47]) - log(gs);
ga         = Z([50,49]) - log(gs);
           % g   = [g_t,g_{t+1}]'

a          = Z([52,51]);
aa         = Z([54,53]);
           % a   = [a_t,a_{t+1}]'

ph         = z(1,:)' - log(phs);
pf         = z(2,:)' - log(phs);
pfa        = z(3,:)' - log(phs);
pha        = z(4,:)' - log(phs);
           % ph  = [ph_{t-1},ph_t]'

y          = z(7,:)' - log(ys);
ya         = z(8,:)' - log(ys);
           % y   = [y_t,y_{t+1}]'

r          = z(9,:)' - rs;
ra         = z(10,:)' - rs;
           % r   = [r_t,r_{t+1}]'

x          = (k(2:3)-delt1*k(1:2))/delta;
xa         = (ka(2:3)-delt1*ka(1:2))/delta;
           % x  = [x_t,x_{t+1}]'

c          = (ys*y-xs*x-gs*g)/cs;
ca         = (ys*ya-xs*xa-gs*ga)/cs;
           % c  = [c_t,c_{t+1}]'

p          = (ps/phs)^(rho/rho1)*(ome1^(1/rho1)*ph+ome2^(1/rho1)*pf);
pa         = (ps/phs)^(rho/rho1)*(ome1^(1/rho1)*pfa+ome2^(1/rho1)*pha);
           % p  = [p_t,p_{t+1}]'

yh         = y+(p-ph)/rho1;
yf         = y+(p-pf)/rho1;
yfa        = ya+(pa-pfa)/rho1;
yha        = ya+(pa-pha)/rho1;
           % yh = [yh_t,yh_{t+1}]'

l          = (yh *yhs + yha *yhas)./(alph1*(yhs+yhas))-alpha*k(1:2)/alph1-a;
la         = (yfa*yhs + yf  *yhas)./(alph1*(yhs+yhas))-alpha*ka(1:2)/alph1-aa;
           % l  = [l_t,l_{t+1}]'

m          = (-(UCM/UM-UCC/UC)*cs*c -(ULM/UM-UCL/UC)*ls*l +beta/mu/rs*r)/ ...
             ((UMM/UM-UCM/UC)*MPs);
ma         = (-(UCM/UM-UCC/UC)*cs*ca-(ULM/UM-UCL/UC)*ls*la+beta/mu/rs*ra)/ ...
             ((UMM/UM-UCM/UC)*MPs);
           % m  = [m_t-p_t,m_{t+1}-p_{t+1}]'

w          = (UCL/UL-UCC/UC)*cs*c+(ULL/UL-UCL/UC)*ls*l+  ...
             (ULM/UL-UCM/UC)*MPs*m;
wa         = (UCL/UL-UCC/UC)*cs*ca+(ULL/UL-UCL/UC)*ls*la+ ...
             (ULM/UL-UCM/UC)*MPs*ma;
           % w  = [w_t,w_{t+1}]'

mc         = w+alpha*(l-k(1:2))-alph1*a;
mca        = wa+alpha*(la-ka(1:2))-alph1*aa;
           % mc = [mc_t,mc_{t+1}]'

q          = -UCC*cs/UC*(c-ca)-UCL*ls/UC*(l-la)-UCM*MPs/UC*(m-ma);
           % q  = [q_t,q_{t+1}]'

%______________________________________________________________________________
%
% LINEARIZED RESIDUALS:
%
% 1. PRICING EQUATIONS
%

R(1,1)       = (ph(1) -(p(1) +mc(1)))      /phs;
R(2,1)       = (pf(1) -(p(1) +mca(1)+q(1)))/phs;
R(3,1)       = (pfa(1)-(pa(1)+mca(1)))     /phs;
R(4,1)       = (pha(1)-(pa(1)+mc(1) -q(1)))/phs;

%
% 2. MONOPOLISTS' EULER EQUATIONS FOR CAPITAL
%

R(5,1)       =-(UCC*cs /UC*(c(2)-c(1))+ ...
                UCL*ls /UC*(l(2)-l(1))+ ...
                UCM*MPs/UC*(m(2)-m(1)))- ...
                 (1-beta*delt1) * (w(2)+l(2)-k(2))+ ...
                 b*delta * (x(1)-k(1))- ...
                 beta*b*delta * (x(2)-k(2)) ;

R(6,1)       =-(UCC*cs /UC*(ca(2)-ca(1))+ ...
                UCL*ls /UC*(la(2)-la(1))+ ...
                UCM*MPs/UC*(ma(2)-ma(1)))- ...
                 (1-beta*delt1) * (wa(2)+la(2)-ka(2))+ ...
                 b*delta * (xa(1)-ka(1))- ...
                 beta*b*delta * (xa(2)-ka(2)) ;


%
% 3. MONEY DEMAND EQUATIONS
%

R(7,1)       = UM/UC*((UCM/UM-UCC/UC)*cs *c(1)+ ...
                      (ULM/UM-UCL/UC)*ls *l(1)+ ...
                      (UMM/UM-UCM/UC)*MPs*m(1))+ ...
              beta/mu*(UCC*cs /UC*(c(2)-c(1))+ ...
                       UCL*ls /UC*(l(2)-l(1))+ ...
                       UCM*MPs/UC*(m(2)-m(1))+p(1)-p(2));

R(8,1)       = UM/UC*((UCM/UM-UCC/UC)*cs *ca(1)+ ...
                      (ULM/UM-UCL/UC)*ls *la(1)+ ...
                      (UMM/UM-UCM/UC)*MPs*ma(1))+ ...
              beta/mu*(UCC*cs /UC*(ca(2)-ca(1))+ ...
                       UCL*ls /UC*(la(2)-la(1))+ ...
                       UCM*MPs/UC*(ma(2)-ma(1))+pa(1)-pa(2));

%
% 4. INTEREST RATE EQUATIONS
%
plag       = (ps/phs)^(rho/rho1)*(ome1^(1/rho1)*phlag+ome2^(1/rho1)*pflag);
palag      = (ps/phs)^(rho/rho1)*(ome1^(1/rho1)*pfalag+ome2^(1/rho1)*phalag);
           % plag  = [p_{t-3},p_{t-2},p_{t-1}]'
    
yflag      = ylag+(plag(2:3)-pflag(2:3))/rho1;
yhalag     = yalag+(palag(2:3)-phalag(2:3))/rho1;
           % yflag = [yf_{t-2},yf_{t-1}]'

gdp        = y    + (yha-yf)*yhas/ys;
gdpa       = ya   + (yf-yha)*yhas/ys;
gdplag     = ylag + (yhalag-yflag)*yhas/ys;
gdpalag    = yalag+ (yflag-yhalag)*yhas/ys;
           % gdp     = [gdp_t,gdp_{t+1}]'
           % gdplag  = [gdp_{t-2},gdp_{t-1}]'

R(9,1)     = r(1)-irule' *[rlag(3);rlag(2);rlag(1);p(2)-p(1);
                           p(1)-plag(3);plag(3)-plag(2);plag(2)-plag(1);
                           gdp(1);gdplag(2);gdplag(1)]-eps;
R(10,1)    = ra(1)-irule'*[ralag(3);ralag(2);ralag(1);pa(2)-pa(1);
                           pa(1)-palag(3);palag(3)-palag(2);palag(2)-palag(1);
                           gdpa(1);gdpalag(2);gdpalag(1)]-epsa;
