%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The model code is based on Lawrence J. Christiano, Mathias Trabandt and Karl Walentin, 2011. Introducing financial frictions and unemployment into a small open economy model, Journal of Economic Dynamics and Control, Elsevier, vol. 35(12), pages 1999-2041.
% Adapted by Ginters Buss for Latvia as a small open economy in a monetary
% union.
% 
% If these codes are useful, cite: 
% Ginters Buss (2015) "Search-and-matching frictions and labor market dynamics in Latvia", Working Papers 2015/04, Latvijas Banka.
% 
% Copyright of this version: Latvijas Banka
% These codes are distributed by Latvijas Banka for academic purposes. 
% If in doubt, contact Latvijas Banka.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Here we solve the core equations of the nonlinear steady state. By
%substitution we have reduced the system of equation to the set of
%equations listed below

function ff=finfric_steadystatesolvecore_noDelta3(xxx,pibar,mupsi,tauk,pkprime,muzplus,R,alpha,Rf,epsilon,lambdad,...
            etag,Rx,etax,omegax,pmx,px,etaf,omegai,pinvest,etai,Upsilon,zetac,beta,b,pc,tauc,zetah,sigmaL,omegac,...
            etac,Rnustar,tauw,tauy,etaa,taud,s,phalo,pitildew,lambdamc,pmc,mc,piw,phalomi,phalomc,...
            phalomx,Rstar,lambdami,pmi,lambdamx,phalox,lambdax,Rk,mu,we,Fomegabar,xiw,lambdaw,...
            intensity_target,iy_target,xy_target,nk_target,taumx,taumi,taumc,mcmx,mcmc,mcmi,taux,mcx,delta); 
 

%backing out the guesses for the variables to be solved for
sigma=xxx(1)*.41;
omegabar=xxx(2)*.39;
Rk=xxx(3)*1.05;
kbar=xxx(4)*1.12;
wbar=xxx(5)*.8;
H=xxx(6)*.27;
ystar=xxx(7)*.07;
rkbar=xxx(8)*.13;
AL=xxx(9)*4.16;
%delta=xxx(10);
varphi=xxx(10)*2.37;

        %Relative import prices
pmx=taumx*Rnustar*varphi/mcmx;
pmc=taumc*Rnustar*varphi/mcmc;
pmi=taumi*Rnustar*varphi/mcmi;

%Relative consumption price
pc=(1-omegac+omegac*pmc^(1-etac))^(1/(1-etac));

%Relative investment price
pinvest=(1-omegai+omegai*pmi^(1-etai))^(1/(1-etai));

%Relative export price
px=(taux*Rx/(mcx*varphi))*(omegax*pmx^(1-etax)+1-omegax)^(1/(1-etax));

%Relative price of capital
pkprime=pinvest/Upsilon;

%some abbreviations
z       =   (log(omegabar)+sigma^2/2)/sigma;
Gamma   =   normcdf(z-sigma)+omegabar*(1-normcdf(z));
Gam_muG =   (1-mu)*normcdf(z-sigma)+omegabar*(1-normcdf(z));


%Rental rate on capital
equ1=rkbar-(Rk*mupsi*pkprime/pibar-(1-delta)*pkprime-tauk*delta*mupsi*pkprime/pibar)/(1-tauk);

%Real Wage
equ2=wbar-(mc/(taud*(1/(1-alpha))^(1-alpha)*(1/alpha)^alpha*rkbar^alpha*Rf^(1-alpha)/epsilon))^(1/(1-alpha));

%Zero profit and law of motion for net worth
%equ3=kbar-1/(pkprime*(1-Rk/R*Gam_muG))...
%    *we/(1-gamma/(pibar*muzplus)*((Rk-R-(Gamma-Gam_muG)*Rk)*pkprime/(pkprime*(1-Rk/R*Gam_muG))+R));

%zero profits of banks
equ3=-nk_target+(1-Rk/R*Gam_muG);


% Capital labor ratio
equ4=kbar/H-muzplus*((taud*mupsi^alpha*wbar*Rf)/((1-alpha)*mc))^(1/alpha);



