clear all
close all

global beta delta alpha1 alpha2 alpha3 Hs He bank rp eta mu sigmacs Ls nu 

% set parameters
beta = 0.99;
delta = 0.02;
alpha1 = 0.36;
alpha2 = 0.6399;
alpha3 = 0.0001;
bank = 0.0292;
Ls = 0.3;
rp = 0.0312; % risk premium, need to put at quarterly frequency
%rp = (1.0187)^(1/4)-1;
eta = 0.1; % share
He = eta;
Hs = (1-eta)*Ls;
mu = 0.25;
sigmacs = 2;
nu =2;

% guess omegab and sigma
omegabb = 0.6039;
sigmaa = 0.2070;

guess = [omegabb;sigmaa]
[ss,fval] = fsolve(@CF_Find,guess);

omegab = ss(1);
sigma = ss(2);


M = - 0.5*sigma^2;
PHIs = normcdf((log(omegab) - M)/sigma);
phis = normpdf((log(omegab) - M)/sigma)/(omegab*sigma);
gs = normcdf((log(omegab)-M-sigma^2)/sigma) - PHIs*mu + (1-PHIs)*omegab;
fs = 1-mu*PHIs-gs;
qs = (1-PHIs*mu-phis*mu*fs/(1-PHIs))^(-1);
gamma = (1-qs*gs)/(qs*fs);
rs = qs*(1-beta*(1-delta))/beta;
Ks = (rs/(alpha1*Hs^(alpha2)*He^(alpha3)))^(1/(alpha1-1));
Ys = Ks^(alpha1)*Hs^(alpha2)*He^(alpha3);
ws = alpha2*Ks^(alpha1)*Hs^(alpha2-1)*He^(alpha3);
xs = (1-alpha1-alpha2)*Ks^(alpha1)*Hs^(alpha2)*He^(alpha3-1);
Is = delta*Ks/((1-PHIs*mu));
is = Is/eta;
ns = is*(1-qs*gs);
kes = (ns-xs)/(rs+qs*(1-delta));
ces = qs*fs*is - qs*kes;
cs = (1/(1-eta))*(Ys - eta*ces - eta*is);
chi = cs^(sigmacs) * ws /(Ls^(1/nu));
rks = omegab*(is/(is-ns))-1;
kcs = (1/(1-eta))*(Ks - eta*kes);

% targets
bankr = PHIs - bank;
spread = qs*(1+rks)-1 - rp;

% stochastic process
rho = 0.95;
st = 0.9;
ss = 1;
rhoepsilon = 0.99;

save params_cf M sigma omegab mu nu beta delta eta alpha1 alpha2 alpha3 Hs Ls gamma rho st ss rhoepsilon

dynare CF1997BE.mod noclearall nolog

