Dynare Wouter J. Den Haan University of Amsterdam July 26, 2010 Introduction Do it yourself Tricks IRFs & Simulations Introduction • What is the objective of perturbation? • Peculiarities of Dynare • Some examples Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Objective of lst-order perturbation • Obtain linear approximations to the policy functions that satisfy the first-order conditions • state variables: Xt = [x\rt X2rt %3,t • • • Xn,t]' • result: yt = y+{xt- x)'a • a bar above a variable indicates steady state value Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Underlying theory • Model: Ef[f(g(x))]=0, • f(x) is completely known • g(x) is the unknown policy function. • Perturbation: Solve sequentially for the coefficients of the Taylor expansion of g(x). • More info: • notes and slides on perturbation • slides on Blanchard-Kahn conditions Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Neoclassical growth model • xt = [kt-i,zt] • yt = [ct,kt,zt] • linearized solution: ct = c + aC/k(kt-i - k) + ac,z(zt - z) kt = k + %k(h-i akiZ{zt - z) Zt = pZt-\ + £t Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Linear in what variables? • Dynare does not understand what q is. • could be level of consumption • could be log of consumption • could be rainfall in Scotland • Dynare simply generates a linear solution in what you specify as the variables • More on this below Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Peculiarities of Dynare Variables known at beginning of period t must be dated t — 1. Thus, • k{. the capital stock chosen in period t • kt-\\ the capital stock available at beginning of period t Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Peculiarities of Dynare The solution ct = c + aCtk(kt-i - k) + aCrZ(zt - z) kt = k + ak/k(kt_i -k)+ ak/Z(zt - z) zt = pzt-i + £t can of course be written (less conveniently) as ct = c + aC/k(kt-i -k)+ aCiZ_x (zt-i -z)+ aCrZet kt = k + aKk{kt-i -k)+ ak/Zl (zt-i -z)+ ak/Zet Zt = pZt-l + £t with aCrZl = paCrZ and ak/Zl = pak/Z Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Peculiarities of Dynare • Dynare gives the solution in the less convenient form: ct = c + aC/k(kt_i -k)+ aCiZ_x {zt-\ -z)+ ac>zet kt = k + ak/k(kt-i -k)+ ak/Zl (zt-i - z) + ak/Zet zt = pzt-i + et • Since the Dynare solution satisfies flc,z_! = pac,z and ak/Zl = pak/Z one could always rewrite the Dynare solution in the more convenient form Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Dynare program blocks • Labeling block: indicate which symbols indicate what • variables in "var" • exogenous shocks in "varexo" • parameters in "parameters" • Parameter values block: Assign values to parameters Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Dynare program blocks • Model block: Between "model" and "end" write down the n equations for n variables • note that dynare has no conditional expectations but if an equation has a (+1) variable, then Dynare knows there is a conditional expectation Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Dynare program blocks • Initialization block: Dynare has to solve for the steady state. This can be the most difficult part (since it is a true non-linear problem). So good initial conditions are important • Random shock block: Indicate the standard deviation for the exogenous innovation Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Dynare program blocks • Solution & Properties block: • Solve the model with the command • lst-order: stochsimul(order=l,nocorr,nomoments,IRF=0) • 2nd-order: stochsimul(order=2,nocorr,nomoments,IRF=0) • Dynare can calculate IRFs and business cycle statistics. E.g., • stochsimul(order=l,IRF=30), • but I would suggest to program this yourself (see below) Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Running Dynare • In Matlab change the directory to the one in which you have your *.mod files • In the Matlab command window type dynare programname • This will create and run several Matlab files Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Model with productivity in levels (FOCs A) Specification of the problem |- V—'00 ot — \C\ V~ 1 max{CfMEi:f=1/3 s.t. ct + kt = zt^_1 + {l-S)kt-i Zt = (1 - p) + pZt-l + Et Icq given Et[et+1] = 0 & Ef[£2 ] = a2 Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Distribution of innovation • lst-order approximations: • the distribution of £f does not matter, except that Et[£f+i] has to be zero. • 2nd-order approximations: • a matters (it affects the mean) • higher-order moments do not • Also see notes and slides on perturbation theory Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Everything in levels: FOCs A Model equations: Lt ct + kt Zt Dynare equations: c~(-nu) =beta*c(+1)~(-nu)*(alpha*z(+1)*k~(alpha-1)+l-delta); c+k=z*k(-l)~alpha+(l-delta)k(-l); z=(1-rho)+rho*z(-1)+e; ztK-x + (1 - <%-l (1 - p) + pzt-i + et Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Policy functions reported by Dynare • 5 = 0.025,1/ = 2, oc = 0.36, 0 = 0.99, and p = 0.95 POLICY AND TRANSITION FUNCTIONS k z c constant 37.989254 1.000000 2.754327 k(-l) 0.976540 -0.000000 0.033561 z(-l) 2.597386 0.950000 0.921470 e 2.734091 1.000000 0.969968 Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical !!!! You have to read output as k z c constant 37.989254 1.000000 2.754327 k(-l)-kss 0.976540 -0.000000 0.033561 z(-l)-zss 2.597386 0.950000 0.921470 e 2.734091 1.000000 0.969968 • That is, explanatory variables are relative to steady state. • (Note that steady state of e is zero by definition) • If explanatory variables take on steady state values, then choices are equal to the constant term, which of course is simply equal to the corresponding steady state value Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Changing amount of uncertainty Suppose u = 0.1 instead of 0.007 POLICY AND TRANSITION FUNCTIONS k constant 37.989254 k(-l) 0.976540 z(-l) 2.597386 e 2.734091 z c 1.000000 2.754327 -0.000000 0.033561 0.950000 0.921470 1.000000 0.969968 Any change? Introduction Do it yourself Tricks IRFs & Simulations Model with productivity in logs Specification of the problem 00 1-1/ _ -1 maxET^-1^-- {ctM ftr i~y s.t. Ct + kt = exp(zt)K_1 + (l-S)kt Zt = pZt-l + £t k0 given, Ef[em] = 0 Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Variables in levels & prod, in logs - FOCs B Model equations: ct + kt /3c-l'1(aexp(zm)^-1 + l-= exp(zf)^_1 + (1 - 8)kt-\ Zt = pZt-i + £t 8) Dynare equations: c~(-nu) =beta*c(+l)~(-nu)*(alpha*exp(z(+l))*k~(alpha-l)+l-delta); c+k=exp(z)*k(-l)~alpha+(l-delta)k(-l); z=rho*z(-l)+e; Introduction Do it yourself Tricks IRFs & Simulations Pruning Policy functions reported by Dynare • 5 = 0.025,1/ = 2, oc = 0.36 and 0 = 0.99 POLICY AND TRANSITION FUNCTIONS k z c constant 37.989254 0.000000 2.754327 k(-l) 0.976540 -0.000000 0.033561 z(-l) 2.597386 0.950000 0.921470 e 2.734091 1.000000 0.969968 • What does z stand for here? Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Linear solution in what? Dynare gives a linear system in what you specify the variables to be Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical All variables in logs - FOCs C Model equations: (exp(Q)r = = Et [/3(exp(Q+i))-1/(aexp(zf+i)(exp(^))fl;"1 + 1 - <*) exp(cf) + exp(fcf) = exp(zf)(exp(^_i))a + (1-0) exp(kt_i] zt = pzt-i + et The variables ct and kt are the log of consumption and capital. Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical All variables in logs - FOCs C Model equations (rewritten a bit) exp(-vct) = Et [jSexp(-VQ+i)(aexp(zf+i + (a - l)kt) + 1 - $)] exp(ct) + exp(kt) = exp(zf + ockt-i) + (1 - $) exp(kt_i) zt = pzt-i + et Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical All variables in logs - FOCs C Dynare equations: exp(-nu*lc)=beta*exp(-nu*lc(+1))* (alpha*exp(lz(+1)+(alpha-1)*lk))+l-delta); exp(lc)+exp(lk) =exp(lz+alpha*lk(-l))+(l-delta)exp(lk(-l)); lz=rho*lz(-l)+e; Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical All variables in logs - FOCs C • This system gives policy functions that are linear in the variables lc, i.e., ln(cf), lk,i.e., ]n(kt), and lz, i.e., ln(zf), • Programmers often do not make clear that a variable is a log. That is, they would simply use c, k, and z in the dynare equations above instead of lc, lk, and lz Introduction Do it yourself Tricks IRFs & Simulations Pruning PracLical All variables in logs - FOCs C Dynare equations (with different notation): exp(-nu*c)=beta*exp(-nu*c(+1))* (alpha*exp(z(+1)+(alpha-1)*k))+l-delta); exp(c)+exp(k)=exp(z+alpha*k(-1))+(1-delta)exp(k(-l)); z=rho*z(-l)+e; Introduction Do it yourself IRFs & Simulations Pruning POLICY AND TRANSITION FUNCTIONS for foe B constant k(-l) e 37.989254 0.976540 2.597386 2.734091 0.000000 -0.000000 0.950000 1.000000 2.754327 0.033561 0.921470 0.969968 POLICY AND TRANSITION FUNCTIONS for foe C constant k(-l) e 3.637303 0.976540 0.068372 0.071970 0.000000 0.000000 0.950000 1.000000 1.013173 0.462887 0.334554 0.352162 Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical These are not the same solutions Suppose that k0 = 49.3860 & zt = 0 W 44.5 Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Example with analytical solution • If 5 = v = 1 then we know the analytical solution. It is kt = tx^exp(zt)kat_1 ct = (1 - ocp) exp(zf)^_1 or \nkt = h\(tx.(5) + tx.h\kt-i+Zt In ct = ln(l — a/3) + (x\nkt-i + Zt • That is, the policy rules are linear in the logs Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Dynare solutions I Dynare equations (with consumption and capital in logs): exp(-c) =beta*exp(-c(+1))*(alpha*exp(z(+1)+(alpha-1)*k))); exp(c)+exp(k)=exp(z+alpha*k(-1)); z=rho*z(-l)+e; Introduction Do it yourself Tricks IRFs & Simulations Dynare solutions I • Dynare solution (a = 0.36 and f> = 0.99) k z c constant -1.612037 0.000000 -1.021009 k(-l) 0.359999 -0.000000 0.360000 z(-l) 0.949998 0.950000 0.950000 e 0.999998 1.000000 1.000000 • Note that c, k, and z are logs • Check yourself that this is correct Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Dynare solutions II Dynare equations (with consumption and capital in levels): l/c=beta*(1/c(+1))*(alpha*exp(z(+1))*k~(alpha-1)); c+k=exp(z)*k(-l)"alpha; z=rho*z(-l)+e; Introduction Do it yourself Tricks IRFs & Simulations Dynare solutions II • Dynare solution (a = 0.36 and ß = 0.99) k z c constant 0.199482 0.000000 0.360231 k(-l) 0.360000 0.000000 0.650101 z(-l) 0.189507 0.950000 0.342219 e 0.199482 1.000000 0.360231 • Note that c and k indicate levels and z logs • This is not the same !!! Introduction Do it yourself Tricks IRFs & Simulations Pruning Substitute out consumption- FOCs D Model equations: [zt exp(afcf_i) + (1 - 6) exp(kt_i) - exp(kt)] ~V [zt+i exp(otkt) + (1-6) exp(kt) - exp(£m)] {oczt+i exp((o: - l)kt) + 1-6) zt = (1 - p) + pZt-l + Zt Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Dynare solution Dynare equations (with capital in logs): (z*exp(alpha*lk(-l)+(1-delta)*exp(lk(-1))-exp(lk))~(-nu) =beta*(z(+1)*exp(alpha*lk+(1-delta)*exp(lk)-exp(lk(+1))~( *(alpha*exp(z(+1)+(alpha-1)*lk))+(1-delta)); z=l-rho+rho*z(-1)+e; Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Dynare solution • Dynare solution (a = 0.36 and /3 = 0.99) Ik z constant 3.637303 0.000000 lk(-l) 0.976540 0.000000 z(-l) 0.068372 0.950000 e 0.071970 1.000000 • Given this law of motion for \n(kt) you can solve Ct using the non-linear equation Ct = exp(zt) exp(a *\nkt-i) + (1 — 5) exp(\nkt-i) — exp(lnfcf) Introduction Do it yourself Tricks IRFs & Simulations Pruning Do it yourself! • Try to do as much yourself as possible Introduction Do it yourself Tricks IRFs & Simulations What (not) to do your self Policy functions: • can be quite tricky so let Dynare do it. IRFs, business cycle statistics, etc: • easy to program yourself • you know exactly what you are getting Introduction Do it yourself Tricks IRFs & Simulations Pruning Why do things yourself? • Dynare linearizes everything • Suppose you have an RBC in log of capital • Add the following equation to introduce investment exp(z't) = exp(kt) -(1-6) exp(h-i) • Dynare will approximate this linear equation. Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Why do things yourself? • Now suppose you have an approximation in levels • Add the following equation to introduce output yt = ztkt lit • Dynare will take a first-order condition of this equation to get a first-order approximation for yt • But you already have solutions for kt and ht Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Why do things yourself? • Getting the policy rules requires a bit of programming • Thus, it makes sense to use Dynare for this • But the more you program yourself, the better you understand the results • Try, therefore, to program the simpler things, like IRFs, simulated time paths, and business cycle statistics yourself, that is, simply use • stoch_simul(order=l,nocorr,nomoments,IRF=0) Introduction Do it yourself Tricks IRFs & Simulations Pruning Tricks • Incorporating Dynare in other Matlab programs • Reading parameter values in *.mod file from external file • Reading Dynare policy functions as they appear on the screen • How to get good initial conditions (to solve for steady state) Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Keeping variables in memory • Dynare clears all variables out of memory • To overrule this, use dynare program.mod noclearall Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Saving solution to a file • Replace the file "disp_dr.m" with the provided file • I made two changes: • The original Dynare file only writes a coefficient to the screen if it exceeds 10"6 in absolute value. I eliminated this condition • I save the policy functions, exactly the way Dynare now writes them to the screen To load the policy rules into the matrix "decision" simply type load dynarerocks Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Saving solution to a file • Note that Dynare also saves policy functions, but for second-order this is not what you see on the screen Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Saving solution to a file • Note that Dynare also saves policy functions, but for second-order this is not what you see on the screen Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Loops • This trick allows you to run the same dynare program for different parameter values • Suppose your Dynare program has the command nu=3; • You would like to run the program twice; once for nu=3, and once for nu=5. Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Loops O In your Matlab program, loop over the different values of nu. In each iteration, first save the current value of nu (and the associated name) to the file wouterrocks with "save parameterfile nu and then run Dynare © In your Dynare program file, replace the command "nu = 3" with load parameterfile set_param_value('nu',nu); Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Using loop to get good initial conditions With a loop you can update the initial conditions used to solve for steady state O Use parameters to definite initial conditions © Solve model for simpler case 0 Gradually change parameter O You can even gradually change models using weighting coefficients 0 Alternative: (also) use different algorithm to solve for steady state O solve_algo=l,2, or 3 0 solve for coefficients instead of variables Introduction Do it yourself Tricks IRFs & Simulations Pruning Simple model with endogenous labor /3c-v1(aexp(zm) (h/ht+xf-1 + 1-5) ct + kt = exp{zt)k?_1hL-« + {l-crv(l-a)exp(z,)(fc,_i/fe,)a S)kt-i Zt = pZt-l + £t Introduction Do it yourself Tricks IRFs & Simulations Pruning Simple model with endogenous labor O Solve for c,k, h using 1 = 0(a (k/hy-1 + 1 - 5) c + k = kah1-a + (1-S)k c~v(l -cc)(k/hf = 0 Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical IRFs for linear systems • Value of kT_i and values of original shock {et}J=T irrelevant for IRFs • Thus, make your life easy by setting • t= 1 • ko(= fcT_i) = k • £T+y = 0 for j > OTake as given ko, Zq, and time series for £f, • If k is in logs then subtract k and you have the IRF • If k is in levels calculate (kT+j — k)/k or \n(kT+j/k) Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Impulse Response functions lst-order case: Dynare gives you kt = k + aKk(kt-i -k)+ ak/Zl (zt-\ - z) + ak/£et • Start atko = k and Zq = z (= 0) • Let E\ = <7£ and et = 0 for t > 1 • Calculate time path for Zt • Calculate time path for kt • Calculate time path for other variables • Calculate % change (subtract steady-state value if variables are in logs) Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Impulse Response functions 2nd-order case: • One could repeat procedure described in last slide • But with a non-linear law of motion results do depend on initial value of k, realizations of shocks in the original series, and whether e* = eT + u or e* = eT — u • For example, IRF can be different when initial capital stock is low than when it is high Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical How to calculate a simulated data set Dynare gives you kt = k + ak>k(kt_i -k)+ ak>Zl (zt-i - z) + akf£et • Start at ko = k and Zq = z (= 0) • Use a random number generator to get a series for et for t = 1 to t = T • Calculate time path for Zt • Calculate time path for kt • Calculate time path for other variables • Discard an initial set of values • Note that procedure is the same for first and second-order solutions Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Simulate higher-order & pruning • first-order solutions are by construction stationary • simulation cannot be problematic • simulation of higher-order can be problematic • simulation of 2nd-order will be problematic for large shocks • trick proposed: Pruning • pruning: • is a trick to ensure stability • it uses a distorted numerical approximation Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Pruning k(n\k_i,z): the nth-order perturbation solution for k as a function of k_\ and z. 4M):the value of kt generated with k(n\-). Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Pruning For n > 1, the regular perturbation solution can be written as k^-kss flW+flW (kf\-kss)+a^ (zt-zss) + *(b)(*£U) Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Pruning • With pruning one would simulate two series k^-kss = (k^ -kss)+a^(zt-zss) flW+flW m-kss)+a^ (zt-zss) +m(k(t%zt) • kt is stationary as long as BK conditions are satisfied • k(n)(k^}yZt) is then also stationary < 1 then ensures that k\ is stationary » Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Pruning • The pruned simulated series, k^ is NOT a function of the corresponding state variables k\ and Zt Introduction Do it yourself Tricks IRFs & Simulations Pruning Practical Practical • Dynare expects files to be in a regular path like e:\... and cannot deal with subdirectories like //few.eur.nl/.../... • The solution is to put your *.mod files on a memory stick