| clear,clc
|
|
|
|
|
|
|
|
|
| addpath('files');
|
| load('model')
|
|
|
|
|
|
|
|
|
| n_e=1;
|
| [n_nodes,nodes,weights] = Monomials_2(n_e,eye(n_e));
|
| nodes=nodes'; % transpose to n_e-by-n_nodes
|
|
|
| %----------------------------------
|
| % Make a vector of parameter values
|
| %----------------------------------
|
| BETA=.96; GAMMA=2; ALPHA=.3; RHO=.8; DELTA=.1; SIGMA=.02;
|
| params=eval(symparams);
|
|
|
| %----------------------------------------------------------------------
|
| % Prepare an initial guess - in this case I use a perturbation solution
|
| %----------------------------------------------------------------------
|
|
|
| % Steady state values
|
|
|
| kss=((1/BETA-1+DELTA)/ALPHA)^(1/(ALPHA-1));
|
| zss=0;
|
| css=kss^ALPHA-DELTA*kss;
|
|
|
| nxss=[kss;zss];
|
| nyss=css;
|
|
|
| % Cross moments of the shocks
|
|
|
| M=get_moments(nodes,weights,model.order(2));
|
|
|
| % Compute the perturbation solution (keep the 4 outputs):
|
|
|
| [derivs,stoch_pert,nonstoch_pert,model]=get_pert(model,params,M,nxss,nyss);
|
|
|
| % Explanation of outputs:
|
| % derivs=structure with the perturbation solution as explained in Levintal
|
| % (2017): "Fifth-Order Perturbation Solution to DSGE Models".
|
| % stoch_pert=the perturbation solution in the form of unique polynomial coefficients.
|
| % nonstoch_pert=same as stoch_pert but without correction for the model volatility (i.e. this is a perturbation solution of a deterministic version of the model)
|
|
|
| %-------------------------------------
|
| % Solve the model by Taylor projection
|
| %-------------------------------------
|
|
|
| x0=nxss; % the approximation point (here we use the steady state, but it could be any arbitrary state)
|
| c0=nxss; % the center of the initial guess
|
|
|
| % tolerance parameters for the Newton solver
|
| tolX=1e-6;
|
| tolF=1e-6;
|
| maxiter=10;
|
|
|
| % model.jacobian='exact'; % this is the default
|
| % model.jacobian='approximate'; % for large models try the approximate jacobian.
|
|
|
| initial_guess=stoch_pert;
|
| [coeffs,model]=tpsolve(initial_guess,x0,model,params,c0,nodes,weights,tolX,tolF,maxiter);
|
|
|
| %------------------------------------------------------------------
|
| % Compute the residual function and the model variables at point x0
|
| %------------------------------------------------------------------
|
|
|
| [R_fun0,g_fun0,Phi_fun0,auxvars0]=residual(coeffs,x0,params,c0,nodes,weights);
|
|
|
| % R_fun0 is the residual function at x0.
|
| % g_fun0 is the control variables at x0, namely, g(x0).
|
| % Phi_fun0 is the function Phi at x0 and each future node, namely, Phi(x0,g(x0),epsp), for each node of the quadrature.
|
| % auxvars0 is the auxiliary functions at x0 and each future node.
|
|
|
| % compute the function g(x) at x0
|
| y0=evalg(x0,coeffs,c0);
|
|
|
| % compute the function Phi(x,y,epsp) at x0, y0 and epsp0
|
| epsp0=.02;
|
| xp0=evalPhi(x0,y0,epsp0,params);
|
|
|
| %---------------------------------
|
| % simulate the model for T periods
|
| %---------------------------------
|
| T=100;
|
| shocks=randn(1,T+1); % draw shocks
|
|
|
| % preallocate
|
| x_simul=zeros(model.n_x,T+1);
|
| y_simul=zeros(model.n_y,T);
|
| R_simul=zeros(model.n_y,T);
|
|
|
| x_simul(:,1)=x0;
|
|
|
| % option=1; % compute only simulated variables
|
| option=2; % compute model residuals
|
|
|
| for t=1:T
|
| xt=x_simul(:,t);
|
| epsp=shocks(t+1);
|
|
|
| % Option 1 - compute only the simulated variables
|
| if option==1
|
| yt=evalg(xt,coeffs,c0);
|
|
|
| y_simul(:,t)=yt;
|
| x_simul(:,t+1)=evalPhi(xt,yt,epsp,params);
|
| else
|
| % Option 2 - compute also model residuals
|
| [Rt,yt]=residual(coeffs,xt,params,c0,nodes,weights);
|
|
|
| y_simul(:,t)=yt;
|
| x_simul(:,t+1)=evalPhi(xt,yt,epsp,params);
|
| R_simul(:,t)=Rt;
|
| end
|
| end
|
|
|
| %-------------------------------------------
|
| % Solve the model again at a different state
|
| %-------------------------------------------
|
| % This is useful when the long run domain of the model is far from the
|
| % initial state, so we need to approximate the solution at the long run state
|
| % (e.g. the risky steady state or the mean of the ergodic distribution)
|
| % rather than the steady state.
|
|
|
| x1=x0*1.1; % take some arbitrary state
|
| [coeffs1,model]=tpsolve(coeffs,x1,model,params,c0,nodes,weights,tolX,tolF,maxiter); % solve at x1
|
|
|
| %-----------------------
|
| % Use a different solver
|
| %-----------------------
|
|
|
| % The function tpsolve uses the Newton method for up to maxiter iterations. If it fails, it
|
| % switches automatically to fsolve for another maxiter iterations. You can
|
| % control the parameters of the second solver by optimoptions. The
|
| % supported solvers are fsolve and lsqnonlin.
|
|
|
| % For example, do one Newton iteration and switch to lsqnonlin:
|
|
|
| x2=x1*1.1;
|
| maxiter=1; % one Newton iteration
|
| OPTIONS = optimoptions('lsqnonlin','TolX',tolX,'TolF',tolF,'MaxIter',10,'Display','iter-detailed'); % 10 more iterations by lsqnonlin
|
|
|
| [coeffs2,model]=tpsolve(coeffs,x2,model,params,c0,nodes,weights,tolX,tolF,maxiter,OPTIONS);
|
|
|
|
|
| % or switch to fsolve:
|
|
|
| maxiter=1; % one Newton iteration
|
| OPTIONS = optimoptions('fsolve','TolX',tolX,'TolF',tolF,'MaxIter',10,'Display','iter-detailed'); % 10 more iterations by fsolve
|
|
|
| [coeffs3,model]=tpsolve(coeffs,x2,model,params,c0,nodes,weights,tolX,tolF,maxiter,OPTIONS);
|
|
|
|
|
|
|
|
|
|
|
| |