// Dynare program for open economy model with monopolistic competition and balanced budget.
// This version includes FB risk premium.

var a c cons_tax dfb div divf fb g i infl int_tax inv k lab lab_tax lam lamf ls_tax n ni nx psi q qf r rh rk rx w xi y gs gsts inflh padjc st rmc ups ups_d cero tau_c divai cero1; // to params:

// a = technology parameter
// c = real aggregate domestic consumption
// cons_tax = consumption tax -> endogenised version of (1 + tau_c)
// dfb = change in real foreign bond holding
// div = real dividend of production firms
// divf = real dividend of foreign investment firms
// fb = real foreign bond holdings
// fw = total real domestic household financial wealth
// g = real government expenditure
// h = total real domestic household wealth
// hw = total real domestic household human wealth
// i = domestic nominal interest rate
// infl = domestic CPI inflation
// int_tax = household interest income tax -> endogenised version of (1 - tau_r)
// inv = real domestic investment
// k = real domestic capital stock
// lab = domestic labour supply
// lab_tax = labour income tax -> endogenised version of (1 - tau_l)
// lam = lagrange multiplier from investment decision of actuarial firms
// lamf = lagrange multiplier from foreign bond decision of foreign investment firms
// ls_tax = lump-sum tax -> endogenised version of tau_ls
// m = real money balances
// n = real actuarial note holdings
// ni = household net interest
// nx = real net exports
// psi = real capital adjustment costs
// q = real share price of production firms
// qf = real share price of foreign investment firms
// r = domestic real interest rate
// rh = real interest rate for domestic households
// rk = real rental rate of capital
// rx = real interest rate (as perceived by domestic residents) on foreign bonds
// s = marginal propensity to save out of wealth

// w = domestic real wage rate
// x = auxilliary variable in marginal propensity to consume out of wealth
// xi = real foreign bond adjustment costs
// y = domestic real output
// z = auxilliary variable in marginal propensity to consume out of wealth
// **** gs = CPI/PPI ratio
// **** gsts = foreign CPI/PPI ratio
// **** inflh = domestic PPI inflation
// **** padjc = production firm price adjustment costs
// **** rmc = real marginal cost
// **** st = terms of trade = P_f/P_h
// **** ydd = domestic demand of composite good
// **** ytd = total demand of composite good
// tau_c = consumption tax rate

varexo e ge inflf ix tau_l tau_ls tau_r ups_star;

// e = technology shock
// ge = domestic government spending shock
// inflf = foreign producer price inflation
// ix = foreign nominal interest rate
// tau_l = labour income tax rate
// tau_r = interest income tax
// tau_ls = lump sum tax
// ****ups_star = foreign demand of domestic coomposite good

parameters A0 bet d chi del cp cpf G0 phi the vsig alp epsi eta nn thep real_for usta om eua eub;

d       =	1;           // probability of still being alive next period, [0,1]
A0      =   1;             // steady state technology level, >0
bet 	=	0.97;           // time discount rate, [0,1]
chi 	=	0.3;           // capital share, [0,1]
cp      =	0.1;//1e-8;              // capital adjustment costs parameter, >= 0
cpf 	=	0.001; //(1-d);//1e-8;              // foreign bond adjustment costs parameter, >= 0
del 	=	0.05;           // depreciation rate of capital, [0,1]
G0      =   0; //10*(1-d);              // steady state government spending, >= 0
phi 	=	1;            // utility function parameter for substitutability between (consumption and money) and leisure, [0,1], 1 = leisure not valued
the 	=	1;                  // utility function parameter for curvature, >= 0, 1 = log utility
vsig	=	1;              // utility function parameter for substitutability between consumption and money, [0,1], 1 = money not valued
alp     =	0.5;           // **** home bias, [0,1]
epsi	=	0.2;           // **** demand curve parameter for production firms, >= 0, 0 = perfect competition
eta     =	0.2;            // **** elasticity of substitution parameter between domestic and foreign produced goods, >= 0,
nn      =	0.1;           // **** relative steady state size of domestic economy, [0,1]
thep	=	0.001; //*(1-d);//4183;        // **** price adjustment costs parameter, >= 0, 0 = flexible prices
real_for = 0.05;
usta = 15; //*(1-d);
om = 0.001; //(1-d);        // **** risk premium on foreign bonds
eua = -1;
eub = 0;
    
model;

//----------------------------------------------------------------------------------------------------------------------

