{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Solving a New Keynesian model with Matlab\n", "\n", "This notebook is part of a computational appendix that accompanies the paper.\n", "\n", "> MATLAB, Python, Julia: What to Choose in Economics? \n", ">> Coleman, Lyon, Maliar, and Maliar (2017)\n", "\n", "In order to run the codes in this notebook you will need to install and configure Matlab and the a jupyter Matlab kernel. We assume you already have Matlab installed and show how to install the jupyter matlab kernel in a few steps:\n", "\n", "1. Install Python version 3.5. We recommend following the instructions on [quantecon](https://lectures.quantecon.org/py/getting_started.html) and using the Anaconda Python distribution. Make sure you don't get a python version above version 3.5.\n", "2. Install the [MATLAB Engine API for Python](https://www.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html). You can follow the official instructions from MathWorks at the link in the previous sentence.\n", "3. Install the `imatlab` kernel by doing the following from the command prompt (or terminal prompt for OSX/Linux userse):\n", "```shell\n", "python -m pip install matlab_kernel\n", "python -m matlab_kernel install\n", "```" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Numerical Tools\n", "\n", "\n", "In order to implement our routines, we need a few functions defining numerical tools.\n", "\n", "Do to limitations in the [Matlab Juptyer kernel](https://github.com/Calysto/matlab_kernel/issues/35), we cannot define functions in the notebook itself. So, we will download the file we need from online, save them to the same directory as this notebook, and then add this directory to the Matlab path.\n", "\n", "So that we know what these files are doing, we will print the text of these files here in the notebook." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "ans =\n", "\n", " 2\n", "\n" ] } ], "source": [ "1+1" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "mon1_txt = fileread(websave('Monomials_1.m', 'https://s3.amazonaws.com/clmm-resources/mfiles/Monomials_1.m'));\n", "mon2_txt = fileread(websave('Monomials_2.m', 'https://s3.amazonaws.com/clmm-resources/mfiles/Monomials_2.m'));\n", "poly_txt = fileread(websave('Ord_Polynomial_N.m', 'https://s3.amazonaws.com/clmm-resources/mfiles/Ord_Polynomial_N.m'));" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "mon1_txt =\n", "\n", " '% Monomials_1.m is a routine that constructs integration nodes and weights \n", " % under N-dimensional monomial (non-product) integration rule with 2N nodes;\n", " % see Judd, Maliar and Maliar, (2010), \"A Cluster-Grid Projection Method:\n", " % Solving Problems with High Dimensionality\", NBER Working Paper 15965\n", " % (henceforth, JMM, 2010).\n", " % -------------------------------------------------------------------------\n", " % Inputs: \"N\" is the number of random variables; N>=1;\n", " % \"vcv\" is the variance-covariance matrix; N-by-N\n", " \n", " % Outputs: \"n_nodes\" is the total number of integration nodes; 2*N;\n", " % \"epsi_nodes\" are the integration nodes; n_nodes-by-N;\n", " % \"weight_nodes\" are the integration weights; n_nodes-by-1\n", " % -------------------------------------------------------------------------\n", " % Copyright � 2011 by Lilia Maliar and Serguei Maliar. All rights reserved.\n", " % The code may be used, modified and redistributed under the terms provided\n", " % in the file \"License_Agreement.txt\".\n", " % -------------------------------------------------------------------------\n", " \n", " function [n_nodes,epsi_nodes,weight_nodes] = Monomials_1(N,vcv)\n", " \n", " n_nodes = 2*N; % Total number of integration nodes\n", " \n", " % 1. N-dimensional integration nodes for N uncorrelated random variables with\n", " % zero mean and unit variance\n", " % ---------------------------------------------------------------------------\n", " z1 = zeros(n_nodes,N); % A supplementary matrix for integration nodes;\n", " % n_nodes-by-N\n", " \n", " for i = 1:N % In each node, random variable i takes value either\n", " % 1 or -1, and all other variables take value 0\n", " z1(2*(i-1)+1:2*i,i) = [1; -1];\n", " end % For example, for N = 2, z1 = [1 0; -1 0; 0 1; 0 -1]\n", " \n", " % z = z1*sqrt(N); % Integration nodes\n", " \n", " % 2. N-dimensional integration nodes and weights for N correlated random\n", " % variables with zero mean and variance-covaraince matrix vcv\n", " % ----------------------------------------------------------------------\n", " sqrt_vcv = chol(vcv); % Cholesky decomposition of the variance-covariance\n", " % matrix\n", " \n", " R = sqrt(N)*sqrt_vcv; % Variable R; see condition (20) in JMM (2010)\n", " \n", " epsi_nodes = z1*R; % Integration nodes; see condition (20) in JMM (2010);\n", " % n_nodes-by-N\n", " \n", " % 3. Integration weights\n", " %-----------------------\n", " weight_nodes = ones(n_nodes,1)/n_nodes;\n", " % Integration weights are equal for all integration\n", " % nodes; n_nodes-by-1; the weights are the same for\n", " % the cases of correlated and uncorrelated random\n", " % variables\n", " '\n", "\n" ] } ], "source": [ "mon1_txt" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "mon2_txt =\n", "\n", " '% Monomials_2.m is a routine that constructs integration nodes and weights \n", " % under N-dimensional monomial (non-product) integration rule with 2N^2+1\n", " % nodes; see Judd, Maliar and Maliar, (2010), \"A Cluster-Grid Projection\n", " % Method: Solving Problems with High Dimensionality\", NBER Working Paper\n", " % 15965 (henceforth, JMM, 2010).\n", " % -------------------------------------------------------------------------\n", " % Inputs: \"N\" is the number of random variables; N>=1;\n", " % \"vcv\" is the variance-covariance matrix; N-by-N;\n", " \n", " % Outputs: \"n_nodes\" is the total number of integration nodes; 2*N^2+1;\n", " % \"epsi_nodes\" are the integration nodes; n_nodes-by-N;\n", " % \"weight_nodes\" are the integration weights; n_nodes-by-1\n", " % -------------------------------------------------------------------------\n", " % Copyright � 2011 by Lilia Maliar and Serguei Maliar. All rights reserved.\n", " % The code may be used, modified and redistributed under the terms provided\n", " % in the file \"License_Agreement.txt\".\n", " % -------------------------------------------------------------------------\n", " \n", " \n", " function [n_nodes,epsi_nodes,weight_nodes] = Monomials_2(N,vcv)\n", " \n", " n_nodes = 2*N^2+1; % Total number of integration nodes\n", " \n", " % 1. N-dimensional integration nodes for N uncorrelated random variables with\n", " % zero mean and unit variance\n", " % ---------------------------------------------------------------------------\n", " \n", " % 1.1 The origin point\n", " % --------------------\n", " z0 = zeros(1,N); % A supplementary matrix for integration nodes: the\n", " % origin point\n", " \n", " % 1.2 Deviations in one dimension\n", " % -------------------------------\n", " z1 = zeros(2*N,N); % A supplementary matrix for integration nodes;\n", " % n_nodes-by-N\n", " \n", " for i = 1:N % In each node, random variable i takes value either\n", " % 1 or -1, and all other variables take value 0\n", " z1(2*(i-1)+1:2*i,i) = [1; -1];\n", " end % For example, for N = 2, z1 = [1 0; -1 0; 0 1; 0 -1]\n", " \n", " % 1.3 Deviations in two dimensions\n", " % --------------------------------\n", " z2 = zeros(2*N*(N-1),N); % A supplementary matrix for integration nodes;\n", " % 2N(N-1)-by-N\n", " \n", " i=0; % In each node, a pair of random variables (p,q)\n", " % takes either values (1,1) or (1,-1) or (-1,1) or\n", " % (-1,-1), and all other variables take value 0\n", " for p = 1:N-1\n", " for q = p+1:N\n", " i=i+1;\n", " z2(4*(i-1)+1:4*i,p) = [1;-1;1;-1];\n", " z2(4*(i-1)+1:4*i,q) = [1;1;-1;-1];\n", " end\n", " end % For example, for N = 2, z2 = [1 1;1 -1;-1 1;-1 1]\n", " \n", " % z = [z0;z1*sqrt(N+2);z2*sqrt((N+2)/2)]; % Integration nodes\n", " \n", " % 2. N-dimensional integration nodes and weights for N correlated random\n", " % variables with zero mean and variance-covaraince matrix vcv\n", " % ----------------------------------------------------------------------\n", " sqrt_vcv = chol(vcv); % Cholesky decomposition of the variance-\n", " % covariance matrix\n", " \n", " R = sqrt(N+2)*sqrt_vcv; % Variable R; see condition (21) in JMM\n", " % (2010)\n", " \n", " S = sqrt((N+2)/2)* sqrt_vcv; % Variable S; see condition (21) in JMM\n", " % (2010)\n", " \n", " epsi_nodes = [z0;z1*R;z2*S];\n", " % Integration nodes; see condition (21)\n", " % in JMM (2010); n_nodes-by-N\n", " \n", " % 3. Integration weights\n", " %-----------------------\n", " weight_nodes = [2/(N+2)*ones(size(z0,1),1);(4-N)/2/(N+2)^2*ones(size(z1,1),1);1/(N+2)^2*ones(size(z2,1),1)];\n", " % See condition (21) in JMM (2010);\n", " % n_nodes-by-1; the weights are the same\n", " % for the cases of correlated and\n", " % uncorrelated random variables\n", " '\n", "\n" ] } ], "source": [ "mon2_txt" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "poly_txt =\n", "\n", " '% Ord_Polynomial_N.m is a routine that constructs the basis functions of \n", " % complete ordinary polynomial of the degrees from one to five for the\n", " % multi-dimensional case; see \"Numerically Stable and Accurate Stochastic\n", " % Simulation Approaches for Solving Dynamic Economic Models\" by Kenneth L.\n", " % Judd, Lilia Maliar and Serguei Maliar, (2011), Quantitative Economics 2/2,\n", " % 173�210 (henceforth, JMM, 2011).\n", " %\n", " % This version: July 14, 2011. First version: August 27, 2009.\n", " % -------------------------------------------------------------------------\n", " % Inputs: \"z\" is the data points on which the polynomial basis functions\n", " % must be constructed; n_rows-by-dimen;\n", " % \"D\" is the degree of the polynomial whose basis functions must\n", " % be constructed; (can be 1,2,3,4 or 5)\n", " %\n", " % Output: \"basis_fs\" is the matrix of basis functions of a complete\n", " % polynomial of the given degree\n", " % -------------------------------------------------------------------------\n", " % Copyright � 2011 by Lilia Maliar and Serguei Maliar. All rights reserved.\n", " % The code may be used, modified and redistributed under the terms provided\n", " % in the file \"License_Agreement.pdf\".\n", " % -------------------------------------------------------------------------\n", " \n", " function basis_fs = Ord_Polynomial_N(z,D)\n", " \n", " \n", " % A polynomial is given by the sum of polynomial basis functions, phi(i),\n", " % multiplied by the coefficients; see condition (13) in JMM (2011). By\n", " % convention, the first basis function is one.\n", " \n", " [n_rows,dimen] = size(z); % Compute the number of rows, n_rows, and the\n", " % number of variables (columns), dimen, in the\n", " % data z on which the polynomial basis functions\n", " % must be constructed\n", " \n", " % 1. The matrix of the basis functions of the first-degree polynomial\n", " % (the default option)\n", " % -------------------------------------------------------------------\n", " basis_fs = [ones(n_rows,1) z]; % The matrix includes a column of ones\n", " % (the first basis function is one for\n", " % n_rows points) and linear basis\n", " % functions\n", " i = dimen+1; % Index number of a polynomial basis function; the first\n", " % basis function (equal to one) and linear basis functions\n", " % are indexed from 1 to dimen+1, and subsequent polynomial\n", " % basis functions will be indexed from dimen+2 and on\n", " \n", " % 2. The matrix of the basis functions of the second-degree polynomial\n", " % --------------------------------------------------------------------\n", " if D == 2\n", " \n", " % Version one (not vectorized):\n", " for j1 = 1:dimen\n", " for j2 = j1:dimen\n", " i = i+1;\n", " basis_fs(:,i) = z(:,j1).*z(:,j2);\n", " end\n", " end\n", " \n", " % Version 2 (vectorized): Note that this version works only for a second-degree\n", " % polynomial in which all state variables take non-zero values\n", " % for r = 1:n_rows\n", " % basis_fs(r,2+dimen:1+dimen+dimen*(dimen+1)/2) = [nonzeros(tril(z(r,:)'*z(r,:)))'];\n", " % Compute linear and quadratic polynomial basis functions for\n", " % each row r; \"tril\" extracts a lower triangular part of z'z\n", " % (note that matrix z'z is symmetric so that an upper triangular\n", " % part is redundant); \"nonzeros\" forms a column vector by\n", " % stacking the columns of the original matrix one after another\n", " % and by eliminating zero terms\n", " % end\n", " \n", " % 3. The matrix of the basis functions of the third-degree polynomial\n", " % -------------------------------------------------------------------\n", " elseif D == 3\n", " \n", " for j1 = 1:dimen\n", " for j2 = j1:dimen\n", " i = i+1;\n", " basis_fs(:,i) = z(:,j1).*z(:,j2);\n", " for j3 = j2:dimen\n", " i = i+1;\n", " basis_fs(:,i) = z(:,j1).*z(:,j2).*z(:,j3);\n", " end\n", " end\n", " end\n", " \n", " % 4. The matrix of the basis functions of the fourth-degree polynomial\n", " % -------------------------------------------------------------------\n", " elseif D == 4\n", " \n", " for j1 = 1:dimen\n", " for j2 = j1:dimen\n", " i = i+1;\n", " basis_fs(:,i) = z(:,j1).*z(:,j2);\n", " for j3 = j2:dimen\n", " i = i+1;\n", " basis_fs(:,i) = z(:,j1).*z(:,j2).*z(:,j3);\n", " for j4 = j3:dimen\n", " i = i+1;\n", " basis_fs(:,i) = z(:,j1).*z(:,j2).*z(:,j3).*z(:,j4);\n", " end\n", " end\n", " end\n", " end\n", " \n", " % 5. The matrix of the basis functions of the fifth-degree polynomial\n", " % -------------------------------------------------------------------\n", " \n", " elseif D == 5\n", " \n", " for j1 = 1:dimen\n", " for j2 = j1:dimen\n", " i = i+1;\n", " basis_fs(:,i) = z(:,j1).*z(:,j2);\n", " for j3 = j2:dimen\n", " i = i+1;\n", " basis_fs(:,i) = z(:,j1).*z(:,j2).*z(:,j3);\n", " for j4 = j3:dimen\n", " i = i+1;\n", " basis_fs(:,i) = z(:,j1).*z(:,j2).*z(:,j3).*z(:,j4);\n", " for j5 = j4:dimen\n", " i = i+1;\n", " basis_fs(:,i) = z(:,j1).*z(:,j2).*z(:,j3).*z(:,j4).*z(:,j5);\n", " end\n", " end\n", " end\n", " end\n", " end\n", " \n", " end % end of \"if\"/\"elseif\"\n", " '\n", "\n" ] } ], "source": [ "poly_txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accuracy and Simulation\n", "\n", "Before proceeding to the main code, we will also need to have routines for simulating and checking the accuracy of our solution to the model.\n", "\n", "We also download these from online as follows:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "nkacc_txt = fileread(websave('NK_accuracy.m', 'https://s3.amazonaws.com/clmm-resources/mfiles/NK_accuracy.m'));\n", "nksim_txt = fileread(websave('NK_simulation.m', 'https://s3.amazonaws.com/clmm-resources/mfiles/NK_simulation.m'));" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "nkacc_txt =\n", "\n", " '% NK_accuracy.m is a routine for evaluating accuracy of the solutions \n", " % to the new Keynesian model considerd in the article \"Merging Simulation\n", " % and Projection Approaches to Solve High-Dimensional Problems with an\n", " % application to a New Keynesian Model\" by Lilia Maliar and Serguei Maliar,\n", " % Quantitative Economics 6, 1-47 (2015) (henceforth, MM, 2015).\n", " % -------------------------------------------------------------------------\n", " % Inputs: \"nua\", \"nuL\", \"nuR\", \"nuG\", \"nuB\", \"nuu\" are the time series\n", " % of shocks generated with the innovations for the test;\n", " % \"delta\", \"L\", \"Y\", \"Yn\", \"pie\", \"S\", \"F\", \"C\" are the time\n", " % series solution generated with the innovations for test;\n", " % \"rho_nua\", \"rho_nuL\", \"rho_nuR\", \"rho_nuu\", \"rho_nuB\", \"rho_nuG\"\n", " % are the parameters of the laws of motion for shocks;\n", " % \"mu\", \"gam\", \"epsil\", \"vartheta\", \"beta\", \"A\", \"tau\", \"rho\",\n", " % \"vcv\", \"beta\", \"phi_y\", \"phi_pie\", \"theta\", \"piestar\" and \"Gbar\"\n", " % are the parameters of the model;\n", " % \"vk_2d\" is the matrix of coefficients of the GSSA solution;\n", " % \"discard\" is the number of data points to discard;\n", " % \"Degree\" is the degree of polynomial approximation\n", " %\n", " % Output: \"Residuals_mean\" and \"Residuals_max\" are, respectively, the mean\n", " % and maximum absolute residuals across all points and all equi-\n", " % librium conditions; \"Residuals_max_E\" is the maximum absolute\n", " % residuals across all points, disaggregated by optimality conditions;\n", " % \"Residuals\" are absolute residuals disaggregated by the equi-\n", " % librium conditions\n", " % -------------------------------------------------------------------------\n", " % Copyright � 2013-2016 by Lilia Maliar and Serguei Maliar. All rights reserved.\n", " % The code may be used, modified and redistributed under the terms provided\n", " % in the file \"License_Agreement.txt\".\n", " % -------------------------------------------------------------------------\n", " \n", " function [Residuals_mean, Residuals_max, Residuals_max_E, Residuals] = NK_accuracy(nua,nuL,nuR,nuG,nuB,nuu,R,delta,L,Y,Yn,pie,S,F,C,rho_nua,rho_nuL,rho_nuR,rho_nuu,rho_nuB,rho_nuG,gam,vartheta,epsil,beta,phi_y,phi_pie,mu,theta,piestar,vcv,discard,vk_2d,Gbar,zlb,Degree)\n", " \n", " tic % Start counting time for running the test\n", " \n", " [T] = size(nua,1); % Infer the number of points on which accuracy is\n", " % evaluated\n", " Residuals = zeros(T,6); % Allocate memory to the matrix of residuals; T-by-6\n", " \n", " % Integration method for evaluating accuracy\n", " % ------------------------------------------\n", " [n_nodes,epsi_nodes,weight_nodes] = Monomials_2(6,vcv);\n", " % Monomial integration rule with 2N^2+1 nodes\n", " \n", " % Compute variables on the given set of points\n", " %---------------------------------------------\n", " \n", " for t = 1:T; % For each given point,\n", " t;\n", " \n", " % Take the corresponding value for shocks at t\n", " %---------------------------------------------\n", " nuR0 = nuR(t,1); % nuR(t)\n", " nua0 = nua(t,1); % nua(t)\n", " nuL0 = nuL(t,1); % nuL(t)\n", " nuu0 = nuu(t,1); % nuu(t)\n", " nuB0 = nuB(t,1); % nuB(t)\n", " nuG0 = nuG(t,1); % nuG(t)\n", " \n", " % Compute shocks at t+1 in all future nodes using their laws of motion\n", " %---------------------------------------------------------------------\n", " % Note that we do not premultiply by standard deviations as epsi_nodes\n", " % already include them\n", " nuR1(1:n_nodes,1) = (ones(n_nodes,1)*nuR0)*rho_nuR + epsi_nodes(:,1);\n", " % nuR(t+1); n_nodes-by-1\n", " nua1(1:n_nodes,1) = (ones(n_nodes,1)*nua0)*rho_nua + epsi_nodes(:,2);\n", " % nua(t+1); n_nodes-by-1\n", " nuL1(1:n_nodes,1) = (ones(n_nodes,1)*nuL0)*rho_nuL + epsi_nodes(:,3);\n", " % nuL(t+1); n_nodes-by-1\n", " nuu1(1:n_nodes,1) = (ones(n_nodes,1)*nuu0)*rho_nuu + epsi_nodes(:,4);\n", " % nuu(t+1); n_nodes-by-1\n", " nuB1(1:n_nodes,1) = (ones(n_nodes,1)*nuB0)*rho_nuB + epsi_nodes(:,5);\n", " % nuB(t+1); n_nodes-by-1\n", " nuG1(1:n_nodes,1) = (ones(n_nodes,1)*nuG0)*rho_nuG + epsi_nodes(:,6);\n", " % nuG(t+1); n_nodes-by-1\n", " \n", " R0 = R(t,1); % R(t-1)\n", " delta0 = delta(t,1); % delta(t-1)\n", " R1 = R(t+1,1); % R(t)\n", " delta1 = delta(t+1,1); % delta(t)\n", " \n", " L0 = L(t,1); % L(t)\n", " Y0 = Y(t,1); % Y(t)\n", " Yn0 = Yn(t,1); % Yn(t)\n", " pie0 = pie(t,1); % pie(t)\n", " S0 = S(t,1); % S(t)\n", " F0 = F(t,1); % F(t)\n", " C0 = C(t,1); % C(t)\n", " \n", " % Future choices at t+1\n", " %----------------------\n", " delta1_dupl = ones(n_nodes,1)*delta1;\n", " R1_dupl = ones(n_nodes,1)*R1;\n", " % Duplicate \"delta1\" and \"R1\" n_nodes times to create a matrix with\n", " % n_nodes identical rows; n_nodes-by-1\n", " \n", " X1 = Ord_Polynomial_N([log(R1_dupl) log(delta1_dupl) nuR1 nua1 nuL1 nuu1 nuB1 nuG1],Degree);\n", " % Form a complete polynomial of degree \"Degree\" (at t+1) on future state\n", " % variables; n_nodes-by-npol_2d\n", " \n", " S1 = X1*vk_2d(:,1); % Compute S(t+1) in all nodes using vk_2d\n", " F1 = X1*vk_2d(:,2); % Compute F(t+1) in all nodes using vk_2d\n", " C1 = (X1*vk_2d(:,3)).^(-1/gam); % Compute C(t+1) in all nodes using vk_2d\n", " pie1 = ((1-(1-theta)*(S1./F1).^(1-epsil))/theta).^(1/(epsil-1));\n", " % Compute pie(t+1) using condition (35)\n", " % in MM (2015)\n", " \n", " % Compute residuals for each of the 9 equilibrium conditions\n", " %-----------------------------------------------------------\n", " Residuals(t,1) = 1-weight_nodes'*(exp(nuu0)*exp(nuL0)*L0^vartheta*Y0/exp(nua0) + beta*theta*pie1.^epsil.*S1)/S0;\n", " Residuals(t,2) = 1-weight_nodes'*(exp(nuu0)*C0^(-gam)*Y0 + beta*theta*pie1.^(epsil-1).*F1)/F0;\n", " Residuals(t,3) = 1-weight_nodes'*(beta*exp(nuB0)/exp(nuu0)*R1*exp(nuu1).*C1.^(-gam)./pie1)/C0^(-gam);\n", " Residuals(t,4) = 1-((1-theta*pie0^(epsil-1))/(1-theta))^(1/(1-epsil))*F0/S0;\n", " Residuals(t,5) = 1-((1-theta)*((1-theta*pie0^(epsil-1))/(1-theta))^(epsil/(epsil-1)) + theta*pie0^epsil/delta0)^(-1)/delta1;\n", " Residuals(t,6) = 1-exp(nua0)*L0*delta1/Y0;\n", " Residuals(t,7) = 1-(1-Gbar/exp(nuG0))*Y0/C0;\n", " Residuals(t,8) = 1-(exp(nua0)^(1+vartheta)*(1-Gbar/exp(nuG0))^(-gam)/exp(nuL0))^(1/(vartheta+gam))/Yn0;\n", " Residuals(t,9) = 1-piestar/beta*(R0*beta/piestar)^mu*((pie0/piestar)^phi_pie * (Y0/Yn0)^phi_y)^(1-mu)*exp(nuR0)/R1; % Taylor rule\n", " if (zlb==1); Residuals(t,9) = Residuals(t,9)*(R1>1);end\n", " % If the ZLB is imposed and R>1, the residuals in the Taylor rule (the\n", " % 9th equation) are zero\n", " \n", " end\n", " \n", " % Residuals across all the equilibrium conditions and all test points\n", " %--------------------------------------------------------------------\n", " Residuals_mean = log10(mean(mean(abs(Residuals(1+discard:end,:)))));\n", " % Mean absolute residuals computed after discarding the first\n", " % \"discard\" observations\n", " \n", " Residuals_max = log10(max(max(abs(Residuals(1+discard:end,:)))));\n", " % Maximum absolute residuals computed after discarding the first\n", " % \"discard\" observations\n", " \n", " % Residuals disaggregated by the eqiulibrium conditions\n", " %------------------------------------------------------\n", " Residuals_max_E = log10(max(abs(Residuals(1+discard:end,:))))';\n", " % Maximum absolute residuals across all test points for each of the 9\n", " % equilibrium conditions computed after discarding the first \"discard\"\n", " % observations;\n", " \n", " time_test = toc; % Time needed to run the test\n", " '\n", "\n" ] } ], "source": [ "nkacc_txt" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "nksim_txt =\n", "\n", " '% NK_simulation.m is a routine for simulating the solution to the new \n", " % Keynesian model considerd in the article \"Merging Simulation and Projection\n", " % Approaches to Solve High-Dimensional Problems with an application to a New\n", " % Keynesian Model\" by Lilia Maliar and Serguei Maliar, Quantitative Economics\n", " % 6, 1-47 (2015) (henceforth, MM, 2015).\n", " % -------------------------------------------------------------------------\n", " \n", " % -------------------------------------------------------------------------\n", " % Inputs: \"vk\" is the matrix of coefficients of the GSSA solution;\n", " % \"nua\", \"nuL\", \"nuR\", \"nuG\", \"nuB\", \"nuu\" are the time series\n", " % of shocks generated with the innovations for the test;\n", " % \"R_init\" and \"delta_init\" are initial values for R and delta\n", " % \"gam\", \"vartheta\", \"epsil\", \"betta\", \"phi_y\", \"phi_pie\", \"mu\",\n", " % \"theta\", \"piestar\", \"Gbar\" are the parameters of the model;\n", " % \"zlb\" is a dummy parameter which is equal to 0 when ZLB is not\n", " % imposed and is equal to 1 when it is imposed;\n", " % \"Degree\" is the degree of polynomial approximation\n", " %\n", " % Output: \"S\", \"F\", \"delta\", \"C\", \"Y\", \"Yn\", \"L\", \"R\" and \"pie2\" are the\n", " % simulated series\n", " % -------------------------------------------------------------------------\n", " % Copyright � 2013-2016 by Lilia Maliar and Serguei Maliar. All rights reserved.\n", " % The code may be used, modified and redistributed under the terms provided\n", " % in the file \"License_Agreement.txt\".\n", " % -------------------------------------------------------------------------\n", " \n", " \n", " function [S F delta C Y Yn L R pie w] = NK_simulation(vk,nuR,nua,nuL,nuu,nuB,nuG,R_init,delta_init,gam,vartheta,epsil,betta,phi_y,phi_pie,mu,theta,piestar,Gbar,zlb,Degree)\n", " \n", " \n", " [T] = size(nua,1); % Infer the number of points on which the accuracy\n", " % is evaluated\n", " \n", " delta = ones(T+1,1); % Allocate memory for the time series of delta(t)\n", " R = ones(T+1,1); % Allocate memory for the time series of R(t)\n", " S = ones(T,1); % Allocate memory for the time series of S(t)\n", " F = ones(T,1); % Allocate memory for the time series of F(t)\n", " C = ones(T,1); % Allocate memory for the time series of C(t)\n", " pie = ones(T,1); % Allocate memory for the time series of pie(t)\n", " Y = ones(T,1); % Allocate memory for the time series of Y(t)\n", " L = ones(T,1); % Allocate memory for the time series of L(t)\n", " Yn = ones(T,1); % Allocate memory for the time series of Yn(t)\n", " w = ones(T,1);\n", " state = ones(T,8); % Allocate memory for the time series of the state\n", " % variables; T-by-8\n", " \n", " delta(1,1) = delta_init; % Initial condition for delta(t-1), i.e., delta(-1)\n", " R(1,1) = R_init; % Initial condition for R(t-1)\n", " \n", " for t = 1:T\n", " pol_bases = Ord_Polynomial_N([log(R(t,1)) log(delta(t,1)) nuR(t,1) nua(t,1) nuL(t,1) nuu(t,1) nuB(t,1) nuG(t,1)],Degree);\n", " % Construct the matrix of explanatory variables \"pol_bases\" on the series\n", " % of state variables; columns of \"pol_bases\" are given by the basis\n", " % functions of the polynomial of degree \"Degree\"\n", " S(t,1) = pol_bases*vk(:,1); % Compute S(t) using vk\n", " F(t,1) = pol_bases*vk(:,2); % Compute F(t) using vk\n", " C(t,1) = (pol_bases*vk(:,3)).^(-1/gam); % Compute C(t) using vk\n", " pie(t,:) = ((1-(1-theta)*(S(t,:)/F(t,:))^(1-epsil))/theta)^(1/(epsil-1));\n", " % Compute pie(t) from condition (35) in MM (2015)\n", " delta(t+1,:) = ((1-theta)*((1-theta*pie(t,1)^(epsil-1))/(1-theta))^(epsil/(epsil-1))+theta*pie(t,1)^epsil/delta(t,1))^-1;\n", " % Compute delta(t) from condition (36) in MM (2015)\n", " Y(t,:) = C(t,1)/(1-Gbar/exp(nuG(t,1)));\n", " % Compute Y(t) from condition (38) in MM (2015)\n", " L(t,1) = Y(t,1)/delta(t+1,1)/exp(nua(t,1));\n", " % Compute L(t) from condition (37) in MM (2015)\n", " Yn(t,:) = (exp(nua(t,1))^(1+vartheta)*(1-Gbar/exp(nuG(t,1)))^(-gam)/exp(nuL(t,1)))^(1/(vartheta+gam));\n", " % Compute Yn(t) from condition (31) in MM (2015)\n", " R(t+1,:) = piestar/betta*(R(t,:)*betta/piestar)^mu*((pie(t,:)/piestar)^phi_pie*(Y(t,:)/Yn(t,:))^phi_y)^(1-mu)*exp(nuR(t,1));\n", " % Compute R(t) from conditions (27), (39) in MM (2015)\n", " w(t,1) = exp(nuL(t,1))*(L(t,1)^vartheta)*(C(t,1)^gam);\n", " % Compute real wage\n", " \n", " if zlb == 1\n", " R(t+1,:) = max(R(t+1,:),1);\n", " % If ZLB is imposed, set R(t)=1 if ZLB binds\n", " end;\n", " \n", " end\n", " '\n", "\n" ] } ], "source": [ "nksim_txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Main routine" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% Start counting time\n", "% -------------------\n", "tic;" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% 1. Parameter values\n", "% -------------------\n", "zlb = 0; % Impose ZLB on nominal interest rate\n", "gam = 1; % Utility-function parameter\n", "betta = 0.99; % Discount factor\n", "vartheta = 2.09; % Utility-function parameter\n", "epsil = 4.45; % Parameter in the Dixit-Stiglitz aggregator\n", "phi_y = 0.07; % Parameter of the Taylor rule\n", "phi_pie = 2.21; % Parameter of the Taylor rule\n", "mu = 0.82; % Parameter of the Taylor rule\n", "theta = 0.83; % Share of non-reoptimizing firms (Calvo's pricing)\n", "piestar = 1.0; % Target (gross) inflation rate\n", "Gbar = 0.23; % Steady-state share of government spending in output\n", "\n", "% Autocorrelation coefficients in the processes for shocks\n", "%----------------------------------------------------------\n", "rho_nua = 0.95; % See process (22) in MM (2015)\n", "rho_nuL = 0.25; % See process (16) in MM (2015)\n", "rho_nuR = 0.0; % See process (28) in MM (2015)\n", "rho_nuu = 0.92; % See process (15) in MM (2015)\n", "rho_nuB = 0.0; % See process (17) in MM (2015)\n", "rho_nuG = 0.95; % See process (26) in MM (2015)\n", "\n", "\n", "% Standard deviations of the innovations in the processes for shocks\n", "%--------------------------------------------------------------------\n", "sigma_nua = 0.0045; % See process (22) in MM (2015)\n", "sigma_nuL = 0.4054; % See process (16) in MM (2015)\n", "sigma_nuR = 0.0028; % See process (28) in MM (2015)\n", "sigma_nuu = 0.0054; % See process (15) in MM (2015)\n", "sigma_nuB = 0.0010; % See process (17) in MM (2015)\n", "sigma_nuG = 0.0038; % See process (26) in MM (2015)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% 2. Steady state of the model (see page 19 in Supplement to MM (2015))\n", "% -------------------------------------------------------------------\n", "Yn_ss = exp(Gbar)^(gam/(vartheta+gam));\n", "Y_ss = Yn_ss;\n", "pie_ss = 1;\n", "delta_ss = 1;\n", "L_ss = Y_ss/delta_ss;\n", "C_ss = (1-Gbar)*Y_ss;\n", "F_ss = C_ss^(-gam)*Y_ss/(1-betta*theta*pie_ss^(epsil-1));\n", "S_ss = L_ss^vartheta*Y_ss/(1-betta*theta*pie_ss^epsil);\n", "R_ss = pie_ss/betta;\n", "w_ss = (L_ss^vartheta)*(C_ss^gam);" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% 3. Construct a grid for computing a solution\n", "% --------------------------------------------\n", "m = 200; % Choose the number of grid points\n", "grid_type = 1; % Choose a grid type; grid_type = 1 corresponds to a uniformely\n", " % distribued random grid, and grid_type = 2 corresponds to\n", " % a quasi Monte Carlo grid\n", "\n", "% Random grid\n", "% -----------\n", "if grid_type == 1 % Choose the grid type\n", "nuR0 = (-2*sigma_nuR+4*sigma_nuR*rand(m,1))/sqrt(1-rho_nuR^2);\n", "nua0 = (-2*sigma_nua+4*sigma_nua*rand(m,1))/sqrt(1-rho_nua^2);\n", "nuL0 = (-2*sigma_nuL+4*sigma_nuL*rand(m,1))/sqrt(1-rho_nuL^2);\n", "nuu0 = (-2*sigma_nuu+4*sigma_nuu*rand(m,1))/sqrt(1-rho_nuu^2);\n", "nuB0 = (-2*sigma_nuB+4*sigma_nuB*rand(m,1))/sqrt(1-rho_nuB^2);\n", "nuG0 = (-2*sigma_nuG+4*sigma_nuG*rand(m,1))/sqrt(1-rho_nuG^2);\n", " % Values of exogenous state variables are distributed uniformly\n", " % in the interval +/- std/sqrt(1-rho_nu^2)\n", "\n", "R0 = 1+0.05*rand(m,1);\n", "delta0 = 0.95+0.05*rand(m,1);\n", " % Values of endogenous state variables are distributed uniformly\n", " % in the intervals [1 1.05] and [0.95 1], respectively\n", "end\n", "\n", "% Quasi Monte-Carlo grid\n", "%-----------------------\n", "if grid_type == 2 % Choose the grid type\n", "dimensionality = 8; % The number of state variables (exogenous and endogenous)\n", "Def_sobol = sobolset(dimensionality);\n", " % Constructs a Sobol sequence point set in\n", " % \"dimensionality\" dimensions\n", "\n", "Sob = net(Def_sobol,m);\n", " % Get the first m points\n", "\n", "nuR0 = (-2*sigma_nuR+4*(max(Sob(:,1))-Sob(:,1))/(max(Sob(:,1))-min(Sob(:,1)))*sigma_nuR)/sqrt(1-rho_nuR^2);\n", "nua0 = (-2*sigma_nua+4*(max(Sob(:,2))-Sob(:,2))/(max(Sob(:,2))-min(Sob(:,2)))*sigma_nua)/sqrt(1-rho_nua^2);\n", "nuL0 = (-2*sigma_nuL+4*(max(Sob(:,3))-Sob(:,3))/(max(Sob(:,3))-min(Sob(:,3)))*sigma_nuL)/sqrt(1-rho_nuL^2);\n", "nuu0 = (-2*sigma_nuu+4*(max(Sob(:,4))-Sob(:,4))/(max(Sob(:,4))-min(Sob(:,4)))*sigma_nuu)/sqrt(1-rho_nuu^2);\n", "nuB0 = (-2*sigma_nuB+4*(max(Sob(:,5))-Sob(:,5))/(max(Sob(:,5))-min(Sob(:,5)))*sigma_nuB)/sqrt(1-rho_nuB^2);\n", "nuG0 = (-2*sigma_nuG+4*(max(Sob(:,6))-Sob(:,6))/(max(Sob(:,6))-min(Sob(:,6)))*sigma_nuG)/sqrt(1-rho_nuG^2);\n", " % Values of exogenous state variables are in the interval +/- std/sqrt(1-rho^2)\n", "\n", "R0 = 1+0.05*(max(Sob(:,7))-Sob(:,7))/(max(Sob(:,7))-min(Sob(:,7)));\n", "delta0 = 0.95+0.05*(max(Sob(:,8))-Sob(:,8))/(max(Sob(:,8))-min(Sob(:,8)));\n", " % Values of endogenous state variables are in the intervals [1 1.05] and\n", " % [0.95 1], respectively\n", "end\n", "\n", "if zlb == 1; R0=max(R0,1); end\n", " % If ZLB is imposed, set R(t)=1 if ZLB binds\n", "\n", "Grid = [log(R0(1:m,1)) log(delta0(1:m,1)) nuR0 nua0 nuL0 nuu0 nuB0 nuG0];\n", " % Construct the matrix of grid points; m-by-dimensionality" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% 4. Constructing polynomial on the grid\n", "% --------------------------------------\n", "Degree = 2; % Degree of polynomial approximation\n", "X0_Gs{1} = Ord_Polynomial_N(Grid, 1);\n", "X0_Gs{Degree} = Ord_Polynomial_N(Grid, Degree);\n", " % Construct the matrix of explanatory variables X0_Gs\n", " % on the grid of state variables; the columns of X0_Gs\n", " % are given by the basis functions of polynomial of\n", " % degree \"Degree\"\n", "npol = size(X0_Gs{Degree},2); % Number of coefficients in polynomial of degree\n", " % \"Degree\"; it must be smaller than the number of grid\n", " % points" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% 5. Integration in the solution procedure\n", "% ----------------------------------------\n", "N = 6; % Total number of exogenous shocks\n", "vcv = diag([sigma_nuR^2 sigma_nua^2 sigma_nuL^2 sigma_nuu^2 sigma_nuB^2 sigma_nuG^2]);\n", " % Variance covariance matrix\n", "\n", "% Compute the number of integration nodes, their values and weights\n", "%------------------------------------------------------------------\n", "[n_nodes,epsi_nodes,weight_nodes] = Monomials_1(N,vcv);\n", " % Monomial integration rule with 2N nodes\n", "%[n_nodes,epsi_nodes,weight_nodes] = Monomials_2(N,vcv);\n", " % Monomial integration rule with 2N^2+1 nodes\n", "\n", "nuR1(:,1:n_nodes) = (nuR0*ones(1,n_nodes)).*rho_nuR + ones(m,1)*epsi_nodes(:,1)';\n", "nua1(:,1:n_nodes) = (nua0*ones(1,n_nodes)).*rho_nua + ones(m,1)*epsi_nodes(:,2)';\n", "nuL1(:,1:n_nodes) = (nuL0*ones(1,n_nodes)).*rho_nuL + ones(m,1)*epsi_nodes(:,3)';\n", "nuu1(:,1:n_nodes) = (nuu0*ones(1,n_nodes)).*rho_nuu + ones(m,1)*epsi_nodes(:,4)';\n", "nuB1(:,1:n_nodes) = (nuB0*ones(1,n_nodes)).*rho_nuB + ones(m,1)*epsi_nodes(:,5)';\n", "nuG1(:,1:n_nodes) = (nuG0*ones(1,n_nodes)).*rho_nuG + ones(m,1)*epsi_nodes(:,6)';\n", " % Compute future shocks in all grid points and all integration\n", " % nodes; the size of each of these matrices is m-by-n_nodes" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% 6. Allocate memory\n", "%--------------------\n", "% TODO: pick up here!!!!\n", "e = zeros(m,3);\n", "% Allocate memory to integrals in the right side of 3 Euler equations\n", "\n", "S0_old_G = ones(m,1);\n", "F0_old_G = ones(m,1);\n", "C0_old_G = ones(m,1);\n", "% Allocate memory to S, F and C from the previous iteration (to check\n", "% convergence)\n", "\n", "S0_new_G = ones(m,1);\n", "F0_new_G = ones(m,1);\n", "C0_new_G = ones(m,1);\n", "% Allocate memory to S, F, C from the current iteration (to check\n", "% convergence)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%--------------------------------------------------------------------------\n", "%\n", "% The main iterative cycle\n", "%\n", "% -------------------------------------------------------------------------\n", "\n", "% 7. Algorithm parameters\n", "%------------------------\n", "damp = 0.1; % Damping parameter for (fixed-point) iteration on\n", " % the coefficients of 3 decision functions (for\n", " % S, F and C^(-gam))" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% 8. The loop over the polynomial coefficients\n", "% --------------------------------------------\n", "for deg = [1, Degree]\n", " diff = 1e+10;\n", " it = 0;\n", " X0_G = X0_Gs{deg};\n", "\n", " % 9. Initial guess for coefficients of the decision functions for the\n", " % variables S and F and marginal utility MU\n", " % -------------------------------------------------------------------\n", " if deg == 1\n", " vk = ones(size(Grid, 2)+1, 3)*1e-5; % Initialize first all the coefficients\n", " % at 1e-5\n", " vk(1,:) = [S_ss F_ss C_ss.^(-gam)]; % Set the initial values of the constant\n", " % terms in the decision rules for S,\n", " % F and MU to values that give the\n", " % deterministic steady state\n", " else\n", " % For degree > 1, initial guess for coefficients is given by\n", " % regressing final state matrix from degree 1 solution (e) on the\n", " % complete polynomial basis matrix\n", " vk = X0_G\\e;\n", " end\n", "\n", " while diff > 1e-7 % The convergence criterion (which is unit free\n", " % because diff is unit free)\n", "\n", " % Current choices (at t)\n", " % ------------------------------\n", " S0 = X0_G*vk(:,1); % Compute S(t) using vk\n", " F0 = X0_G*vk(:,2); % Compute F(t) using vk\n", " C0 = (X0_G*vk(:,3)).^(-1/gam); % Compute C(t) using vk\n", "\n", " pie0 = ((1-(1-theta)*(S0./F0).^(1-epsil))/theta).^(1/(epsil-1));\n", " % Compute pie(t) from condition (35) in MM (2015)\n", " delta1 = ((1-theta)*((1-theta*pie0.^(epsil-1))/(1-theta)).^(epsil/(epsil-1))+theta*pie0.^epsil./delta0).^(-1);\n", " % Compute delta(t) from condition (36) in MM (2015)\n", " Y0 = C0./(1-Gbar./exp(nuG0));\n", " % Compute Y(t) from condition (38) in MM (2015)\n", " L0 = Y0./exp(nua0)./delta1;\n", " % Compute L(t) from condition (37) in MM (2015)\n", " Yn0 = (exp(nua0).^(1+vartheta).*(1-Gbar./exp(nuG0)).^(-gam)./exp(nuL0)).^(1/(vartheta+gam));\n", " % Compute Yn(t) from condition (31) in MM (2015)\n", " R1 = piestar/betta*(R0*betta./piestar).^mu.*((pie0./piestar).^phi_pie .* (Y0./Yn0).^phi_y).^(1-mu).*exp(nuR0); % Taylor rule\n", " % Compute R(t) from conditions (27), (39) in MM (2015)\n", " if zlb == 1; R1=max(R1,1); end\n", " % If ZLB is imposed, set R(t)=1 if ZLB binds\n", "\n", " % Future choices (at t+1)\n", " %--------------------------------\n", " for u = 1:n_nodes\n", "\n", " X1 = Ord_Polynomial_N([log(R1) log(delta1) nuR1(:,u) nua1(:,u) nuL1(:,u) nuu1(:,u) nuB1(:,u) nuG1(:,u)],deg);\n", " % Form complete polynomial of degree \"Degree\" (at t+1) on future state\n", " % variables; n_nodes-by-npol\n", "\n", " S1(:,u) = X1*vk(:,1); % Compute S(t+1) in all nodes using vk\n", " F1(:,u) = X1*vk(:,2); % Compute F(t+1) in all nodes using vk\n", " C1(:,u) = (X1*vk(:,3)).^(-1/gam); % Compute C(t+1) in all nodes using vk\n", "\n", " end\n", "\n", " pie1 = ((1-(1-theta)*(S1./F1).^(1-epsil))/theta).^(1/(epsil-1));\n", " % Compute next-period pie using condition\n", " % (35) in MM (2015)\n", "\n", "\n", " % Evaluate conditional expectations in the Euler equations\n", " %---------------------------------------------------------\n", " e(:,1) = exp(nuu0).*exp(nuL0).*L0.^vartheta.*Y0./exp(nua0) + (betta*theta*pie1.^epsil.*S1)*weight_nodes;\n", " e(:,2) = exp(nuu0).*C0.^(-gam).*Y0 + (betta*theta*pie1.^(epsil-1).*F1)*weight_nodes;\n", " e(:,3) = betta*exp(nuB0)./exp(nuu0).*R1.*((exp(nuu1).*C1.^(-gam)./pie1)*weight_nodes);\n", "\n", "\n", " % Variables of the current iteration\n", " %-----------------------------------\n", " S0_new_G(:,1) = S0(:,1);\n", " F0_new_G(:,1) = F0(:,1);\n", " C0_new_G(:,1) = C0(:,1);\n", "\n", " % Compute and update the coefficients of the decision functions\n", " % -------------------------------------------------------------\n", " vk_hat_2d = X0_G\\e; % Compute the new coefficients of the decision\n", " % functions using a backslash operator\n", "\n", " vk = damp*vk_hat_2d + (1-damp)*vk;\n", " % Update the coefficients using damping\n", "\n", " % Evaluate the percentage (unit-free) difference between the values\n", " % on the grid from the previous and current iterations\n", " % -----------------------------------------------------------------\n", " diff = mean(mean(abs(1-S0_new_G./S0_old_G)))+mean(mean(abs(1-F0_new_G./F0_old_G)))+mean(mean(abs(1-C0_new_G./C0_old_G)));\n", " % The convergence criterion is adjusted to the damping\n", " % parameters\n", "\n", " % Store the obtained values for S(t), F(t), C(t) on the grid to\n", " % be used on the subsequent iteration in Section 10.2.6\n", " %-----------------------------------------------------------------------\n", " S0_old_G = S0_new_G;\n", " F0_old_G = F0_new_G;\n", " C0_old_G = C0_new_G; \n", " end\n", "end" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% 10. Finish counting time\n", "% ------------------------\n", "running_time = toc;" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% 11. Simualating a time-series solution\n", "%---------------------------------------\n", "T = 10201; % The length of stochastic simulation\n", "\n", "\n", "% Initialize the values of 6 exogenous shocks and draw innovations\n", "%-----------------------------------------------------------------\n", "nuR = zeros(T,1); eps_nuR = randn(T,1)*sigma_nuR;\n", "nua = zeros(T,1); eps_nua = randn(T,1)*sigma_nua;\n", "nuL = zeros(T,1); eps_nuL = randn(T,1)*sigma_nuL;\n", "nuu = zeros(T,1); eps_nuu = randn(T,1)*sigma_nuu;\n", "nuB = zeros(T,1); eps_nuB = randn(T,1)*sigma_nuB;\n", "nuG = zeros(T,1); eps_nuG = randn(T,1)*sigma_nuG;\n", "\n", "% Generate the series for shocks\n", "%-------------------------------\n", "for t = 1:T-1\n", " nuR(t+1,1) = rho_nuR*nuR(t,1) + eps_nuR(t);\n", " nua(t+1,1) = rho_nua*nua(t,1) + eps_nua(t);\n", " nuL(t+1,1) = rho_nuL*nuL(t,1) + eps_nuL(t);\n", " nuu(t+1,1) = rho_nuu*nuu(t,1) + eps_nuu(t);\n", " nuB(t+1,1) = rho_nuB*nuB(t,1) + eps_nuB(t);\n", " nuG(t+1,1) = rho_nuG*nuG(t,1) + eps_nuG(t) ;\n", "end\n", "\n", "% Initial values of two endogenous state variables\n", "%-------------------------------------------------\n", "R_initial = 1; % Nominal interest rate R\n", "delta_initial = 1; % Price dispersion \"delta\"\n", "\n", "tic;\n", "% Simulate the model\n", "%-------------------\n", "[S, F, delta, C, Y, Yn, L, R, pie, w] = NK_simulation(vk,nuR,nua,nuL,nuu,nuB,nuG,R_initial,delta_initial,gam,vartheta,epsil,betta,phi_y,phi_pie,mu,theta,piestar,Gbar,zlb,Degree);\n", "\n", "simulation_time = toc;" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Solver time: 3.988508 seconds\n", "Simulation time: 0.194495 seconds\n", "Accuracy time: 0.922081 seconds\n", "Total time: 5.105085 seconds\n", "pistar: 1.0000 sigma_L: 0.4054\n", "zlb : 0 grid: 1\n", "tex line: -1.08 & -1.27 & 5.11\n" ] } ], "source": [ "% 12. Compute unit free Euler equation residuals on simulated points\n", "%-------------------------------------------------------------------\n", "discard = 200; % The number of observations to discard\n", "time0 = tic;\n", "\n", "[Residuals_mean(1), Residuals_max(1), Residuals_max_E(1:9,1), Residuals] = NK_accuracy(nua,nuL,nuR,nuG,nuB,nuu,R,delta,L,Y,Yn,pie,S,F,C,rho_nua,rho_nuL,rho_nuR,rho_nuu,rho_nuB,rho_nuG,gam,vartheta,epsil,betta,phi_y,phi_pie,mu,theta,piestar,vcv,discard,vk,Gbar,zlb,Degree);\n", "\n", "accuracy_time = toc;\n", "% Compute the residuals; Residuals_mean, Residuals_max and Residuals_max_E(1:9,5)\n", "% are filled into the 5h columns of the corresponding vectors/matrix, while\n", "% the first 4 columns correspond to PER1, PER2 with and without ZLB\n", "\n", "% ------------------------------------------------------------------------\n", "% disp(' '); disp(' OUTPUT:'); disp(' ');\n", "% disp('RUNNING TIME (in seconds):'); disp('');\n", "% display(running_time);\n", "% disp('SIMULATION TIME (in seconds):'); disp('');\n", "% display(simulation_time);\n", "% disp('ACCURACY TIME (in seconds):'); disp('');\n", "% display(accuracy_time);\n", "% disp('APPROXIMATION ERRORS (log10):'); disp('');\n", "% disp('a) mean error in the model equations');\n", "% disp(Residuals_mean)\n", "% disp('b) max error in the model equations');\n", "% disp(Residuals_max)\n", "% disp('b) max error in by equation');\n", "% disp(Residuals_max_E(1:9,1))\n", "\n", "l1 = log10(sum(max(abs(Residuals), [], 1)));\n", "tot_time = running_time + simulation_time + accuracy_time;\n", "fprintf('Solver time: %4.6f seconds\\n', running_time);\n", "fprintf('Simulation time: %4.6f seconds\\n', simulation_time);\n", "fprintf('Accuracy time: %4.6f seconds\\n', accuracy_time);\n", "fprintf('Total time: %4.6f seconds\\n', running_time + simulation_time + accuracy_time)\n", "fprintf('pistar: %3.4f sigma_L: %3.4f\\n', piestar, sigma_nuL);\n", "fprintf('zlb : %d grid: %d\\n', zlb, grid_type);\n", "fprintf('tex line: %0.2f & %0.2f & %0.2f\\n', l1, Residuals_max, tot_time);" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Matlab", "language": "matlab", "name": "matlab" }, "language_info": { "codemirror_mode": "octave", "file_extension": ".m", "help_links": [ { "text": "MetaKernel Magics", "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" } ], "mimetype": "text/x-matlab", "name": "matlab", "version": "0.14.3" } }, "nbformat": 4, "nbformat_minor": 2 }