 
% ----- --------------------------------------------------------------
% Endogenous Variables
% ----- --------------------------------------------------------------
var 	
nubeta      % discount factor shock (domestic)
nun         % labor supply shock (domestic)
nuc         % taste shock (domestic)
nui         % investment shock (domestic)
nup         % productivity shock  (domestic)
tauyd       % output tax rate (domestic)
taun        % labor tax rate (domestic)
tauk        % capital tax rate (doemstic)
taud        % tax rate on doemstic consumption (domestic)
taum        % tax rate on imported consumption (domestic)
gd          % consumption on domestic goods by government (domestic)
gm          % consumption on imported goods by government (domestic)
g           % total governemnt spending (domestic)
pid         % domestic inflation rate (domestic) 
pi          % cpi inflation (domestic)
pidopt      % reset price inflation (domestic)
psigma      % price dispersion (domestic)
f           % numerator of reset price inflation (domestic)
gpr         % denominator of reset price inflation (domestic)
x           % total consumption (domestic)
c           % total habit adjusted consumption in steady state (domestic)
cd          % domestic consumption (domestic)
cm          % imported consumption (domestic)
n           % labor supply (domestic)
w           % real wage rate (domestic)
r           % real rate of return on capital (domestic)
k           % stock of future capital (domestic)
i           % investment (domestic)
yd          % domestic output supplied/demanded (domestic)
lambda2     % multiplier of investment/ marginal utility of investment (domestic)
mc          % marginal cost in steady state (domestic)
lambda1     % real marginal utility of money (domestic)
ydnat       % natural rate of domestic output in steady state (domestic)
% foreign endo variables
nubetas     % discount factor shock (foreign)
nuns        % labor supply shock (foreign)
nucs        % taste shock (foreign)
nuis        % investment shock (foreign)
nups        % productivity shock (foreign)
tauyds      % output tax rate (foreign)
tauns       % labor tax rate (foreign)
tauks       % capital tax rate (foreign)
tauds       % tax rate on doemstic consumption (foreign)
taums       % tax rate on imported consumption (foreign)
gds         % consumption on domestic goods by government (foreign)
gms         % consumption on imported goods by government (foreign)
gs          % total governemnt spending (foreign)
pids        % domestic inflation rate (foreign)
pis         % cpi inflation (foreign)
pidopts     % reset price inflation (foreign)
psigmas     % price dispersion (foreign)
fs          % numerator of reset price inflation (foreign)
gprs        % denominator of reset price inflation (foreign)
xs          % total consumption (foreign)
cs          % total habit adjusted consumption (foreign) 
cds         % domestic consumption (foreign)
cms         % imported consumption (foreign)
ns          % labor supply (foreign)
ws          % real wage rate (foreign)
rs          % real rate of return on capital (foreign) 
ks          % stock of future capital (foreign)
is          % investment (foreign)
yds         % domestic output supplied/demanded (foreign)
lambda2s    % multiplier of investment/ marginal utility of investment (foreign)
mcs         % marginal cost (foreign)
lambda1s    % real marginal utility of money (foreign)
ydnats      % natural rate of domestic output (foreign)
num         % monetary policy shock (cu)
icu         % nominal interest rate  (cu)
ygapcu      % output gap  (cu)
picugap     % inflation gap (cu)
picu        % inflation (cu)
picunat     % natural steady state inflation (cu)
tau         % terms of trade between two countries (cu)
ycu         % currency union output
ycun        % natural rate of currency union output
; 
 
 
% ----- --------------------------------------------------------------
% Exogenous Variables 
% ----- --------------------------------------------------------------
varexo 
ebeta       % discount factor shock error (domestic)
en          % labor supply shock error (domestic)
ecshock     % taste shock error (domestic)
ei          % investment shcok error (domestic)
ep          % productivity shock error (domestic)
//etauyd 
//etaun 
//etauk 
//etaud 
//etaum
epsg
ebetas 
ens 
ecshocks
eis 
eps 
//etauyds 
//etauns 
//etauks 
//etauds 
//etaums
epsgs
em
; 
 
predetermined_variables k ks;
 