// Household equations -> ok: 2 mrt 2009

    lab = 1 - ((1-phi)/(phi) * (cons_tax/lab_tax) * (c/w));        // (5.8)
    
    //((1)/(cons_tax*c)) = bet*(1+ni(+1))*(1/(cons_tax(+1)*c(+1)*infl(+1)));      // Works fine
    // (1/(cons_tax*c))*(((c)^phi)*((1-lab)^(1-phi)))^(1-the) = bet*(1+ni(+1))*(((c(+1))^phi)*((1-lab(+1))^(1-phi)))^(1-the)*(1/(cons_tax(+1)*c(+1)*infl(+1)));      // MJDGGES error 23
    // (1/cons_tax)*c^(phi-(phi*the)-1)*(1-lab)^((1-phi)*(1-the)) = bet*(1+ni(+1))*c(+1)^(phi-(phi*the)-1)*(1-lab(+1))^((1-phi)*(1-the))*(1/(cons_tax(+1)*infl(+1)));      // MJDGGES error 23
    // (1/cons_tax)*c^(1-the-1)*(1-lab)^((1-1)*(1-the)) = bet*(1+ni(+1))*c(+1)^(1-the-1)*(1-lab(+1))^((1-1)*(1-the))*(1/(cons_tax(+1)*infl(+1)));      // Works fine
    // (1/cons_tax)*c^(eua)*(1-lab)^(eub) = bet*(1+ni(+1))*c(+1)^(eua)*(1-lab(+1))^(eub)*(1/(cons_tax(+1)*infl(+1)));      // MJDGGES error 23
    (1/cons_tax)*c^(-1)*(1-lab)^(0) = bet*(1+ni(+1))*c(+1)^(-1)*(1-lab(+1))^(0)*(1/(cons_tax(+1)*infl(+1)));      // Works fine
    
    cero1 = n - (1+i(-1)*int_tax)*n(-1)/(infl) + tau_ls - lab_tax*w*lab + cons_tax*c - int_tax*divai;

//----------------------------------------------------------------------------------------------------------------------
    
// Actuarial firms -> ok: 23 feb 2009

    (1+r)*q=q(+1)+div(+1);         // (9.10)
    (1+r)*qf=qf(+1)+divf(+1);      // (9.11)
    lam = 1 + psi + (inv/k(-1)) * 2*cp*((inv/k(-1)) - del);                     // (A.12)
    (1+r)*lam = rk(+1)+ 2*cp*((inv(+1)/k) - del)*(inv(+1)/k)^2 + lam(+1)*(1-del);                // (A.13)
    divai = n + div + divf - (1 + i(-1))*n(-1)/infl + rk*k(-1)-inv-psi*inv;              // (9.14)
    0 = n(+1) + div(+1) + divf(+1) - (1 + r)*n + rk(+1)*k-inv(+1)-psi(+1)*inv(+1);              // (9.14)
    inv = k - (1-del)*k(-1);              // (9.15)
    psi = cp*(inv/k(-1)-del)^2;              // (9.16)

//----------------------------------------------------------------------------------------------------------------------
    
// Foreign investment firms -> ok: 23 feb 2009

    lamf = 1+2*cpf*dfb;                           // (9.17)
    (1+r)*lamf = rx - 2*om*fb(-1) + lamf(+1); // (9.18)
    divf = (1+ix(-1) - om*fb(-1))*fb(-1)/infl - fb - xi;             // (9.19)
    dfb = fb - fb(-1);                          // (9.20)
    xi  = cpf*(dfb)^2;                          // (9.21)
    nx  = fb - (1+ix(-1) - om*fb(-1))*fb(-1)/infl;               // (9.22)

//----------------------------------------------------------------------------------------------------------------------

// Government -> ok: 23 feb 2009

    g = G0 + ge;                           // (9.23)
    tau_ls = (g - tau_r*i(-1)*n(-1)/infl -tau_l*w*lab - tau_c*c ); // (9.24)  
   

//----------------------------------------------------------------------------------------------------------------------

// Aggregators block

    st/st(-1) = inflf/inflh;                            // (9.25)
    gs  = ((1-(1-nn)*alp)+(1-nn)*alp*st^((eta-1)/eta))^(eta/(eta-1));         //(9.26)
    gsts= ((1-nn*alp)+nn*alp*st^((1-eta)/eta))^(eta/(eta-1));                 //(9.27)
    nx  = (1/gs)*nn*alp*((gsts*st)^(1/eta))*ups_star - (1-nn)*alp*((gs/st)^((1-eta)/eta))*ups;                     // (9.28)
    infl = gs/gs(-1)*inflh;                                     // (9.29)

