% --------------------------------------
%  Parameters' Declaration
% --------------------------------------

parameters  bc sigmac betaa alpha Omega alphap tetap mup gammap muW mu varrho rhoi ap ay eps_tax delta d kapai sigmal sigmaWbar
        eps_taxw muww alphaw tetaw gammaw Q1wbar Q2wbar Wstarbar lomebar muGpbar pi SEome
        lambdabar cbar Rbar pipbar rrealbar lhbar wbar piwbar ybar lbar kbar sbar webar zbar Q1bar Q2bar Pstarbar Fomebar abar rhoA sigmaA
        Fnomebar omebar SIGbar muGbar SIGpbar ffomebar deltaubar deltauubar adjibar  adjiibar nwbar ibar ubar gbar chilbar rhoChil rhoG
        ktildebar bbar xxbar qbar rkbar rrbar LAMbar vbar cebar  gammabar deltatild rhoGamma rhoPref rhoEm rhoTechno prefbar embar technobar
        sigmaGamma sigmaPref sigmaTechno sigmaEm sigmaChil sigmaG sigmaSIG rhoSIG;



% ---- Preference and production parameters
betaa   = 0.99;     % discount factor
bc      = 0;     % habits
sigmac  = 1;        % Intert. elasticity of substitution
sigmal  = 1;        % 
alpha   = 0.35;     % capital share
Omega   = 0.9846;   % Proportion of Houshold labor on aggregate labor composite, Omega = 1 - .01/(1-alpha), where .01 is entrepreneurs' labor income share
delta   = 0.025;
tau     = 0;        % tax (does not appear in .mod)
kapai   = 5.86;     % investment adjustment cost

alphap  = 0.67;     % calvo price
gammap  = 0.75;     % indexation price
tetap   = 11;       % elasticity of subst. demand
mup     = tetap/(tetap-1);  % mark up
SNbar    = 1/mup;

alphaw   = 0.75;
gammaw   = 0.75;
tetaw    = 11;
muww      = tetaw/(tetaw-1);

kappap  = (1-alphap)*(1-alphap*betaa)/(alphap);                          
kappaw  = ((1-alphaw)*(1-betaa*alphaw)/(alphaw*(1+sigmal*tetaw)));

xbar    = 2;            % = ktildebar/nbar
rrbar   = 1.02^(1/4);   % = rkbar/rbar
Rbar    = 1/betaa;       % risk free interest rate
rkbar   = rrbar*Rbar;   % return of capital

gammabar = 0.9728;  % entrepr default proba -- BGG
varrho   = 0.01;    % (1-varrho) = tranfers from entrepreneur to HH

Eome     = 1;       % mean of omegabar -- BGG
omebar0  = 1;       % Initial value of omegabar 
SEome0   = 0.28;    % s.e of omegabar -- BGG
gamma0   = 0.9;
mu0      = 0.1;

paramm0  = [omebar0 SEome0 gamma0 mu0];
paramc   = [tau betaa delta alpha Omega tetap xbar rrbar Eome];

options  = optimset('Display','iter');
[x,Fval,exitflag] = fsolve('funx2',paramm0,options,paramc);      % what value of omebar such that: logncdf(omebar,muW,sigmaW) = PHI;
omebar   = x(1);   
SEome    = x(2); 
gammabar = x(3);
mu       = x(4);



% ---------- Compute SIGMA(omebar) and G(omebar)
% ----------------------------------------------
sigmaWbar  = sqrt(log(1 + (SEome^2)/(Eome^2)));  
muW        = log(Eome) - 0.5*sigmaWbar^2;
tbar       = (muW - log(omebar))/sigmaWbar;
lomebar    = log(omebar);
Fomebar   = normcdf(lomebar,muW,sigmaWbar);   % F(omebar)
aa = exp(-0.5 * ((log(omebar) - muW)/sigmaWbar)^2) / (omebar * sqrt(2*3.14) * sigmaWbar);
ffomebar  = aa;

//Fomebar    = logncdf(omebar,muW,sigmaWbar);   % F(omebar)
//ffomebar    = lognpdf(omebar,muW,sigmaWbar);   % f(omebar)
ffomep    = -(ffomebar/omebar)*(1 - (muW-log(omebar))/(sigmaWbar*sigmaWbar));  
Fnomebar  = Eome - exp(muW+sigmaWbar*sigmaWbar/2)*normcdf(tbar+sigmaWbar,0,1); 
SIGbar    = omebar*(1-Fomebar) + Fnomebar;      % SIGMA(omebar)
SIGpbar   = 1 - Fomebar;                     % SIGMA'(omebar)
muGbar    = mu*Fnomebar;                     % mu*G(omebar)
muGpbar   = mu*omebar*ffomebar;               % mu*G'(omebar)
SIGppbar  = - ffomebar;                       % SIGMA''(omebar)
muGbar    = mu*Fnomebar;                     % mu*G(omebar)
muGpbar   = mu*omebar*ffomebar;               % mu*G'(omebar)
muGppbar  = mu*(ffomebar + omebar*ffomep);     % mu*G''(omebar)






% ---------------------------------------------------------
% ---------- Numerical computations of sigome derivatices 
% ---------------------------------------------------------
vareps   = 1e-15;
SEome1   = SEome + vareps;
% ---------- Compute derivative of SIGMA(omebar) and G(omebar)
sigmaW1  = sqrt(log(1 + (SEome1^2)/(Eome^2)));  
muW1     = log(Eome) - 0.5*sigmaWbar^2;
sigmaW   = sigmaW1;
muW      = muW1;
tbar1    = (muW1 - log(omebar))/sigmaW1;
//Fome1    = normcdf(lomebar,muW1,sigmaW1);   % F(omebar)
//fome1    = normcdf(lomebar,muW1,sigmaW1);   % f(omebar)
//fomep1   = -(fome1/omebar)*(1 - (muW1-log(omebar))/(sigmaW1*sigmaW1));   % f'(omebar)

