function R = res_sticky1(Z,param)
%RES_STICKY1    Residuals for general sticky price model with benchmark
%               functional form for utility (called by sticky1).

%               Ellen R. McGrattan, 2-16-99
%               Revised, ERM, 2-23-99

%______________________________________________________________________________
%
% PARAMETERS

N          = param(1);
psi        = param(2);
sigma      = param(3);
omega      = param(4);
eta        = param(5);
beta       = param(6);
alp1       = param(7);
alp2       = param(8);
delta      = param(9);
b          = param(10);
epsilon    = param(11);
chi        = param(12);

omeg1      = 1-omega;
delt1      = 1-delta;
sigm1      = 1-sigma;
nu         = (eta-1)/eta;
gamma      = 1/(1+psi);
gamm1      = 1-gamma;

%______________________________________________________________________________
%
% STEADY STATES

theta      = 1-1/epsilon;
Fk         = (1-beta*delt1)/(beta*theta);
ckratio    = Fk/alp1-delta;
mcratio    = (omega/omeg1*(1-beta))^(-eta);
Ls         = 1/(1+psi*ckratio*(1+omeg1/omega*mcratio^(1-1/eta))/ ...
             (theta*Fk*alp2/alp1));
Ks         = (alp1*Ls^alp2/Fk)^(1/(1-alp1));
Cs         = ckratio*Ks;
Ms         = mcratio*Cs;
Ys         = Ks^alp1*Ls^alp2;
Xs         = Ys-Cs;
ys         = log(Ys);
xs         = log(Xs);
cs         = log(Cs);
ms         = log(Ms);
ls         = log(Ls);
U          = ((omega*Cs^nu+omeg1*Ms^nu)^(gamma/nu)*(1-Ls)^gamm1)^sigm1/sigm1;
UC         = sigm1*gamma*U*omega*Cs^(nu-1)/(omega*Cs^nu+omeg1*Ms^nu);
UM         = sigm1*gamma*U*omeg1*Ms^(nu-1)/(omega*Cs^nu+omeg1*Ms^nu);
UL         = -sigm1*gamm1*U/(1-Ls);
UCM        = UC/U * UM - UC/(omega*Cs^nu+omeg1*Ms^nu) * omeg1*nu*Ms^(nu-1);
ULM        = UL/U * UM;
UMM        = UM/U * UM - UM/(omega*Cs^nu+omeg1*Ms^nu) *  ...
             omeg1*nu*Ms^(nu-1) + (nu-1) * UM/Ms;
UCC        = UC/U * UC - UC/(omega*Cs^nu+omeg1*Ms^nu) *  ...
             omega*nu*Cs^(nu-1) + (nu-1) * UC/Cs;
UCL        = UC/U * UL;
ULL        = UL/U * UL + UL/(1-Ls);
ws         = log(-UL/UC);

%______________________________________________________________________________
%
% VARIABLES APPEARING IN THE RESIDUALS 
%

z          = zeros(N+2,N);
z(:)       = Z(1:(N+2)*N);
plag       = Z((N+2)*N+N-1:-1:(N+2)*N+1);
klag       = Z((N+2)*N+N:(N+2)*N+2*N-1);

