Running simple DSGE model in Dynare

Dynare is quite sophisticated software for economic simulation. It is mainly used for estimating of Dynamic Stochastic General Equilibrium Models (DSGE models). DSGE models are considered as the state-of-the-art of economic simulations and predictions and they are used by plenty of central banks and ministries of finance all over the world.

You can find a lot of articles about DSGE models on the internet. Most of them are really sophisticated, however, they are usually written for people with good knowledge of mathematical optimization and RBC models theory. I would like to write this article as Dynare tutorial for people with limited knowledge of both economic models and mathematical optimization.

Model definition

We will show how Dynare works on a basic economic model. Let’s assume that we have a representative household who wants to maximize an expected present value of its utility in an infinite time horizon. If you are new in the RBC models theory, this may sound horrifying to you. So I would like to explain what does this actually mean.

We assume that household’s utility depends on both amounts of consumption and amount of free time. The household splits his time among work and free time. The more time it works the more consumption it can afford. On the other hand, the work is unpleasant and the household would rather enjoy its free time than spending time by work. So the household balances these two variables to maximize its utility.

The decisions are made in multiple time periods and decision in one period affects the situation in following time periods. In our model for example when household makes savings in some time period, it will get interests from these savings in future time periods. This makes our model dynamic. The household maximizes its utility in infinite time horizon. The infinite time horizon is used to make to the solution of the model easier (this may sound unlikely to you but I will prove it in this article). The usual interpretation of this fact is that the household takes care about its childer, grandchildren etc. What does the present value mean? Close time periods are more important for the household than the distant one. Or we could say that the household prefers utility now than the utility in the future. For example, if we want to convince the household to postpone its consumption, we need to offer higher consumption in the future. This is the basic principle of savings – people save money to earn the interest. We can say that the household is impatient. The impatience of the household is given by a parameter \beta . The lower values of \beta means that the household is more impatient. Let’s remark time period as t. So if we multiply utility in the time period by \beta^t , we get the present value of the utility. It is similar to calculating a present value of future money income.

Finally, we will explain the expected value. The S letter in DSGE means stochastic. So there is some economic uncertainty in the model.The household cannot be sure about its future income because the economy is affected by random events. So the household does not know the exact value of its utility in the future. However, the household makes some predictions about the future. So if the household makes some economic decision it has some expectation about its outcome. So the household makes the decision to maximize the expected value of the future utility. We add a \mathbb{E}_0 symbol to represent the expected value. The 0 symbol says that the expectations are being made into zero time period.

This is the formula of household decision problem:

\max \mathbb{E}_0 \sum\limits_{t=0}^{\infty} \beta^t [ \ln (c_t) - \psi \cdot n_t] \, .

We have two kinds of “symbols” in the formula. The symbols with t lower index are variables. The value of the variables can be changed by the household and the values are different in different time periods. In fact, the household maximizes its utility by setting values of the variables. The symbols without the t index are parameters. The values of the parameters are constant for all time periods and they describe the household behavior.

Let’s define the variables and parameters in the utility function. The c_t represents an amount of consumed goods at t period, n_t is an percentage of disposable time spent by work at t period, \ln is natural logarithm function\psi is a parameter that gives us the “unwillingness” to work. If we increase the value of the parameter, the household will work less because the work is more unpleasant to it. As you can see, the utility of the household

As you can see, the utility of the household at time t is given by the difference between utility gain from the consumption and disutility of time spent by work: \ln (c_t) - \psi \cdot n_t . Then this utility is discounted to its value at time 0 by multiplying by \beta^t . Because we want to calculate the sum of utilities at all time periods, we add the sum.

We must also add restrictions to the model. If we didn’t, a there household could consume an unlimited amount of goods. But the amount of consumption is limited by a number of goods which was produced in the economy. To calculate the amount of consumption we need to define a production function. The production function gives us an amount of produced goods based on a number of production factors. We have two production factors in the model: the work and the capital goods. The amount of capital goods at time t is defined by variable k_t . The amount of work is given by variable n_t which already appeared in the utility function.

We will use a Cobb-Douglas production function. This production function has one parameter \alpha. This parameter represents the output elasticity of capital. So the production function formula is: k^{\alpha}_t \cdot n_t^{1-\alpha} . The production function is multiplied by e^{z_t} . The z_t is a value of a stochastic shock at time t . The stochastic shock represents the stochastic element of the model. The value of the stochastic shock is random and it is not determined by any other element of the model. This is the reason why we call it the exogenous stochastic shock.

The amount of produced goods is split to consumption, depreciation and investment. So we can write the formula of the model condition as

c_t + k_{t+1} - (1 - \delta) \cdot k_t = A \cdot e^{z_t} \cdot k^{\alpha}_t \cdot n_t^{1-\alpha} \, .

This formula is sometimes called as the law of motion for capital.