parameters 
rhob 
rhon 
rhoc 
rhoi 
rhop 
rhotauyd 
rhotaun 
rhotauk 
rhotaud 
rhotaum 
el
eg 
ej
om
omg
rhog 
xi 
mun 
pidss           % steady state domestic inflation rate (domestic)
pidsss          % steady state domestic inflation rate (foreign)
muc 
ec 
betta 
h 
kappa  
zeta        // population proportion (domestic)
zetas       // population proportion (foreign) zeta + zetas = 1
alppha 
delta 
egs 
oms
omgs 
rhom 
psiygap 
psipi
rhoicu 
taudss
taumss
taukss
taunss
tauydss
gss
sigmataud
sigmataum
sigmatauk
sigmataun
sigmatauyd
sigmag
sigmam
sigmac
sigman
sigmabeta
sigmai
sigmap
taudsss
sigmatauds
taumsss
sigmataums
tauksss
sigmatauks
taunsss
sigmatauns
tauydsss
sigmatauyds
gsss
sigmags
sigmacs
sigmans
sigmabetas
sigmais
sigmaps
tauss
icuss
;
 
 

// Parameters Values (Calibrated/Estimated Parameters)

rhob        = 0.9  ;
rhon        = rhob ;
rhoc        = rhob ;
rhoi        = rhob ;
rhop        = rhob ;
rhotauyd    = rhob ;
rhotaun     = rhob ;
rhotauk     = rhob ;
rhotaud     = rhob ;
rhotaum     = rhob ;
el          = 1.1  ; %// substitution of goods across countries
eg          = el   ;
ej          = 10   ;     
om          = 0.3  ;
omg         = 0.3  ;
rhog        = 0.8  ;
xi          = 0.9  ;
mun         = 10;
pidss       = 0.02 ;        % Steady state inflation rate is at 2% for domestic country.
pidsss       = 0.02      ;   % steady state inflation rate is at 2% for foreign country. 
muc         = 1;
ec          = 2.000;        %// Intertemporal Elasticity for Consumption, Sims 2.47
betta        = 0.998;
h           = 0.8  ; 
kappa       = 4.00 ;        % Sims (2016)
zeta        = 1/3  ;        % Population in Home Country
zetas       = 1 - zeta  ;
alppha       = 1/3  ;
delta       = 0.025;
egs         = eg   ;
oms         = 0.3  ;
omgs        = oms  ;
rhom        = 0.7  ;
psiygap     = 0.000;
psipi       = 0.000;
%//psiygap     = 1.000;
%//psipi       = 2.500;
rhoicu      = 0.900;
taudss      = 0.01    ;
taumss      = 0.01    ;
taukss      = 0.01    ;
taunss      = 0.01    ;
tauydss     = 0.01    ;
gss         = 0.20    ;
sigmataud   = 0.1  ;
sigmataum   = 0.1  ;
sigmatauk   = 0.1  ;
sigmataun   = 0.1  ;
sigmatauyd  = 0.1  ;
sigmag      = 0.1  ;
sigmam      = 0.1  ;
sigmac      = 0.100;
sigman      = 0.100;
sigmabeta   = 0.100;
sigmai      = 0.1  ;
sigmap      = 0.1  ;
taudsss     = 0.01    ;
sigmatauds  = 0.1  ;
taumsss     = 0.01    ;
sigmataums  = 0.1  ;
tauksss     = 0.01    ;
sigmatauks  = 0.1  ;
taunsss     = 0.01    ;
sigmatauns  = 0.1  ;
tauydsss    = 0.01    ;
sigmatauyds = 0.1  ;
gsss        = 0.20 ; 
sigmags     = 0.1  ;
sigmacs     = 0.1  ;
sigmans     = 0.1  ;
sigmabetas  = 0.1  ;
sigmais     = 0.1  ;
sigmaps     = 0.1  ;
tauss = 1.0        ; %// steady state terms of trade
% ----- --------------------------------------------------------------
% Model Block
% ----- --------------------------------------------------------------
 
 
 
model;
% Consumers First Order Conditions

% 1 Marginal Utility of Money Definition for consumer
lambda1 = (x - muc * nuc)^(-ec);
% 2 Consumption Definition for domestic consumer
x = c - h * c(-1);
% 3 Eulers Equation for consumer
(nubeta *lambda1 *(1+pi))/(betta* nubeta(+1)* lambda1(+1)) = (nubetas * lambda1s * (1+pis))/(betta * nubetas(+1) * lambda1s(+1));
% 4 Fischer Equation for consumer
mun * n * nun = lambda1 * w * (1-taun);
% 5 International Risk Sharing on account of complete markets for consumers
lambda1*(1+pi(+1))*nubeta = lambda1s * (1+pis(+1))*nubetas;
% 6 Investment foc for consumer
lambda1 = lambda2 * nui* ( 1 -  ( (kappa/2) * ((i/i(-1)) - 1)^2 ) - (((nui * i * kappa)/(i(-1)) ) * ((i/i(-1)) - 1)) ) + (betta * (nubeta(+1)/nubeta) * nui(+1) * lambda2(+1) * kappa * ((i(+1)/i) - 1)*(i(+1)/i)^2);
%7 Future capital stock for consumer
lambda2 = betta * (nubeta(+1)/nubeta) * ((r(+1) *lambda1(+1) * (1-tauk(+1))) + (lambda2 * (1-delta)));
% 8 Capital Accumulation equation
k(+1) =  k*(1-delta) + (nui * (1- (kappa/2) *((i/i(-1)) - 1)^2) * i ) ;