Fome1    = logncdf(omebar,muW1,sigmaW1);   % F(omebar)
fome1    = lognpdf(omebar,muW1,sigmaW1);   % f(omebar)
fomep1   = -(fome1/omebar)*(1 - (muW1-log(omebar))/(sigmaW1*sigmaW1));   % f'(omebar)

Fnome1   = Eome - exp(muW1+sigmaW1*sigmaW1/2)*normcdf(tbar1+sigmaW1,0,1); % Fnormal(omebar) % Gamma
SIG1     = omebar*(1-Fome1) + Fnome1;      % SIGMA(omebar)
SIGp1    = 1 - Fome1;                      % SIGMA'(omebar)
SIGpp1   = - fome1;                        % SIGMA''(omebar)
muG1     = mu*Fnome1;                      % mu*G(omebar)
muGp1    = mu*omebar*fome1;                % mu*G'(omebar)
muGpp1   = mu*(fome1 + omebar*fomep1);     % mu*G''(omebar)
% ----------------------------------------------------------
SIGsbar     = (SIG1-SIGbar)/vareps;
SIGpsbar    = (SIGp1-SIGpbar)/vareps;

muGsbar     = (muG1-muGbar)/vareps;
muGpsbar    = (muGp1-muGpbar)/vareps;
% ----------------------------------------------------------


% ----------  Steady State variables 
%------------------------------------
% ------- Ratios
GbarY      = 0.19;         
eps_tax    = 1/(tetap-1);
eps_taxw   = 1/(tetaw-1);
Pstarbar   = ((1-alphap)/(1-alphap))^(1/(1-tetap));
sbar       = (1+eps_tax)*Pstarbar/mup;       % rmc

zbar    = (rkbar - (1-delta));  % rental price of capital
YbarK   = zbar/(sbar*alpha);
IbarY   = delta*(1/YbarK);
CebarK  = varrho*(1-gammabar)*(rkbar - Rbar*(1-1/xbar) - muGbar*rkbar);
CebarY  = CebarK/YbarK;

CbarY   = 1 - IbarY - CebarY - GbarY  - muGbar*rkbar*(1/YbarK) ;
WebarK  = sbar*(1-Omega)*(1-alpha)*YbarK;
VbarN   = rkbar*xbar - Rbar*(xbar - 1) - muGbar*rkbar*xbar;
CebarN  = (1-gammabar)*varrho*VbarN;
LbarK   = (YbarK)^(1/(1-alpha));


% ------- Variables
ybar       = 1;
piwbar     = 1;
pipbar     = 1;
rrealbar   = Rbar;
ubar       = 0;
qbar       = 1;
kbar       = 1/(YbarK*(1/ybar));
ktildebar  = kbar;
ibar       = IbarY*ybar;
cebar      = CebarY*ybar;
cbar       = CbarY*ybar;
gbar       = GbarY*ybar;
nwbar      = kbar/xbar; 
vbar       = VbarN*nwbar;
lambdabar  = (1-betaa*bc)*(cbar*(1-bc))^(-1/sigmac);
lbar       = LbarK*kbar;
lhbar      = lbar^(1/Omega);
webar      = WebarK*kbar;
wbar       = sbar*Omega*(1-alpha)*ybar/lhbar;
Wstarbar   = wbar;
% chilbar    = wbar*lambdabar*lhbar^(-sigmal)
chilbar    = (1+eps_taxw)*Wstarbar*lambdabar*lhbar^(-sigmal)/muww;
bbar       = ktildebar - nwbar;
xxbar      = ktildebar/nwbar;
LAMbar     = SIGpbar/(SIGpbar - muGpbar);
abar       = ((rkbar/rrealbar)*xxbar*(SIGbar - muGbar))/(xxbar - 1);
deltatild  = zbar;
d          = deltatild/delta;
Q1bar      = (ybar*sbar)/(1-alphap*betaa);
Q2bar      = (1+eps_tax)*ybar/(1-alphap*betaa);
Q1wbar     = (chilbar*lhbar^(sigmal))/(1-alphaw*betaa);
Q2wbar     = ((1+eps_taxw)*lambdabar)/(1-alphaw*betaa);
deltaubar  = delta;
deltauubar = 0;%deltatild;
adjibar    = 0;
adjiibar   = 0;
prefbar    = 1;
embar      = 1;
technobar  = 1;


% ----- Monetary policy rule and shocks
% --------------------------------------
rhoi     = 0.878;     % Monetary policy rule
ap       = 1.852;     % CMR(2009) - US 
ay       = 0.313/4;   % CMR(2009) - US
aq       = 0;


rhoGamma  = 0.561;   %% CMR(2009) - US
rhoPref   = 0.903;
rhoTechno = 0.945;
rhoEm     = 0;
rhoChil   = 0.2;
rhoG      = 0.6;
rhoSIG    = 0.98;
rhoA      = 0.5;

sigmaGamma  = 0.01^2;  
sigmaPref   = 0.01^2;
sigmaTechno = 0.01^2;
sigmaEm     = 0.01^2;
sigmaChil   = 0.01^2;
sigmaG      = 0.01^2;
sigmaSIG    = 0.01^2;
sigmaA      = 0.01^2;


save_params_and_steady_state(ParamMatrix);