Finally, we must define the exogenous stochastic shock more closely. We assume that the shock value has some inertia. In other words, we can say that the value of the shock partly depends on its own value in previous time periods. For example, if the shock had high positive value at the previous period, it the value at this period will be more likely positive than negative. This is modeled by an autoregressive process. Let’s look at the process formula:

z_t = \rho_z \cdot z_{t-1} + \epsilon_t \, ,

The shock value z_t is given by its previous value z_{t-1} and by random value \epsilon_t . A higher value of the \rho_z parameter means that the present value of the shock is more dependent on its previous value. The stochastic variable is denoted by \epsilon_t . This variable is random and it’s generated by random number generator. But to be able to generate values of this variable, we need to set a random distribution of the variable and its parameters. In the most cases, the normal distribution is used. The normal distribution has two parameters: mean value and variance. Our mean value will be 0. The stochastic shock is the source of uncertainty, but it is not moving the economy permanently to one side. The value of variance is not restricted – it depends on how big uncertainty we want to have in the model.

Solving the model

Now we will solve the model. By solving we mean to solve the first order conditions (FOC). Generally, the first order conditions give us points where can be an extreme. The second order conditions confirm that there is really the extremal value at some point and that it is minimum or maximum. Mostly in economic models, we solve only the first order conditions. The assumptions of the models guarantee us that we have really found the extremal value and that we found the type of extreme which we need. In our case, the first order conditions give us values which maximize the household utility.

We will use the method of Lagrangian multipliers to solve the model. At the biginnig, we have to set up a Lagrangian function. This equation containt of the utility function and all the conditions equation multiplied by Lagrangian multiplier. We created only one equation, however this equation must be valid at every time period. So, we have the infinite number of the conditions. So also need infinite number of the Lagrangian multipliers and the Lagrangian multiplier for the period t will be denoted by \lambda_t .

\mathscr{L} = \mathbb{E}_0\sum_{t=0}^{\infty} \beta^t [ \ln c_t - \psi \cdot n_t] + \sum_{t=0}^{\infty} \lambda_t \cdot [ A \cdot e^{z_t} \cdot k^{\alpha}_t \cdot n_t^{1-\alpha} - c_t - k_{t+1} + (1 - \delta) \cdot k_t ] \, .

To solve the model, we need to derive the Lagrangian formula by all its variables and set these derivatives equal to zero. First, we will derive the Lagrangian formula by $\k_{t+1} $ and we will set this derivative eqaul to 0:

\frac{\partial \mathscr{L}}{\partial k_{t}} = -\lambda_{t-1} + \lambda_{t} \cdot \left( A \cdot e^{z_t} \cdot k^{\alpha - 1}_{t} \cdot \alpha \cdot n_{t}^{1-\alpha} + 1 - \delta \right) = 0 \, .

This is the most complicated step for the solution and maybe is difficult for some people to understand the result. To make it clearer, I will write the formula without the sum.

\begin{aligned} \mathscr{L} &= \mathbb{E}_0 \beta^0 [ \ln c_0 - \psi \cdot n_0] \\ +& \beta^1 \left[ \ln c_1 - \psi \cdot n_1 \notag \right] \\ +& \dotsb \left . \right. \\ +& \beta^{\tau} \left[ \ln c_{\tau} - \psi \cdot n_{\tau} \notag \right] \\ +& \lambda_0 \left [ A \cdot e^{z_0} \cdot k^{\alpha}_0 \cdot n_0^{1-\alpha} - c_0 - k_{1} + (1 - \delta) \cdot k_0 \notag \right] \\ +& \lambda_1 \left [ A \cdot e^{z_1} \cdot k^{\alpha}_1 \cdot n_0^{1-\alpha} - c_1 - k_{2} + (1 - \delta) \cdot k_1 \notag \right] \\ +& \dotsb \left . \right. \\ +& \lambda_{\tau-1} \left [ A \cdot e^{z_{\tau-1}} \cdot k^{\alpha}_{\tau-1} \cdot n_{\tau-1}^{1-\alpha} - c_{\tau-1} - k_{{\tau}} + (1 - \delta) \cdot k_{\tau-1} \notag \right] \\ +& \lambda_{\tau} \left [ A \cdot e^{z_{\tau}} \cdot k^{\alpha}_{\tau} \cdot n_{\tau}^{1-\alpha} - c_{\tau} - k_{{\tau+1}} + (1 - \delta) \cdot k_{\tau} \notag \right] \\ +& \dotsb \left . \right. \end{aligned}