% Economy Wide Prices

% 9 Domestic price index composition between calvo and non calvo firms
(1+pid)^(1-ej) = ( xi*(1+pidss)^(1-ej) + (1-xi)*(1+pidopt)^(1-ej) );
% 10 CPI between domestic price and foreign price (via tot)
(1+pi)^(1-el) = ( ((1-om)*(1+taud)^(1-el)+ om * (1+taum)^(1-el) *tau^(1-el) )/((1-om)*(1+taud(-1))^(1-el) + om * (1+taum(-1))^(1-el) * tau(-1)^(1-el) ) ) * (1+pid)^(1-el);
% 11 Price Dispersion on account of Monopolistic competition
psigma = (1-tauyd)^(ej)*((1-xi) * ((1+pidopt)/(1+pid))^(-ej) + xi * ((1+pid)/(1+pidss))^(ej) * psigma(-1));
% 12 Numerator for domestic price evolution for Calvo firms as a function of average 
f = nubeta*lambda1*(1-tauyd)^(-ej)*mc*yd + xi*betta*((1+pid(+1))/(1+pidss))^(ej)*f(+1);
% 13 Denominator for domestic price evolution for Calvo firms as a function of average 
gpr = nubeta*lambda1*(1-tauyd)^(-ej)*yd + xi*betta*((1+pid(+1))/(1+pidss))^(ej-1)*gpr(+1);
% 14 Domestic Price evolution for Calvo Firms
(1+pidopt) = (ej/(ej-1))*(f/gpr)*(1+pid);
% 15 Terms of trade between the economies
tau = ((1+pids)/(1+pid))* tau(-1);

% Firms Optimality

% 16 Optimal Input Ratio economy-wide
(w*alppha)/(r*(1-alppha)) = (k/n);
% 17 real marginal cost
mc = (1/nup)*((n/k)^(alppha))*(w/(1-alppha));
% 18 Input markets and total tradable output (used for natural rate purposes)
nup*(k/n)^(alppha) * n = yd*(1-tauyd)^(ej)*psigma^(-1);


% Aggregate Market Clearing

% 19 Total output demand
yd = cd + gd + i + (zetas/zeta)*(cms + gms);
% 20 Domestic consumption of home countrys final good by consumer
cd = (((1-om)* x)/((1+taud)^(el))) * ((1-om)*(1+taud)^(1-el) + om * (1+taum)^(1-el) * tau^(1-el))^(el/(1-el));
% 21 Import by home country consumers (demand for foreign goods)
cm =  (((om)* x)/((1+taum)^(el))) * ((1-om)*(1+taud)^(1-el) * tau^(-(1-el)) + om*(1+taum)^(1-el))^(el/(1-el));
% 22 Domestic consumption of home countrys final good by government
gd = (((1-omg)* g)/((1+taud)^(eg))) * ((1-omg)*(1+taud)^(1-eg)+ omg*(1+taum)^(1-eg) * tau^(1-eg))^(eg/(1-eg));
% 23 Import by home country government (demand for foreign goods)
gm = (((omg)* g)/((1+taum)^(eg))) * ((1-omg)*(1+taud)^(1-eg)* tau^(-(1-eg)) + omg*(1+taum)^(1-eg) )^(eg/(1-eg));


% Fiscal Policy Processes

% 24 Taxes on domestic consumption
taud = (1- rhotaud) * taudss + rhotaud * taud(-1) ;
%taud = (1- rhotaud) * taudss + rhotaud * taud(-1) + sigmataud * etaud;
% 25 Taxes on Imported consumption
taum = (1- rhotaum) * taumss + rhotaum * taum(-1) ;
%taum = (1- rhotaum) * taumss + rhotaum * taum(-1) + sigmataum * etaum;
% 26 Tases on Rental Income from Capital
tauk = (1- rhotauk) * taukss + rhotauk * tauk(-1);
%tauk = (1- rhotauk) * taukss + rhotauk * tauk(-1) + sigmatauk * etauk;
% 27 Taxes on Labor Income
taun = (1- rhotaun) * taunss + rhotaun * taun(-1);
%taun = (1- rhotaun) * taunss + rhotaun * taun(-1)+ sigmataun * etaun;
% 28 Taxes on Selling Final output to the market on Firms
tauyd = (1- rhotauyd) * tauydss + rhotauyd * tauyd(-1) ;
%tauyd = (1- rhotauyd) * tauydss + rhotauyd * tauyd(-1) + sigmatauyd * etauyd;
% 29 exogenous government spending, equation is in logs
g = (1- rhog) * gss*yd + rhog * g(-1) + sigmag * epsg;

