# Running simple DSGE model in Dynare

Dynare is a quite sophisticated software for economic simulations. It is mainly used for estimations of Dynamic Stochastic General Equilibrium Models (DSGE models). DSGE models are considered the state-of-the-art of economic simulations and predictions and they are used by plenty of central banks and ministries of finances 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

Let me show you 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 let me explain what this actually means.

We assume that household’s utility depends on both amounts of consumption and amount of free time. The household splits its time between 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 has to balance these two variables to maximize its utility.

The decisions are made in multiple time periods and the decision in one period affects the situation in following time periods. In our model, for example, when the household makes savings in some time period, it gets interests from these savings in the future time periods. This makes our model dynamic. The household maximizes its utility in infinite time horizon. The infinite time horizon is used for making the solution of the model easier (this may sound unlikely to you but it will be proved below). The usual interpretation of this fact is that the household takes care of its children, grandchildren etc. What does the present value mean? Close time periods are more important for the household than the distant ones. Or we could say that the household prefers current utility more than the future one. 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 gets 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 to the future. So if the household makes some economic decision, it needs to have some expectation about its outcome. The household makes the decision to maximize the expected value of the future utility. We add a $\mathrm{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 \mathrm{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 behaviour.

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 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, 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 $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}$. We also add a constant $A$, which can be used to fit the production function to some real economy. 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 constraint 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 modelled 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 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 $t$ will be denoted by $\lambda_t$.

$\mathscr{L} = \mathrm{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+1}} = -\lambda_{t} + \lambda_{t+1} \cdot \left( A \cdot e^{z_{t+1}} \cdot k^{\alpha - 1}_{t+1} \cdot \alpha \cdot n_{t+1}^{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} &= \mathrm{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] \\ +& \dotsb \left . \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+1}$ looked like? We would cancel everything from the first part of the equation because there is no $k_{\tau+1}$ in any utility function. We would also delete all constraints except the two ones multiplied by $\lambda_{\tau}$ and $\lambda_{\tau+1}$. We also cancel all parts which don’t contain $k_{\tau+1}$. So at the end, we just need to compute the derivative of

$\frac{\partial \mathscr{L}}{\partial k_{\tau+1}} = \left[ \lambda_{\tau} \left( k_{{\tau+1}} \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} + (1 - \delta) \cdot k_{\tau+1} \notag \right) \right]^{'}_{k_{\tau+1}} = 0 \, .$

The derivative is quite easy – we just use 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+1}} = \lambda_{\tau} + \lambda_{\tau+1} \cdot \left[ A \cdot e^{z_{\tau+1}} \cdot k^{\alpha - 1}_{\tau+1} \cdot \alpha \cdot n_{\tau+1}^{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}$ and $\lambda_{t+1}$. 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} = \mathrm{E}_0 \beta^t \cdot \frac{1}{c_{t}} - \lambda_t = 0 \, .$

We can rewrite the result as

$\lambda_t = \mathrm{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}} = \mathrm{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} = \mathrm{E}_0 \beta^{t+1} \cdot \frac{1}{c_{t+1}} \, .$

Now we can make the substitution:

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

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 $t$, so it also formulates its expectations at time $t$. So we use $latex \mathrm{E}_{t}$ at the formula to

$\frac{1}{c_{t}} = \mathrm{E}_{t} \beta \frac{1}{c_{t+1}} \cdot \left[ A \cdot e^{z_{t+1}} \cdot k^{\alpha - 1}_{t+1} \cdot \alpha \cdot n_{t+1}^{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 = \mathrm{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 constraints equations. We have four equations which we need to solve the model. The first one is the Euler equation:

$\frac{1}{c_{t}} = \mathrm{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;
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)^(1-alpha);
z = rho*z(-1)+e;
end;
initval;
k = 9;
c = 0.76;
n = 0.3;
z = 0;
e = 0;
end;
stoch_simul(order = 1, irf=40);