function [A,B,K,Q,U,R] = sosfindemo(al,eta,phi,ke,lam,bet,rho,vard,vars,vare,qy,qpi,qf);
% Generate A,B,K,Q,U, and R from parameter values of march08 soeme paper models, which modify 
% Gali and Monacelli (2007) to apply to emerging markets with heterogenous agents.
%
%
%  Purpose:   Generate A,B,K,Q,U, and R from parameter values of simplified
%             soeme model.
%
%  Usage:     [ A,B,K,Q,U,R ] = ...
%              = sosfindemo(al,eta,phi,ke,lam,bet,rho,Vara,Vard,Varc,Vars,qy,qpi,qf);
%
%  The period loss function is
%
%  qy*y^2 +  qpi*pi^2 + qf*i^2, which is rewritten here as
%
%  x'Qx + 2x'Uu + u'Ru.
%
%  x = [x1,x2],with 
%  x1(t) = [a(t),cp(t),s(t),q(t),pi(t),e(t),ybar(t),sbar(t),rbar(t),r(t),y(t)] 
%  and x2(t) = [x(t+1),pih(t+1),s(t+1)]
%
%  See Paul Soderlind's lecture notes for further comments.
%  Modified from the program originally written by Paul Soderlind, Paul.Soderlind@hhs.se,  Aug 2000
%
%
% Ashima Goyal (ashima@igidr.ac.in), 13 October 2008.
%----------------------------------------------------------------------------


                           %model->general setup

Aa = [ 0.75 zeros(1,8)];

Ab = [ zeros(1,6) 1 0 0 ];

Ac = [ zeros(1,7) 1 0 ];

Ad = [ zeros(1,8) 1 ];

Ae = [ zeros(1,7) 1 0]+ al*(Ad - [0 0 0 1 zeros(1,5)])- (al* [ zeros(1,7) 1 0]); 

Af = [ zeros(1,9)];

Ag = [(1 + ((lam/(0.8*bet))*(1+(phi*(eta*(1 - al) + al)))))*Ab ]+((eta*(1 - al) + al)*(0.2*[ zeros(1,5) 1 0 0 0 ]-(Ac/(0.8*bet)) + (0.25/bet)* [0 0 1 zeros(1,6)]- 0.01*Aa)); %Benchmark 0.2 r t-1 term, 0.8 rt from B matrix; remove Aa term when no rn shocks;  implies rn changing over simus  

Ah = (1/(bet*0.8))*Ac - [(lam/(0.8*bet))*((1/(eta*(1-al)+al))+phi)*Ab ]-(0.25/bet)* [0 0 1 zeros(1,6)]; %benchmark Ah

%Ah = (1/(bet*0.8))*Ac - [((lam/(0.8*bet))*((sigd+phi)/(1+phi*6)))*Ab]-(0.25/bet)* [0 0 1 zeros(1,6)]; %specific labour market Ah

%Ah = (1/(bet*0.2))*Ac - [(lam/(0.2*bet))*((1/(eta*(1-al)+al))+phi)*Ab]-(0.4/bet)* [0 0 1 zeros(1,6)]; %Backward looking
      
%Ai = Ah + Ad; %S fix remove 1 in B also

Ai =  Ad; %E fix if no 1 in B

A = [ Aa;
      Ab;
      Ac;                    %x(t+1)=A*x(t)+B*u(t)+ e(t+)
      Ad;
      Ae;
      Af;
      Ag;      
      Ah;
      Ai ];
      
%only rt-1 in eqns B = [ zeros(5,1); 1; 0; 0; 0];

B = [ zeros(5,1); 1; 0.8*(eta*(1 - al) + al); 0; 1]; % B enters the pcy rate (rt+1) additively; eta terms for correct addition to rt-1 term in xht+1 

K  = [ [zeros(1,6) 1 0 0 0];           %[y,pi,f] = K*[x,u]
        %[0 0 1 zeros(1,7)  ];            %lag dit
      [zeros(1,7) 1 0 0 ];            %fl dit
       %[zeros(1,4) 1 0 0 0 0 0 ];             %bl cpi tgt to be used with lag CPI
       %[zeros(1,3) -al 0 0 0 (1-al) al 0];       %fl cpi same result if current cpi
       [zeros(1,9) 1]  ];

Wbig = K'* diag([qy;qpi;qf]) * K;
Q = Wbig(1:9,1:9);
U = Wbig(1:9,10);
R = Wbig(10,10);
%-----------------------------------------------------------------------