% Monetary Policy and Processes
% 30 ZLB version of Taylors Rule commented out. 
icu =  (1-rhoicu) * ( psiygap *ygapcu + psipi * picugap + icuss) + rhoicu * icu(-1)   + log(num);
% icu = max(0, psiygap * (1-rhoicu)* ygapcu + psipi  * picu + rhoicu * icu(-1)   + num);
% 31 Monetary policy shock processes
log(num) = rhom * log(num(-1)) + sigmam * em;

% Exogenous Processes
% 32 Taste Shock process
log(nuc) = rhoc*log(nuc(-1)) + sigmac * ecshock;
% 33 Labor Supply Shock
log(nun) = rhon*log(nuc(-1)) + sigman * en;
% 34 Discount Factor Shock: a demand shock
log(nubeta) = rhob* log(nubeta(-1)) + sigmabeta * ebeta;
% 35 Investment supply shock
log(nui) = rhoi* log(nui(-1)) + sigmai * ei;
% 36 Productivity Shock
log(nup) = rhop * log(nup(-1)) + sigmap * ep;

% Consumers First Order Conditions (Foreign Country)
% 37 Marginal Utility of Money Definition for consumer
lambda1s = (xs - muc * nucs)^(-ec);
% 38 Consumption Definition for foreign consumer
xs = cs - h * cs(-1);

        % 39 Eulers Equation for consumer (combined in equation 3, result of international risk sharing)
        %nubetas * lambda1s * (1+pis) = betta * nubetas(+1) * (1+icu) * lambda1s(+1);

% 39 Fischer Equation for consumer
mun * ns * nuns = lambda1s * ws * (1-tauns);
% 40 Investment foc for consumer
lambda1s = lambda2s * nuis * ( 1 -  ( (kappa/2) * ((is/is(-1)) - 1)^2 ) - (((nuis * is * kappa)/(is(-1)) ) * ((is/is(-1)) - 1)) ) + (betta * (nubetas(+1)/nubetas) * nuis(+1) * lambda2s(+1) * kappa * ((is(+1)/is) - 1)*(is(+1)/is)^2);
% 41 Future capital stock for consumer
lambda2s = betta* (nubetas(+1)/nubetas)*(rs(+1)*lambda1s(+1)*(1-tauks(+1))+lambda2s*(1-delta));
% 42 Capital Accumulation equation
ks(+1) =  ks*(1-delta) + (nuis * (1- (kappa/2) *((is/is(-1)) - 1)^2) * is ) ;



% Economy Wide Prices (Foreign Country)
% 43 Domestic price index composition between calvo and non calvo firms
(1+pids)^(1-ej) = ( xi*(1+pidsss)^(1-ej) + (1-xi)*(1+pidopts)^(1-ej) );
% 44 CPI between domestic price and foreign price (via tot)
(1+pis)^(1-el) = ( (((1-oms)*(1+tauds)^(1-el)+oms*(1+taums)^(1-el)*tau^(-1+el)))/(((1-oms)*(1+tauds(-1))^(1-el)+oms*(1+taums(-1))^(1-el)*tau(-1)^(-1+el))) ) * (1+pids)^(1-el);
% 45 Price Dispersion on account of Monopolistic competition
psigmas = (1-tauyds)^(ej)*((1-xi)*((1+pidopts)/(1+pids))^(-ej) + xi* ((1+pids)/(1+pidsss))^(ej)*psigmas(-1));
% 46 Numerator for domestic price evolution for Calvo firms as a function of average 
fs = nubetas*lambda1s*(1-tauyds)^(-ej)*mcs*yds + xi*betta*((1+pids(+1))/(1+pidsss))^(ej)*fs(+1);
% 47 Denominator for domestic price evolution for Calvo firms as a function of average 
gprs = nubetas*lambda1s*(1-tauyds)^(-ej)*yds + xi*betta*((1+pids(+1))/(1+pidsss))^(ej-1)*gprs(+1);
% 48 Domestic Price evolution for Calvo Firms
(1+pidopts) = (ej/(ej-1))*(fs/gprs)*(1+pids);