//----------------------------------------------------------------------------------------------------------------------
    
// Production firms -> ok: 23 feb 2009

    y   = a*(k(-1)^(chi))*lab^(1-chi);                  // (9.30)
    gs*w = rmc*(1-chi)*a*(k(-1)/lab)^chi;               // (9.31) 
    gs*rk = rmc*chi*a*(k(-1)/lab)^(chi-1);              // (9.32) 
    div     = y/gs - w*lab - rk*k(-1)-padjc/gs;         // (9.33) 
    padjc   = thep/2*(inflh-1)^2;                            // (9.34) 
    epsi*thep*inflh*(inflh-1) = (ups_d/nn)*(rmc-(1-epsi))+epsi*thep/(1+r)*gs/gs(+1)*inflh(+1)*(inflh(+1)-1);    // (9.35) 
    ups_d = (1-(1-nn)*alp)*gs^(1/eta)*ups + nn*alp*(gsts*st)^(1/eta)*ups_star + padjc;  // (9.42)
    ups = c+inv+g + psi*inv + xi;                                   // (9.43)
//----------------------------------------------------------------------------------------------------------------------

// Technology and definitions -> ok: 23 feb 2009

    a   = A0 + e ;                                                              // (9.36)
    y   = gs*(ups + nx) + padjc;                                                // (9.37) 
    ni  = int_tax*i(-1);                                                            // (9.38)
    1+rh= (1+ni(+1))/(infl(+1)*d);                                                      // (9.39)
    1+r = (1+i)/infl(+1);                                                           // (9.40)
    1+rx= (1+ix)/infl(+1);                                                          // (9.41)
    cero = y - ups_d;
  

    
//----------------------------------------------------------------------------------------------------------------------
   
// Within Dynare definitions

    int_tax = 1-tau_r;
    lab_tax = 1-tau_l;
    cons_tax = 1 + tau_c;
    ls_tax=tau_ls;

end;

load last_steady8b;

initval;

//endogenous variables
a      =  		 last_steady8b(1);
c        =		 last_steady8b(2);
cons_tax =		 last_steady8b(3);
dfb     = 		 last_steady8b(4);
div      =		 last_steady8b(5);
divf     	=	 last_steady8b(6);
fb       	=	 last_steady8b(7);
// fw       		= last_steady6(8);
g  =      		 last_steady8b(8);
// h    =    		 last_steady6(10);
// hw    =   		 last_steady6(11);
i        	=	 last_steady8b(9);
infl     	=	 last_steady8b(10);
int_tax  	=	 last_steady8b(11);
inv      		= last_steady8b(12);
k    =    		 last_steady8b(13);
lab   =   		 last_steady8b(14);
lab_tax = 		 last_steady8b(15);
lam      =		 last_steady8b(16);
lamf     	=	 last_steady8b(17);
ls_tax   	=	last_steady8b(18);
// m        =		 last_steady6(22);
n        	=	 last_steady8b(19);
ni       		= last_steady8b(20);
nx =      		 last_steady8b(21);
psi  =    		 last_steady8b(22);
q       = 		 last_steady8b(23);
qf       =		 last_steady8b(24);
r        	=	 last_steady8b(25);
rh       	=	 last_steady8b(26);
rk     =  		 last_steady8b(27);
rx       =		 last_steady8b(28);
// s        	=	 last_steady6(33);
w    =    		 last_steady8b(29);
// x       = 		 last_steady6(35);
xi       =		 last_steady8b(30);
y        	=	 last_steady8b(31);
// z        	=	 last_steady6(38) ;
    gs              = 0.95 ;   
    gsts            = 1 ;  
    inflh           = 1 ;
    padjc           = 0 ;   
    rmc             = 0.9 ;   
    st              = 0.9 ;  
    ups             = c+inv+g  + xi + psi*inv;
    ups_d           = ups;
    cero            = 0;
    divai = 0;

    cero1 = 0;

//exogenous variables
    e       = 0;
    ge      = 0;
    inflf    = 1;
    ix      = inflf-1+real_for;   // (1-bet*d)
    tau_c   = 0;   
    tau_ls   = 0;
    tau_r   = 0;
    tau_l = 0;
    ups_star = usta;    // ****

end;
steady;
check;

write_latex_dynamic_model;

endval;
ups_star = usta+1;
end;

steady;
check;

// shocks;
// var ups_star;
// periods 1:19;
// values usta;
// end;

simlength = 1000;
simul(periods=1000);

