   % \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
% \\\\\\\\\\\\\\\\\\\\\\\\\\\THE MODEL-2country-RBC: \\\\\\\\\\\\\\\\\\\\
%\\\International trade in goods,home bonds and home&foreign equities\\\\\\
% \\\\\\\\\\Capital and labour in the production function- NO Adj cost\\\\\\\\\
close all

% af1, af2, af3 etc are the zero-order portfolio holdings calculated with the
% Devereux-Sutherland (DS) method.Code taken from his website.


  
var A1 A2 Y1 Y2 C1 C2 W1 W2 N1 N2 beta_fun1 beta_fun2 Div1 Div2 I1 I2 K1 K2 rk1 rk2
    cd cg NFA re1 re2 Z1 Z2 NX
    RER dER rb1 rb2 NR1 NR2 M1 M2 RP RPSTAR PI_CPI1 PI_CPISTAR PI_P1 PI_P2 ; %   CPI1 CPISTAR P1 P2  
      
varexo ep1 ep2 ep3 ep4 zeta;

parameters consigma PSI PSI2 phi1 alpha epsilon theta tau tau2 
           rhoa rhoa2 rhoMP mudf Kdelta upsilon mupi muy eta mcss PI1bar;



% Parameter values: 

PI1bar=0;
consigma=1.5;                % Intertemporal substitution elasticity of consumption
PSI=19.563468957119511;      % Parameter in the labour function   
PSI2=19.563468957119511;
phi1=2;                      % Implies labour supply elasticity of 1/3(G&M, 2005). Disutility from work
alpha=0.4;                   % Share of domestic consumption allocated to imported goods (G&M, 2005)
epsilon=6;                   % E.S b/varieties within same country. Implies SS mark-up of 1.2 ( G&M2005)
theta=1.5;                   % E.S b/varieties produced at Home and Foreign country. Bakus et al(1994), Beningno and Thoenissen (2008), Corsetti et al use 0.85!!
tau=0.36;                    % Capital share in DOMESTIC(developed) production function: (D&S,2009 JDE)
tau2=0.36;                   % Capital share in FOREIGN (developing) production function: (D&S,2009 JDE)
eta=2/3;
mcss=0.833333333333333;      % Real marginal cost is constant due to flexible pricing. So, set as a parameter.
mudf=0.015971441225614;                   % Paramenter used in the endogenous discount factor. See D&S(2009)
Kdelta=0.025;                % annual capital depreciation rate. Standard literature.
upsilon=15;
%Shocks parameters
rhoa=0.99;                   % Domestic Productivity persistance (D&S,2009 JDE)   (0.66 Estimation G&M 2005)
rhoa2=0.99;                  % Foreign Productivity persistance  (D&S,2009 JDE) 
rhoMP=0.75;                   % Domestic Money persistance (DS1 dynare code)    
 % Weights
mupi=1.5;                    % Weight of inflation HOME     ( Taylor Rule)
%mur=0.8;                     % Weight of interest rate HOME ( Taylor Rule)
muy=0.5/4;                   % Weight of output rate HOME   ( Taylor Rule)

% Standard deviations of the innovation of the shocks
sta=0.01;                    % Std deviation of the innovation of DOMESTIC technology shock (0,007 G&M, 2005) (D&S,2009 JDE Annual 2% => 2%*1/sqr(4)=1% quarterly)
sta2=0.01;                   % Std deviation of the innovation of FOREIGN technology shock About double size of home (D&S,2009 JDE Annual 5% => 5%*1/sqr(4)=2.5% quarterly)

dr={'re1','re2','rb1','rb2'};                  

parameters af1 af2 af3;
af1 = 0;
af2 = 0;
af3=0;

model; 

NFA = rb1*NFA(-1)+(W1*N1)+(rk1*K1(-1))-C1-I1+(Div1)+ (STEADY_STATE(K1))*((STEADY_STATE(beta_fun1)*af1*((re1) -(rb1)))+(STEADY_STATE(beta_fun1)*af2*((re2) -(rb1))) +(STEADY_STATE(beta_fun1)*af3*((rb2) -(rb1))) + zeta);

end;

% Main model equations

model;

cd = C1 - C2;                   

cg = (1/2)*(C1 + C2);          


A1=rhoa*A1(-1) + ep1;           % 4.Domestic Productivity shock   
A2=rhoa2*A2(-1) + ep2;          % 5.Foreign Productivity shock   

% HOME and FOREIGN SET OF EQUATIONS

%6 7 Endogenous discount factor withOUT internalization. S.Schmitt-Grohe and Uribe (JIE2003) P.169
(beta_fun1)=(1+(C1))^(-mudf);
(beta_fun2)=(1+(C2))^(-mudf);