% Firms Optimality (Foreign Country)
% 49 Optimal Input Ratio economy-wide
(ws*alppha)/(rs*(1-alppha)) = (ks/ns);
% 50 real marginal cost
mcs = (1/nups)*((ns/ks)^(alppha))*(ws/(1-alppha));
% 51 Input markets and total tradable output (used for natural rate purposes)
nups *(ks/ns)^(alppha) * ns = yds * (1-tauyds)^(ej)* psigmas^(-1);


% Aggregate Market Clearing (Foreign Country)
% 52 Total output demand
yds = cds + gds + is + (zeta/zetas)*(cm + gm);
% 53 Domestic consumption of home countrys final good by consumer
cds = (((1-oms)* xs)/((1+tauds)^(el))) * ((1-oms)*(1+tauds)^(1-el) + oms*(1+taums)^(1-el) * tau^(-(1-el)))^(el/(1-el));
% 54 Import by home country consumers (demand for foreign goods)
cms =  (((oms)* xs)/((1+taums)^(el))) * ((1-oms)*(1+tauds)^(1-el) * tau^(1-el) + oms*(1+taums)^(1-el))^(el/(1-el));
% 55 Domestic consumption of home countrys final good by government
gds = (((1-omgs)* gs)/((1+tauds)^(eg))) * ((1-omgs)*(1+tauds)^(1-eg) + omgs*(1+taums)^(1-eg) * tau^(-(1-eg)))^(eg/(1-eg));
% 56 Import by home country government (demand for foreign goods)
gms = (((omgs)* gs)/((1+taums)^(eg))) * ((1-omgs)*(1+tauds)^(1-eg)*tau^(1-el) + omgs*(1+taums)^(1-eg))^(eg/(1-eg));


% Fiscal Policy Processes (Foreign Country)
% 57 Taxes on domestic consumption
tauds = (1 - rhotaud) * taudsss + rhotaud * tauds(-1) ;
%tauds = (1 - rhotaud) * taudsss + rhotaud * tauds(-1) + sigmatauds * etauds;
% 58 Taxes on Imported consumption
taums = (1- rhotaum) * taumsss + rhotaum * taums(-1) ;
%taums = (1- rhotaum) * taumsss + rhotaum * taums(-1) + sigmataums * etaums;
% 59 Tases on Rental Income from Capital
tauks = (1- rhotauk) * tauksss + rhotauk * tauks(-1) ;
%tauks = (1- rhotauk) * tauksss + rhotauk * tauks(-1) + sigmatauks * etauks;
% 60 Taxes on Labor Income
tauns = (1- rhotaun) * taunsss + rhotaun * tauns(-1);
%tauns = (1- rhotaun) * taunsss + rhotaun * tauns(-1)+ sigmatauns * etauns;
% 61 Taxes on Selling Final output to the market on Firms
tauyds = (1- rhotauyd) * tauydsss + rhotauyd * tauyds(-1);
%tauyds = (1- rhotauyd) * tauydsss + rhotauyd * tauyds(-1) + sigmatauyds * etauyds;
% 62 exogenous government spending, equation is in logs
gs = (1- rhog) * gsss*yds + rhog * gs(-1) + sigmags * epsgs;


% Exogenous Processes (Foreign Country)
% 63 Taste Shock process
log(nucs) = rhoc * log(nucs(-1)) + sigmacs * ecshocks;
% 64 Labor Supply Shock
log(nuns) = rhon * log(nuns(-1)) + sigmans * ens;
% 65 Discount Factor Shock: a demand shock
log(nubetas) = rhob * log(nubetas(-1)) + sigmabetas * ebetas;
% 66 Investment supply shock
log(nuis) = rhoi * log(nuis(-1)) + sigmais * eis;
% 67 Productivity Shock
log(nups) = rhop * log(nups(-1)) + sigmaps * eps;


% Currency Union Level Output and Inflation 
% 68 CPI Inflation at the Currency Union
picu = zeta * pi + zetas * pis;
% 69 Natural Rate of Currency Union Output
ycun = zeta * ydnat + zetas * ydnats;
% 70 Actual Currency Union Output
ycu = zeta * yd + zetas * yds;
% 71 Gap between actual and natural output
ygapcu = ycu - ycun;
% 72 Natural Output for Home economy
ydnat = yd;
% 73 Natural Output for Foreign Economy
ydnats = yds;
% 74 Natural Inflation Rate for CU
picunat = zeta*pidss + zetas*pidsss;
% 75 Inflation Gap for CU
picugap = picu - picunat;
end;
  
 
shocks;
var epsg;
stderr 0.009;
end;


steady;
check;
 
//stoch_simul(order = 1, relative_irf, irf=30) yd cd cm pi;
 

