File size: 5,644 Bytes
ed7d493
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
clear,clc

%---------------------------------------------------------
% Add folder 'files' to the search path and load the model
%---------------------------------------------------------
addpath('files'); 
load('model')

%----------------------------------------------------------------------------
% Provide nodes and weights for the quadrature that approximates expectations
%----------------------------------------------------------------------------
n_e=1; % number of shocks.
[n_nodes,nodes,weights] = Monomials_2(n_e,eye(n_e)); % this quadrature function was written by Judd, Maliar, Maliar and Valero (2014).
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);