% 8 9 Euler equation C : rt is the interest rate KNOWN at t that pays off in period t + 1
(C1)^(-consigma)= (beta_fun1)*(C1(+1))^(-consigma)*(rb1(+1));   
(C2)^(-consigma)= (beta_fun2)*(C2(+1))^(-consigma)*(rb1(+1))*(RER/RER(+1));   

%10 11 12 Rest of portfolio eulers 
(C1)^(-consigma)= (beta_fun1)*(C1(+1))^(-consigma)*(re1(+1));
(C1)^(-consigma)= (beta_fun1)*(C1(+1))^(-consigma)*(re2(+1));  
(C1)^(-consigma)= (beta_fun1)*(C1(+1))^(-consigma)*(rb2(+1));  


%13 Resources contraint
Y1+Y2=(RP)^(-theta)*((1-alpha)*C1+alpha*RER^theta*C2)+I1+(RPSTAR)^(-theta)*((1-alpha)*C2+alpha*(1/RER)^theta*C1)+I2;

%14 15 Labour Supply N
(W1)=(PSI)*((C1)^(consigma))*(N1)^phi1;  
(W2)=(PSI)*((C2)^(consigma))*(N2)^phi1;  
                
%16 17 Production Function: y
(Y1)=exp(A1)*((N1)^(1-tau))*(K1(-1))^tau;   
(Y2)=exp(A2)*((N2)^(1-tau))*(K2(-1))^tau;  

%18 19 Labour Demand: W
(W1)= exp(A1)*mcss*(1-tau)*((N1)^(-tau))*(K1(-1))^(tau);      
(W2)= exp(A2)*mcss*(1-tau)*((N2)^(-tau))*(K2(-1))^(tau);

%20 net exports
NX=((alpha)*((RP/RER)^(-theta))*C2)-((alpha)*((RPSTAR*RER)^(-theta))*C1);

%21 22 Real Dividends.Note that W and K are real variables ( divided by CPIt)
%(P1/CPI1)*(Div1)=(Y1*(P1/CPI1))-(W1)*(N1)-(rk1)*(K1(-1)); %UNIT ROOT
%(P2/CPISTAR)*(Div2)=(Y2*(P2/CPISTAR))-(W2)*(N2)-(rk2)*(K2(-1)); %UNIT ROOT
(RP)*(Div1)=(Y1*RP)-(W1)*(N1)-(rk1)*(K1(-1));
(RPSTAR)*(Div2)=(Y2*RPSTAR)-(W2)*(N2)-(rk2)*(K2(-1));

%23 24 Capital supply rk             
(re1(+1))=(rk1(+1))+(1-Kdelta);
(re1(+1))*(RER/RER(+1))=(rk2(+1))+(1-Kdelta);

%25 26 Gross Investment I
(I1)=(K1)-(1-Kdelta)*(K1(-1));   
(I2)=(K2)-(1-Kdelta)*(K2(-1)); 

%27 28 Capital Demand:K1(-1) K
(rk1)= mcss*exp(A1)*tau*((N1)^(1-tau))*(K1(-1))^(tau-1);  
(rk2)= mcss*exp(A2)*tau*((N2)^(1-tau))*(K2(-1))^(tau-1);  

%29 30 Gross rate of returns on financial assets. Z1 Z2 are REAL PRICES
%re1=((Div1+Z1)*P1*CPI1(-1))/(Z1(-1)*P1(-1)*CPI1); %UNIT ROOT
%re2=((Div2+Z2)*P2*ER*CPISTAR(-1))/(Z2(-1)*CPISTAR*ER(-1)*P2(-1)); %UNIT ROOT
re1=((Div1+Z1)*RP)/(Z1(-1)*RP(-1));
re2=((Div2+Z2)*RPSTAR*dER)/(Z2(-1)*RPSTAR(-1));

% NOMINAL VARIABLES AND EQUATIONS 31 32

%NR1/(STEADY_STATE(NR1))=(((CPI1/CPI1(-1))/(1+PI1bar))^mupi)*(((Y1)/(STEADY_STATE(Y1)))^mupi)+ep3; %UNIT ROOT
%NR2/(STEADY_STATE(NR2))=(((CPISTAR/CPISTAR(-1))/(1+PI1bar))^mupi)*(((Y2)/(STEADY_STATE(Y2)))^mupi)+ep4; %UNIT ROOT
NR1/(STEADY_STATE(NR1))=(((PI_CPI1)/(1+PI1bar))^mupi)*(((Y1)/(STEADY_STATE(Y1)))^mupi)+ep3;
NR2/(STEADY_STATE(NR2))=(((PI_CPISTAR)/(1+PI1bar))^mupi)*(((Y2)/(STEADY_STATE(Y2)))^mupi)+ep4;