pm         = [plag;z(1,N:-1:1)'];
           % pm = [p_{t-N}-m_{t-N}, ...p_{t+N-2}-m_{t+N-2}]'  (2*N-1 x 1)

mu         = Z((N+2)*N+4*N-2:-1:(N+2)*N+2*N);
           % mu = [mu_{t-N+1}, ..., mu_{t+N-1}]'  (2*N-1 x 1) 

y          = z(N+2,N:-1:1)'; 
           % y  = [y_t, ...y_{t+N-1}]'

ki         = [klag';z(2:N+1,N:-1:1)'];
           % ki = [k_{1,t-1},   k_{2,t-1},   ... k_{N,t-1}
           %       k_{1,t}      k_{2,t},     ... k_{N,t}
           %          :            :             :
           %       k_{1,t+N-1}, k_{2,t+N-1}, ... k_{N,t+N-1}]

nn         = (ones(N,1)*[1:N-1] - triu(N*ones(N,N-1)))/N;
for i=1:N;
  m(i,1)   =-sum(pm(i:i+N-1))/N+[1:N]*mu(i:i+N-1)/N; 
  for j=1:N;
    ppi(i,j) = sum(pm(i:N+i-1))/N-pm(N-j+i)-nn(N-j+1,:)*mu(i:N+i-2);
 end; 
end;
           % m  = [m_t-pb_t,     ... m_{t+N-1}-pb_{t+N-1}]'
           % ppi= [pb_t-p_{t-1}  pb_t-p_{t-2},    ... pb_t-p_{t-N}
           %       pb_{t+1}-p_t  pb_{t+1}-p_{t-1} ... pb_{t+1}-p_{t-N+1}
           %            :                     :                 :
           %       pb_{t+N-1}    pb_{t+N-1}           pb_{t+N-1}
           %         -p_{t+N-2}    -p_{t+N-3} ...       -p_{t-1}  ]

xi         = (ki(2:N+1,:)-delt1*ki(1:N,[N,1:N-1]))/delta+log(delta);
           % x  = [x_{1,t}     x_{2,t}     ... x_{N,t}
           %       x_{1,t+1}   x_{2,t+1}   ... x_{N,t+1}
           %          :            :             :
           %       x_{1,t+N-1} x_{2,t+N-1} ... x_{N,t+N-1}]

c          = (Ys*(y-ys)-Xs*(sum(xi')'/N-xs))/Cs+cs;
           % c  = [c_t,...,c_{t+N-1}]'

yi         = y*ones(1,N) + epsilon*ppi;
           % yi = [y_{1,t}     y_{2,t}     ... y_{N,t}
           %       y_{1,t+1}   y_{2,t+1}   ... y_{N,t+1}
           %          :            :             :
           %       y_{1,t+N-1} y_{2,t+N-1} ... y_{N,t+N-1}] 

li         = (yi-alp1*ki(1:N,[N,1:N-1]))/alp2;
           % li = [l_{1,t}     l_{2,t}     ... l_{N,t}
           %       l_{1,t+1}   l_{2,t+1}   ... l_{N,t+1}
           %          :            :             :
           %       l_{1,t+N-1} l_{2,t+N-1} ... l_{N,t+N-1}] 

l          = sum(li')'/N;
           % l  = [l_t,...,l_{t+N-1}]'

w          = (UCL/UL-UCC/UC)*Cs*(c-cs)+(ULL/UL-UCL/UC)*Ls*(l-ls)+  ....
             (ULM/UL-UCM/UC)*Ms*(m-ms)+ws;
           % w  = [w_t,...w_{t+N-1}]'

mc         = w+(1-alp2)*diag(li)-alp1*diag(ki(1:N,[N,1:N-1]))-log(alp2); 
           % mc = [mc_{1,t},mc_{2,t+1},...mc_{N,t+N-1}]'

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

B0         = beta.^[0:N-1]';            % = [1,beta,...,beta^(N-1)]
B1         = cumsum(beta.^[N-1:-1:1]'); % = [beta^(N-1), ...
                                        %    beta^(N-1)+beta^(N-2), ...
                                        %    beta^(N-1)+beta^(N-2)+...+beta]
B2         = cumsum(B1);               
B2         = B2(N-1:-1:1);              % = [beta^(N-1)+
                                        %    beta^(N-1)+beta^(N-2)+...
                                        %    beta^(N-1)+beta^(N-2)+...+beta,
                                        %                  :
                                        %    beta^(N-1)+beta^(N-1)+beta^(N-2),
                                        %    beta^(N-1)]
B1         = B1(N-1:-1:1);              % = [beta+beta^2+...+beta^(N-1),
                                        %         beta^2+...+beta^(N-1), ..., 
                                        %                    beta^(N-1)]
B3         = cumsum(B0(1:N-1));         % = [1,1+beta,...,1+beta+..+beta^(N-2)]
B4         = cumsum(B3);                % = [1,
                                        %    1+(1+beta),...,
                                        %    1+(1+beta)+...+(1+...+beta^(N-2))]
betasum    = sum(B0);                   % = 1+beta+....+beta^(N-1);

varphi     = (1-1/epsilon)/(2-chi/epsilon);
R(1,1)     = (N-1)*betasum*pm(N)-B3'*pm(1:N-1)-B1'*pm(N+1:2*N-1)- ...
             N*varphi*B0'*mc+N*varphi*betasum*log(1-1/epsilon)- ...
             B2'*mu(N:2*N-2)+B4'*mu(1:N-1);          


%
% 2. MONOPOLISTS' EULER EQUATIONS FOR CAPITAL 
%

R(2:N+1,1) = b*delta*(xi(1,:)'-ki(1,[N,1:N-1])')- ...
             (UCC*Cs/UC*(c(2)-c(1))+ ...
              UCL*Ls/UC*(l(2)-l(1))+ ...
              UCM*Ms/UC*(m(2)-m(1)))- ...
             (w(2)+li(2,[2:N,1])'-ki(2,:)')*(1-beta*delt1)- ...
              beta*b*delta*(xi(2,[2:N,1])'-ki(2,:)')- ...
              b*delta*(log(delta))*(1-beta)+ ...
             (log(1-beta*delt1)-log(beta*alp1/alp2))*(1-beta*delt1);


%
% 3. MONEY DEMAND EQUATION
%

R(N+2,1)   = UM/UC*((UCM/UM-UCC/UC)*Cs*(c(1)-cs)+ ...  
                    (ULM/UM-UCL/UC)*Ls*(l(1)-ls)+ ...  
                    (UMM/UM-UCM/UC)*Ms*(m(1)-ms))+ ...
               beta*(UCC*Cs/UC*(c(2)-c(1))+ ... 
                     UCL*Ls/UC*(l(2)-l(1))+ ... 
                     UCM*Ms/UC*(m(2)-m(1))+m(2)-m(1)-mu(N+1));
