**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 with 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 . The lower values of means that the household is more impatient. Let’s remark time period as . So if we multiply utility in the time period by , 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 symbol to represent the expected value. The symbol says that the expectations are being made into zero time period.

This is the formula of household decision problem:

We have two kinds of “symbols” in the formula. The symbols with 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 index are **parameters**. The values of the parameters are** constant for all-time periods** and they describe the household behaviour.

Let’s define the variables and parameters in the utility function. The represents an amount of consumed goods at period, is an percentage of disposable time spent by work at period, is natural logarithm function. 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 is given by the difference between utility gain from the consumption and disutility of time spent by work: . Then this utility is discounted to its value at time 0 by multiplying by . 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, the 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 is defined by variable . The amount of work is given by variable which already appeared in the utility function.

We will use a Cobb-Douglas production function. This production function has one parameter . This parameter represents the output elasticity of capital. So the production function formula is: . The production function is multiplied by . We also add a constant , which can be used to fit the production function to some real economy. The is a value of a stochastic shock at time . 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 constraint as

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 modelled by an autoregressive process. Let’s look at the process formula:

The shock value is given by its previous value and by random value . A higher value of the parameter means that the present value of the shock is more dependent on its previous value. The stochastic variable is denoted by . 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 constraints **(FOC). Generally, the first-order constraints give us points where *can* be an extreme. The second order constraints 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 constraints. 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 constraints give us values which maximize the household utility.

We will use the method of Lagrangian multipliers to solve the model. At the beginning, we have to set up a **Lagrangian function**. This equation contains the utility function and the constraion equation multiplied by **Lagrangian multiplier**. We created only one equation, however this equation must be valid at every time period. So, we have an **infinite number of constraints**. And thereby, we need infinite number of Lagrangian multipliers. The Lagrangian multiplier for the period will be denoted by .

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 and we will set this derivative eqaul to 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.

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

The derivative is quite easy – we just use basic derivative rules and . So the result is

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 and . 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 :

We can rewrite the result as

Now, we will derive the Lagrangian formula by :

Again, we can rewrite the result as

Now we can make the substitution:

Afther a minor updates, we get formula named **Euler equation**. Euler equation defines **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. The household makes the decision at time , so it also formulates its expectations at time . So we use $latex \mathrm{E}_{t} $ at the formula to

The last derivative to compute is the derivative by .

We do easy modifications to get following formula:

As we know, So we will make a substitution:

Here we have the final formula:

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:

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 constraints equations. We have four equations which we need to solve the model. The first one is the Euler equation:

The second equation gives us the amount of labor.

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.

The last one is the definition of stochastic shock.

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 […]

LikeLike

[…] 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 […]

LikeLike