% Money demand equations: FOC w.r.t to money.33 34
%(M1/CPI1)=((C1^consigma)/(1-(1/NR1)))^(1/mupi);  % NOMINAL MONEY 
%(M2/CPISTAR)=((C2^consigma)/(1-(1/NR2)))^(1/mupi); 
(M1)=((C1^consigma)/(1-(1/NR1)))^(1/mupi);  % REAL MONEY 
(M2)=((C2^consigma)/(1-(1/NR2)))^(1/mupi); 

%35 36
rb1=(NR1(-1))/PI_CPI1;
rb2=(NR2(-1)*dER)/(PI_CPISTAR);

%37 38
%(CPI1)=(((1-alpha)*(P1)^(1-theta))+(alpha)*(P2*ER)^(1-theta))^(1/(1-theta));%UNIT ROOT
%(CPISTAR)=(((1-alpha)*(P2)^(1-theta))+(alpha)*(P1/ER)^(1-theta))^(1/(1-theta));%UNIT ROOT
1=(((1-alpha)*(RP)^(1-theta))+(alpha)*(RPSTAR*RER)^(1-theta))^(1/(1-theta));
1=(((1-alpha)*(RPSTAR)^(1-theta))+(alpha)*(RP/RER)^(1-theta))^(1/(1-theta));

% 39 40 RELATIVE PRICES
RP/RP(-1)=PI_P1/PI_CPI1;
RPSTAR/RPSTAR(-1)=PI_P2/PI_CPISTAR;

% 41 42 
%ER=M2/M1; % UNIT ROOT
%dER(+1)=NR1/NR2;
dER=PI_P2/PI_P1;
%RER=(CPISTAR*ER)/CPI1;
%dER=(M2/M2(-1))/(M1/M2(-1));
RER/RER(-1)=(dER*PI_CPISTAR)/PI_CPI1;


end;

initval; 
cd = 0;
cg =0.876238900469134;
NFA=0;
A1=0;
A2=0;
NX=0;
dER=1;
RER=1;
RP=1;
RPSTAR=1;
PI_CPI1=1;
PI_CPISTAR=1;
PI_P1=1;
PI_P2=1;
%CPI1=1;
%CPISTAR=1;
%P1=1;
%P2=1;
M1=18.877994839260968;
NR1=1.010101010101010;
M2=18.877994839260968;
NR2=1.010101010101010;
N1=(0.3333333333333333);
N2=(0.3333333333333333);
Y1=(1.114338583396246);
Y2=(1.114338583396246);
C1=(0.876238900469134);
C2=(0.876238900469134);
I1=(0.238099682927112);
I2=(0.238099682927112);
K1= (9.523987317084471);
K2=(9.523987317084471);
rk1=(0.035101010101010);
rk2=(0.035101010101010);
W1= (1.782941733433994);
W2= (1.782941733433994);
beta_fun1=(0.990000000000000);
beta_fun2=(0.990000000000000);
re1=(1.010101010101010);
re2=(1.010101010101010);
rb1=(1.010101010101010);
rb2=(1.010101010101010);
Div1=(0.185723097232708);
Div2=(0.185723097232708);
Z1=(18.386586626038042); 
Z2=(18.386586626038042);

end;

resid(1);
steady; 
resid(1); 
check;
model_diagnostics; 

shocks;     
var ep1; stderr 0.01;    
var ep2; stderr 0.01;   
var ep3; stderr 0.01;  
var ep4; stderr 0.01;    
  
end;



%--------------------------------------------------------------------------
% Stage 1: calculate zero-order asset holdings
%--------------------------------------------------------------------------


stoch_simul(order=1,noprint,nomoments,irf=0);


BB=oo_.dr.ghu;  
nvar=M_.endo_nbr;
nu=M_.exo_nbr-1;
ordo=oo_.dr.order_var;
for i=1:nvar
    varo{i,1} = M_.endo_names(ordo(i),:);
end

SIGMA=M_.Sigma_e(1:nu,1:nu);    

icd=inx('cd',varo);   

[dm,na]=size(dr);  

for i=1:na         
    ia(i)=inx(dr{i},varo);
end

D1=BB(icd,nu+1);    
D2=BB(icd,1:nu);   



for k=1:na-1        
    R1(k,:)=BB(ia(k),nu+1)-BB(ia(na),nu+1);
    R2(k,:)=BB(ia(k),1:nu)-BB(ia(na),1:nu);
end

alpha_tilde=inv(R2*SIGMA*D2'*R1'-R2*SIGMA*R2'*D1)*R2*SIGMA*D2';



% Set af1, af2 etc to calculated values 

npr=M_.param_nbr-na+1;  



for i=1:na-1 
    alval{i,1}=['M_.params( ' num2str(i+npr) ' )=alpha_tilde(' num2str(i) ',1);'];
    alval{i,2}=['af' num2str(i) '=alpha_tilde(' num2str(i) ',1);'];
end   


for i=1:na-1
    eval(alval{i,1});
    eval(alval{i,2});
end  
