| clear,clc
|
|
|
|
|
|
|
|
|
| addpath('files');
|
| load('model')
|
|
|
|
|
|
|
|
|
| n_e=model.n_e;
|
| [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; PHI=0.1;
|
| params=eval(symparams);
|
|
|
| %------------------
|
| % transition matrix
|
| %------------------
|
|
|
| transition=[.95,.05;
|
| .5,.5];
|
|
|
| %--------------------------------------------------------------------------
|
| % Prepare an initial guess - perturbation solution of RBC without disasters
|
| %--------------------------------------------------------------------------
|
|
|
| fixed_markov_values=0; % start with a fixed value for the disaster shock (disaster shock is 0 in all states)
|
|
|
| % Steady state values
|
|
|
| kss=((1/BETA-1+DELTA)/ALPHA)^(1/(ALPHA-1));
|
| zss=0;
|
| css=kss^ALPHA-DELTA*kss;
|
|
|
| nxss=[log(kss);zss];
|
| nyss=log(css);
|
|
|
| % Cross moments of the shocks
|
|
|
| M=get_moments(nodes,weights,model.order(2));
|
|
|
| % Compute the perturbation solution
|
|
|
| [derivs,stoch_pert,nonstoch_pert,model]=get_pert(model,params,M,nxss,nyss,fixed_markov_values);
|
|
|
| % the variables stoch_pert and nonstoch_pert have two columns, corresponding to the two Markov states.
|
|
|
| %-------------------------------------
|
| % Taylor projection with Markov shocks
|
| %-------------------------------------
|
|
|
| markov_values=[0,1]; % now the values of the Markov shock depend on the Markov state
|
|
|
| x0=nxss; % the approximation point
|
| 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,markov_values,transition,c0,nodes,weights,tolX,tolF,maxiter);
|
|
|
| %-------------------------------------------------------------------------------------
|
| % Compute the residual function and the model variables at point x0 for Markov state s
|
| %-------------------------------------------------------------------------------------
|
|
|
| s=1; % Markov state 1
|
| [R_fun1,g_fun1,Phi_fun1,auxvars1]=residual(coeffs,x0,params,markov_values,transition,c0,nodes,weights,s);
|
| c1=exp(g_fun1); % consumption in normal state
|
| inv1=exp(Phi_fun1(1,1))-exp(x0(1))*(1-DELTA);
|
|
|
|
|
| s=2; % Markov state 2
|
| [R_fun2,g_fun2,Phi_fun2,auxvars2]=residual(coeffs,x0,params,markov_values,transition,c0,nodes,weights,s);
|
| c2=exp(g_fun2); % consumption in normal state
|
| inv2=exp(Phi_fun2(1,1))-exp(x0(1))*(1-DELTA);
|
|
|
| c2/c1-1 % fall in consumption is smaller than fall in output
|
| inv2/inv1-1 % fall in investment is larger than fall in output
|
|
|
|
|
| %-------------------
|
| % Simulate the model
|
| %-------------------
|
|
|
| T=1000;
|
| shocks=randn(1,T+1); % draw shocks
|
|
|
| % simulate a Markov chain from the transition matrix
|
| ushock=rand(1,T+1); % draw shocks from a uniform distribution to determine the Markov state
|
| Markov_state=zeros(1,T+1);
|
| Markov_state(1)=1; % start at state 1
|
| for t=2:T+1
|
| Markov_state(t)=1+(ushock(t)>transition(Markov_state(t-1),1)); % state in period t, given the state in t-1
|
| end
|
|
|
| % 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;
|
|
|
| for t=1:T
|
| xt=x_simul(:,t);
|
| st=Markov_state(t);
|
|
|
| [Rt,yt]=residual(coeffs,xt,params,markov_values,transition,c0,nodes,weights,st);
|
|
|
| epsp=shocks(t+1); % future iid shock
|
| stp=Markov_state(t+1); % future Markov shock
|
|
|
| y_simul(:,t)=yt;
|
| x_simul(:,t+1)=evalPhi(xt,yt,epsp,stp,st,params,markov_values);
|
| R_simul(:,t)=Rt;
|
| end
|
| |