%Trade balance
equ5=(1-Rstar*s/pibar/muzplus)*etaa*phalo^(lambdad/(lambdad-1))*epsilon*(kbar/H/(mupsi*muzplus))^alpha...%assets
    /(1+phalo^(lambdad/(lambdad-1))*(1/mc-phalo^(lambdad/(1-lambdad))))*H...
    +varphi*Rnustar*(... %imports
    omegac*(pc/pmc)^(etac)*phalomc^(lambdamc/(1-lambdamc))...%import consumption
    *1/(zetah*AL)*((1-xiw*(pitildew/piw)^(1/(1-lambdaw)))/(1-xiw))^(1-lambdaw*(1+sigmaL))...
    *(wbar*(1-tauy)/(lambdaw*(1+tauw)))*(1-beta*xiw*(pitildew/piw)^(lambdaw*(1+sigmaL)/(1-lambdaw)))...
    *(zetac/(muzplus-b)*(muzplus-beta*b)/(pc*(1+tauc)))/(1-beta*xiw*(pitildew/piw)^(1/(1-lambdaw)))*H^(-sigmaL)...
    +phalomi^(lambdami/(1-lambdami))*omegai*(pinvest/pmi)^(etai)*kbar*(1-(1-delta)/(muzplus*mupsi))/Upsilon...%import investment
    +phalomx^(lambdamx/(1-lambdamx))*omegax*(1/pmx*(omegax*pmx^(1-etax)+1-omegax)^(1/(1-etax)))^etax...%import export
    *phalox^(-lambdax/(lambdax-1))*px^(-etaf)*ystar )...
    -varphi*px*px^(-etaf)*ystar; %exports


%Feasibility
equ6=(1-etag)*(phalo^(lambdad/(lambdad-1))*epsilon*(kbar/H/(mupsi*muzplus))^alpha)...
    /(1+phalo^(lambdad/(lambdad-1))*(1/mc-phalo^(lambdad/(1-lambdad))))*H...    %(1-etag)*y
    -(omegax*pmx^(1-etax)+1-omegax)^(etax/(1-etax))*(1-omegax)*phalox^(-lambdax/(lambdax-1))*px^(-etaf)*ystar...  % exports
    -(Gamma-Gam_muG)*Rk*pkprime*kbar/(pibar*muzplus)...%financial frictions monitoring costs
    -(1-omegai)*pinvest^(etai)*kbar*(1-(1-delta)/(muzplus*mupsi))/Upsilon...%investment
    -(1-omegac)*(pc^etac)*1/(zetah*AL)*((1-xiw*(pitildew/piw)^(1/(1-lambdaw)))/(1-xiw))^(1-lambdaw*(1+sigmaL))... %consumption
    *(wbar*(1-tauy)/(lambdaw*(1+tauw)))*(1-beta*xiw*(pitildew/piw)^(lambdaw*(1+sigmaL)/(1-lambdaw)))...
    *(zetac/(muzplus-b)*(muzplus-beta*b)/(pc*(1+tauc)))/(1-beta*xiw*(pitildew/piw)^(1/(1-lambdaw)))*H^(-sigmaL);


    
%FOC for loan contract
equ7=(1-Gamma)*Rk/R+(1-Fomegabar)/(1-Fomegabar-mu*omegabar*lognpdf(omegabar,-sigma^2/2,sigma))*(Rk/R*Gam_muG-1);

%Bancruptcy rate requirement for omega distribution
equ8=logncdf(omegabar,-sigma^2/2,sigma)-Fomegabar;

whalo=((1-xiw*(pitildew/piw)^(1/(1-lambdaw)))/(1-xiw))^(1-lambdaw)...
    /((1-xiw*(pitildew/piw)^(lambdaw/(1-lambdaw)))/(1-xiw))^((1-lambdaw)/lambdaw);
smallh=whalo^(lambdaw/(1-lambdaw))*H;

%match intensity (smallh) to desired target
equ9=smallh-intensity_target;

i=kbar*(1-(1-delta)/(muzplus*mupsi))/Upsilon;

%ybar
ybar=(phalo^(lambdad/(lambdad-1))*epsilon*(kbar/H/(mupsi*muzplus))^alpha)...
            /(1+phalo^(lambdad/(lambdad-1))*(1/mc-phalo^(lambdad/(1-lambdad))))*H;
        
%match i/y to desired target
equ10=i*pinvest/ybar-iy_target;
 
%match xy to desired target
% old incorrect version: equ11=x*px/ybar-xy_target, where
% x=px^(-etaf)*ystar (latter statement true);
equ11=px^(-etaf)*ystar*px*varphi/ybar-xy_target; 

%constructing the matrix to be solved with e.g. fsolve
ff=[equ1
    equ2
    equ3
    equ4
    equ5
    equ6
    equ7
    equ8
    equ9
    %equ10
    equ11];
