REPRO-Bench / 105 /replication_package /UNIT_IES /solve_and_simulate.m
% This file performs the following:
% 1. Solve the model by Taylor projection at the initial state.
% 2. Simulate the model without realized disasters.
%% make initial guess for a deterministic version of the model
% in a deterministic economy, the following variables are constant:
x1 = 1; % agents invests only in equity
x2 = 1;
tilp = 1/RHO; % price/earning ratio
hatyp = exp(G-meanB*P); % average growth
haty = hatyp;
rep = (1+tilp)/tilp*hatyp; % asset return
logq = log(1/rep); % price of bond
c1 = RHO/(1+RHO); % consumption/wealth ratio
c2 = c1;
logu1 = (RHO*log(c1)+log(1-c1)+log(rep))/RHO;
u1 = exp(logu1);
logu2 = (RHO*log(c2)+log(1-c2)+log(rep))/RHO;
u2 = exp(logu2);
f1 = (rep*u1);
f2 = (rep*u2);
k1 = MU;
tila1 = k1*(1+tilp);
state0 = tila1;
c0 = state0;
derivs0 = [f1;f2;x1;x2;logq];
derivs1 = zeros(model.n_f,model.n_x);
derivs2 = zeros(model.n_f,model.n_x^2);
derivs3 = zeros(model.n_f,model.n_x^3);
derivs4 = zeros(model.n_f,model.n_x^4);
if order==1
[ initial_guess ] = derivs2coeffs( model,derivs0,derivs1 );
elseif order==2
[ initial_guess ] = derivs2coeffs( model,derivs0,derivs1,derivs2);
elseif order==3
[ initial_guess ] = derivs2coeffs( model,derivs0,derivs1,derivs2,derivs3 );
elseif order==4
[ initial_guess ] = derivs2coeffs( model,derivs0,derivs1,derivs2,derivs3,derivs4 );
end
%% solve the model
[coeffs,model] = tpsolve(initial_guess,state0,model,params,c0,nodes,weights,tolX,tolF,maxiter,OPTIONS);
%% simulate the model
solve = 1;
stop = 0;
t = 0;
xt = state0;
while stop==0
t = t+1;
% evaluate the previous solution at the new point xt
[R,g,nPhi] = residual(coeffs,xt,params,c0,nodes,weights);
% if residuals are too large solve again
if norm(R(:))>testF && solve==1
t
[coeffs] = tpsolve(coeffs,xt,model,params,c0,nodes,weights,tolX,tolF,maxiter,OPTIONS); % solve
% evaluate the new solution
[R,g,nPhi] = residual(coeffs,xt,params,c0,nodes,weights);
end
newxt = nPhi(:,disaster(t+1)); % new state
if t>=10 % after 10 periods start checking for convergence
if max(abs(newxt-xt))<1e-7
stop = 1;
state0 = xt;
coeffs0 = coeffs;
end
end
xt = newxt;
end