function [A,B,K,Q,U,R] = sosdebt(al,eta,phi,ke,lam,bet,rho,gr,tb,bb,vard,vars,vare,varg,varb,qy,qpi,qf);
%Soeme Generate A,B,K,Q,U, and R from parameter values of July09 soeme debt model.
%
%
%  Purpose:   Generate A,B,K,Q,U, and R from parameter values of 
%             soeme debt model.
%
%  Usage:     [ A,B,K,Q,U,R ] = ...
%              = sosdebt(al,eta,phi,ke,lam,bet,rho,gr,tb,bb,vara,vard,varc,vars,varg,varb,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 lecture notes for further comments.
%
% Modified by Ashima Goyal, 13 July 2009. 
%
%  Paul Soderlind, Paul.Soderlind@hhs.se, 20 August 1997, to MatLab Aug 2000
%----------------------------------------------------------------------------


                           %model->general setup

 
Aa = [ 0.75 zeros(1,11)]; %for natural rate shock

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

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

Ad = [ zeros(1,10) 1 0];

Ae = [ zeros(1,9) 1 0 0]+ al*(Ad - [0 0 0 1 zeros(1,8)])- (al* [ zeros(1,9) 1 0 0]); %correct feb08 and march08 simus with GM error removed

Af = [ zeros(1,12)];

Ag = 0.85*[zeros(1,6) 1 zeros(1,5)];

Ah = [ zeros(1,11) 1];

Ai = [(1 + ((lam/(0.8*bet))*(1+(phi*(eta*(1 - al) + al)))))*Ab ]+((eta*(1 - al) + al)*(0.2*[ zeros(1,5) 1 zeros(1,6) ]-(Ac/(0.8*bet)) + (0.25/bet)* [0 0 1 zeros(1,9)] -0.01 + 0.01*Aa)); %Benchmark, *Aa for last term delivers natural rate shock

Aj = (1/(bet*0.8))*Ac - [(lam/(0.8*bet))*((1/(eta*(1-al)+al))+phi)*Ab ]-(0.25/bet)* [0 0 1 zeros(1,9)]; 
     
Ak =  Ad; %E fix if no 1 in B

Al =  1.01*(Ah*(1-tb)+Ag)-0.01*bb; %current dom prs -bb*Aj-bb*gr 


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

B = [ zeros(5,1); 1; 0; 0; 0.8*(eta*(1 - al) + al); 0; 1; bb]; %bb for rt-1 term in debt eqn

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

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