{ "cells": [ { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Solving and simulating a RBC model adapted from Fabrice Collard's Matlab code, http://fabcol.free.fr/\n", "# tested in Julia 0.6.1.1\n", "# this code is part of chapter 3, \"Solving and Simulating DSGE Models\" from the book: \"Introduction to Quantitative Macroeconomics using Julia\"\n", "# Academic Press - Elsevier\n", "# for comments, email at: petre(dot)caraiani(at)gmail(dot)com\n", "alpha = 0.4;\n", "delta = 0.025;\n", "rho = 0.95;\n", "beta= 0.988;\n", "hs = 0.31;\n", "sz = 0.00217;\n", "\n", "# Deterministic Steady state\n", "\n", "ysk = (1-beta*(1-delta))/(alpha*beta);\n", "ksy = 1/ysk;\n", "si = delta/ysk;\n", "sc = 1-si;\n", "\n", "#Define:\n", "#Y=[k(t+1) a(t+1) E_tc(t+1)]\n", "#X=[y,c,i,h]\n", "\n", "ny = 3; # of variables in vector Y\n", "nx = 4; # of variables in vector X\n", "ne = 1; # of fundamental shocks\n", "nn = 1; # of expectation errors\n", "\n", "# Initialize the Upsilon matrices\n", "\n", "UX=zeros(nx,nx);\n", "UY=zeros(nx,ny);\n", "UE=zeros(nx,ne);\n", "UN=zeros(nx,nn);\n", "\n", "G0Y=zeros(ny,ny);\n", "G1Y=zeros(ny,ny);\n", "G0X=zeros(ny,nx);\n", "G1X=zeros(ny,nx);\n", "GE=zeros(ny,ne);\n", "GN=zeros(ny,nn);\n", "\n", "# Production function\n", "\n", "UX[1,1]=1;\n", "UX[1,4]=alpha-1;\n", "UY[1,1]=alpha;\n", "UY[1,2]=rho;\n", "UE[1]=1;\n", "\n", "#Consumption c(t)=E(c(t)|t-1)+eta(t)\n", "UX[2,2]=1;\n", "UY[2,3]=1;\n", "UN[2]=1;\n", "\n", "# Resource constraint\n", "\n", "UX[3,1]=1;\n", "UX[3,2]=-sc;\n", "UX[3,3]=-si;\n", "\n", "# Consumption-leisure arbitrage\n", "\n", "UX[4,1]=-1;\n", "UX[4,2]=1;\n", "UX[4,4]=1;\n", "\n", "# Accumulation of capital\n", "\n", "G0Y[1,1]=1;\n", "G1Y[1,1]=1-delta;\n", "G1X[1,3]=delta;\n", "\n", "# Productivity shock\n", "\n", "G0Y[2,2]=1;\n", "G1Y[2,2]=rho;\n", "GE[2]=1;\n", "\n", "# Euler equation\n", "\n", "G0Y[3,1]=1-beta*(1-delta);\n", "G0Y[3,3]=1;\n", "G0X[3,1]=-(1-beta*(1-delta));\n", "G1X[3,2]=1;\n", "\n", "# Solution\n", "\n", "# Step 1: solve the first set of equations\n", "\n", "PIY = inv(UX)*UY;\n", "PIE = inv(UX)*UE;\n", "PIN = inv(UX)*UN;\n", "\n", "# Step 2: build the standard System\n", "\n", "A0 = G0Y+G0X*PIY;\n", "A1 = G1Y+G1X*PIY;\n", "B = GE+G1X*PIE;\n", "C = GN+G1X*PIN;\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "#First we compute the Schur decomposition A0=Q'*T*Z' and A1=Q'*S*Z'\n", "r = schurfact(complex(A1),complex(A0))\n", "tol_=1e-8;\n", "cutoff=1\n", "sel = (abs.(diag(r[:S])./diag(r[:T])).r2; \n", " println(\"Indeterminacy: adding beliefs\")\n", " ME = MY0*[Transfo*Q*[B C];zeros(m,size([B C],2))];\n", " ME = Z*ME;\n", " ETA = (U2[:,1:r2]*(D2[1:r2,1:r2]\\V2[:,1:r2]'))'*Q2*[B C];\n", "else\n", " ME = MY0*[Transfo*Q*B;zeros(m,size(B,2))];\n", " ME = Z*ME;\n", " ETA = (U2[:,1:r2]*(D2[1:r2,1:r2]\\V2[:,1:r2]'))'*Q2*B;\n", "end" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "#solutions\n", "MY = real(MY);\n", "ME = real(ME);\n", "ETA = real(ETA);\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#simulate\n", "# Step 4: Recover the impact function\n", "\n", "PIE=PIE-PIN*ETA;\n", "\n", "#horizon of responses\n", "nrep = 20; \n", "YS = zeros(3,nrep);\n", "XS = zeros(4,nrep);\n", "Shock = 1;\n", "YS[:,1] = ME*Shock;\n", "XS[:,1] = PIE;\n", "for t=2:nrep;\n", " YS[:,t] = MY*YS[:,t-1];\n", " XS[:,t] = PIY*YS[:,t-1];\n", "end\n", "\n", "#Pkg.add(\"Plots\")\n", "using Plots\n", "pyplot()\n", "#plotly() # Choose the Plotly.jl backend for web interactivity\n", "plot(XS[1,:],linewidth=1,label=\"output\",title=\"Impulse Response Function to a 1% technological shock\")\n", "plot!(XS[3,:],linewidth=1,label=\"investment\")\n", "\n" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Julia 0.6.1", "language": "julia", "name": "julia-0.6" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "0.6.1" } }, "nbformat": 4, "nbformat_minor": 1 }