Instead of the t symbol we used \tau to represent one specifit time period. How would the derivative by the k_{\tau} looked like? We would cancel everything from the first part of the equation because there is no k_{\tau} in any utility function. We would also delete all condition except the two ones multiplied by \lambda_{\tau-1} and \lambda_{\tau} . We also cancel all parts which don’t contain k_{\tau} . So at the end, we just need to compute the derivative of

 \frac{\partial \mathscr{L}}{\partial k_\tau} = \left[ \lambda_{\tau-1} \left( k_{{\tau}} \right) + +\lambda_{\tau} \left( A \cdot e^{z_{\tau}} \cdot k^{\alpha}_{\tau} \cdot n_{\tau}^{1-\alpha} - c_{\tau} + (1 - \delta) \cdot k_{\tau} \notag \right) \right]^{'}_{k_{\tau}} = 0 \, .

The deravation is quite easy – we just use a basic derivative rules \left( x^a \right)^{'}_{x}  = a \cdot x^{x-1} and \left( \ln x \right)^{'}_{x}  = \frac{1}{x} . So the result is

\frac{\partial \mathscr{L}}{\partial k_\tau} = \lambda_{\tau-1} + \lambda_{\tau} \cdot \left[ A \cdot e^{z_{\tau}} \cdot k^{\alpha - 1}_{\tau} \cdot \alpha \cdot n_{\tau}^{1-\alpha} + (1 - \delta) \right] =0 \, .

This is the same formula as we computed above, only with different time index. We could compute the derivation for any other time period. So we can continue with the computing. As you can see, we have Lagrange multipliers \lambda_{t-1} and \lambda_{t} . It would be nice to substitute the multipliers. So let’s derivate the Lagrange equation by the conspumption. First, we will derive the formula by c_t :

\frac{\partial \mathscr{L}}{\partial c_t} = \mathbb{E}_0 \beta^t \cdot \frac{1}{c_{t}} - \lambda_t = 0 \, .

We can rewrite the result as

\lambda_t =  \mathbb{E}_0 \beta^t \cdot \frac{1}{c_{t}} \, .

Now, we will derive the Lagrangian formula by c_{t-1} :

\frac{\partial \mathscr{L}}{\partial c_{t-1}} = \mathbb{E}_0 \beta^{t-1} \cdot \frac{1}{c_{t-1}} - \lambda_{t-1} = 0 \, .

Again, we can rewrite the result as

\lambda_{t-1} = \mathbb{E}_0 \beta^{t-1} \cdot \frac{1}{c_{t-1}} \, .

Now we can make the substitution:

\mathbb{E}_0 \beta^{t-1} \cdot \frac{1}{c_{t-1}} + \mathbb{E}_0 \beta^t \frac{1}{c_{t}} \cdot \left[ A \cdot e^{z_{\tau}} \cdot k^{\alpha - 1}_{\tau} \cdot \alpha \cdot n_{\tau}^{1-\alpha} + (1 - \delta) \right] =0 \, .

Afther a minor updates, we get formula named Euler equation. Euler equation defines a relation between marginal utility in one period and marginal utility in following period, which is increased by marginal productivity of capital and decreased by depreciation of capital. The household has a choice: it could either invest one unit of consumption goods to consume more in the future or it could consume it now. The household compare its utility of consuming the unit with present value of utility of consuming it in the next period. For example, if the household has a big consumption now and there is only a small amount of capital in production, it would be probably advantageous for the household to postpone its consumption. To be more precise, the household postpones consumption of one unit of good until the marginal utility of consumption equals expected present value of marginal utility of consumption in the future, which is increased by marginal production of capital and decreased by capital depreciation.

\frac{1}{c_{t-1}} = \mathbb{E}_{t-1} \beta \frac{1}{c_t} \cdot \left[ A \cdot e^{z_{\tau}} \cdot k^{\alpha - 1}_{\tau} \cdot \alpha \cdot n_{\tau}^{1-\alpha} + 1 - \delta \right]

The last derivative to compute is the derivative by n_t .

\frac{\partial \mathscr{L}}{\partial n_t} = \beta^t \cdot ( - \psi ) + \lambda_t \cdot [ A \cdot e^{z_t} \cdot k^{\alpha}_t \cdot n_t^{-\alpha} \cdot (1 - \alpha ) ] = 0  \, .

We do easy modifications to get following formula:

\beta^t \cdot  \psi  = \lambda_t \cdot [ A \cdot e^{z_t} \cdot k^{\alpha}_t \cdot n_t^{-\alpha} \cdot (1 - \alpha ) ]  \, .

As we know, \lambda_t =  \mathbb{E}_0 \beta^t \cdot \frac{1}{c_{t}} \, . So we will make a substitution:

\beta^t \cdot  \psi  = \beta^t \cdot \frac{1}{c_{t}} \cdot [ A \cdot e^{z_t} \cdot k^{\alpha}_t \cdot n_t^{-\alpha} \cdot (1 - \alpha ) ]  \, .

Here we have the final formula:

\psi  = \frac{1}{c_{t}} \cdot [ A \cdot e^{z_t} \cdot k^{\alpha}_t \cdot n_t^{-\alpha} \cdot (1 - \alpha ) ]  \, .

The household increase amount of work until marginal utility of consumption multiplied marginal production of labor equal marginal disutility of work. This equation gives us amount of labor in the production.

We are finished with the manual computing, now we will write the model into Dynare.

Typing model in Dynare

Dynare files have multiple sections: preamble, model, steady state or initial value, shocks and computation.

Preamble

The preamble contains a list of variables and parameters of the model. We also need to specify parameters values because, as we said, the parameters values are fixed and we need to specify them. The values are usually based on statistical or econometric studies etc. For our model, let’s set the following values:

  • \alpha = 0.5 \, ,
  • \beta = 0.99 \, ,
  • \delta = 0.05 \, ,
  • \psi = 1.5 \, ,
  • \rho = 0.9 \, ,
  • \sigma = 0.01 \, ,
  • A = 1 \, .

Now we will write it into dynare.

var c k n z;
varexo e;
parameters beta psi delta alpha rho sigma A;
alpha = 0.5;
beta = 0.99;
delta = 0.05;
psi = 1.5;
rho = 0.9;
sigma = 0.01;
A = 1;

Model

This part contains the first order conditions equations. We have four equations which we need to solve the model. The first one is the Euler equation. I just shift it for one period into the future:

\frac{1}{c_{t}} = \mathbb{E}_{t} \beta \frac{1}{c_{t+1}} \cdot \left[ A \cdot e^{z_{\tau}} \cdot k^{\alpha - 1}_{t+1} \cdot \alpha \cdot n_{t+1}^{1-\alpha} + 1 - \delta \right]

The second equation gives us the amount of labor.

c_{t} \cdot \psi  = [ A \cdot e^{z_t} \cdot k^{\alpha}_t \cdot n_t^{-\alpha} \cdot (1 - \alpha ) ]  \, .

The third equation is the law of motion for capital. Before we will write it into the Dynare, we need to shift it one period in the past. It is necesary for Dynare to identify the state variable – which is the amount of kapital. This change doesn’t change interpretaion of the model.

c_{t-1} + k_{t} - (1 - \delta) \cdot k_{t-1} = A \cdot e^{z_{t+1}} \cdot k^{\alpha}_{t-1} \cdot n_{t-1}^{1-\alpha} \, .

The last one is the definition of stochastic shock.

z_t = \rho_z \cdot z_{t-1} + \epsilon_t \, ,

model;
(1/c) = beta*(1/c(+1))*(alpha*A*exp(z)*(k(+1)/n(+1))^(alpha-1) + 1 - delta);
psi*c = (1-alpha)*A*exp(z)*((k/n)^alpha);
c(-1) + k - (1 - delta) * k(-1) = A*exp(z(-1))* k(-1)^alpha * n^(1-alpha);
z = rho*z(-1)+e;
end;

Initial value

We want Dynare to compute the steady state. To compute steady state Dynare needs some initial values to start the calculation. The initial value should be close to real steady state values. The maxit value set

initval;
k = 9;
c = 0.76;
n = 0.3;
z = 0;
e = 0;
end;
steady(maxit = 10000);
check;

Shocks

The is a description of the exogenous shock.

shocks;
var e = sigma^2;
end;

Computation

This command tells Dynare to compute impulse response functions.

stoch_simul(irf=40);

Now is our .mod file complete! We will discuss its results in another article.

Complete code

Here you can see the complete code of our example.

var c k n z;
varexo e;
parameters beta psi delta alpha rho sigma A;
alpha = 0.5;
beta = 0.99;
delta = 0.05;
psi = 1.5;
rho = 0.9;
sigma = 0.01;
A = 1;
model;
(1/c) = beta*(1/c(+1))*(alpha*A*exp(z)*(k(+1)/n(+1))^(alpha-1) + 1 - delta);
psi*c = (1-alpha)*A*exp(z)*((k/n)^alpha);
c(-1) + k - (1 - delta) * k(-1) = A*exp(z(-1))* k(-1)^alpha * n^(1-alpha);
z = rho*z(-1)+e;
end;
initval;
k = 9;
c = 0.76;
n = 0.3;
z = 0;
e = 0;
end;
steady(maxit = 10000);
check;
shocks;
var e = sigma^2;
end;
stoch_simul(order = 1, irf=40);

Trackbacks and Pingbacks

[…] the previous article, we described the simple DSGE model and prepared source code to run this code in Dynare. Today we […]

Like

[…] previous articles, we went through a simple DSGE model. We computed first order conditions and steady-state equations and we ran a simulation in Dynare. Maybe some of you were confused by […]

Like

Leave a Reply

Name and email address are required. Your email address will not be published.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

You may use these HTML tags and attributes:

<a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <pre> <q cite=""> <s> <strike> <strong> 

%d bloggers like this: