联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Matlab编程Matlab编程

日期:2019-07-31 10:09

International Macroeconomics

Contents

1 Programming I: Introduction to Matlab 1

2 Programming II: Use of Dynare to Simulate Dynamic Models 8

2.1 Baseline Theoretical Example . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2.2 Steady Operationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

2.3 Back to Dynamic Equations in Example Model . . . . . . . . . . . . . . . . 12

2.4 Summary of Parameters and Steady State Values . . . . . . . . . . . . . . . 13

2.5 Dynare Timing Convention . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

2.6 The Dynare Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

2.6.1 Declaring Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

2.6.2 Declaring Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

2.6.3 Declaring the Model . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

2.6.4 Initial Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

2.6.5 Shocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

2.6.6 Stochastic Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

2.6.7 Why Do All This? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

2.6.8 Some Additional Things to Note . . . . . . . . . . . . . . . . . . . . . 18

2.7 Matlab Code for General Control and Obtaining Results . . . . . . . . . . . 20

1 Programming I: Introduction to Matlab

This section is designed as a “do it yourself” part so that you can familiarize yourself

with the use of Matlab.

Open Matlab, and you will get something like in the figure below (exact appearance

varies by Matlab version).

1

? Note that the directory to which Matlab is pointing is on top in the middle, in the case

of the example above C:, and you can always change the directory to operate Matlab

from any directory that you wish. How to change the directory may differ depending

on the version of Matlab. In the case in the figure above there is a dropdown menu

just right C: (see the downward pointing arrow on the right of the directory path).

In the top left part of the Matlab interface shown above there is the “New Script”

button. Pressing this, or also going through File → New → Script you can create a

new .m file, which is what is used to run Matlab code.

In the command window (in the case of the figure above it’s right in the middle) you

can see the command prompt after which Matlab code is introduced.

A few icons to the right of the New Script button is the New Variable butt, which, of

course, is used to create a new variable.

We’ll go over all of the preceding Matlab notes in greater detail following below.

For now, note from the figure above that there are 5 panels: (1.) Current Folder

(top left); (2.) Details (bottom left); (3.) Command Window (in the middle); (4.)

Workspace (top right); (5.) Command History (bottom right).

– You can move around these panels and organize them in whatever way you like,

but these are the most useful windows to keep handy.

2

– If any of these windows is not visible you can activate them by using the Layout

button, which in the case of the figure above is to the right of the New Variable

button.

Matlab interprets all variables (or parameters) as matrices.

In the command window type (all codes I indicate should be written in Matlab verbatim)

X = 1 and press enter (note that the variable X, which you just created, now

appears in the Workspace window). Matlab interprets this as a 1 × 1—that is, 1 row

and 1 column).

Note: Matlab understands capitalization. For example, type in the command prompt

x = 1 and you’ll see that now both X and x exist as different variables.

Now, let’s create a row vector of 1 × 5 (i.e., a vector with 1 row and 5 columns). Write

in the command prompt Y = [1 2 3 4 5] and press enter.

To obtain instead a column vector of 5 × 1 (i.e., a vector with 5 rows and 1 column)

type in the command prompt, for instance, Z = [6; 7; 8; 9; 10] (in this case the

semi colons serve to indicate to Matlab that we are moving on to a new row in the

vector).

You can transpose a vector or a matrix. Type Z = Z’ in the command prompt and

press enter (now Z is a vector with 1 row and 5 columns; note that the name Z now

refers to the new form of this variable and no longer to its original column vector

format).

Now, let’s create a 2×5 matrix. Write in the command prompt W = [1 2 3 4 5; 6 7

8 9 10] and press enter. Given what we’ve done so far we could have also generated

W by writing in the command prompt W = [Y; Z] (try it).

If you want to create some variable like we did before but we don’t want the result

to appear in the command window, simply put in a semi colon after the relevant

command. For instance, type W2 = [1; 2]; in the command prompt, press enter,

and then type W2 in the command prompt and press enter to “see” this vector in the

command window (so, in the preceding step we in fact created the vector W2 but its

statement was suppressed from the command window at the time of its creation).

Of course, as long as the dimensions of a matrix allow it, you can perform standard

algebraic operations using Matlab.

Let’s start all over. In the command window type clear all and press enter; note

that all variables that were in the workspace panel have been cleared. Yet, there’s still

clutter in the command window. To get rid of this clutter type clc and press enter.

Now, type in the command prompt X = [1; 2; 3]; and press enter; and do the same

for Y = [4; 5; 6]; and Z = [7 8 9];

Finally, type in W = 2; and press enter.

3

What we’ve done just now is create two 3 × 1 matrices, one 1 × 3 matrix, and a scalar

matrix.

Let’s perform some addition. Write in the command prompt XplusY = X + Y and

press enter.

You can only add matrices that have the same number of rows and columns.

For instance, write in the command prompt XplusZ = X + Z and press enter (see what

happens).

The exception to the preceding addition rule occurs in the case of scalar matrices. For

example, write in the command prompt XplusW = X + W and see what happens.

– These two matrices have different dimensions, but since W is a scalar matrix Matlab

adds W to each element of X.

Subject to matrix algebra rules, you can perform any algebraic operation that you

want, including:

– Addition ( + );

– Subtraction ( -- );

– Multiplication ( * );

– Division ( / );

– and exponentiation ( ^ ).

In Matlab there is a difference between multiplication (or division or exponentiation)

“per se” and:

– Multiplication (or division or exponentiation) “element by element.”

– We now proceed to explore this difference.

Write in the command prompt X = [X Y] and press enter; note that we have created

a new matrix X that is 3 × 2 and replaces the old version of X but using first that old

version to create its new version X.

Let’s move on to other fun stuff.

Write in the command prompt Z(1,4) = 10 and press enter; what we’ve just done is

add an element to Z by telling Matlab to put in the first row, fourth column of Z the

number 10.

Now, write in the command prompt Z(2,1:3) = Z(1,2:4) and press enter; you’ll see

that what we’ve just done is create a second row for Z telling Matlab to fill the second

row, columns 1 through 3 of Z (the colon in Matlab language roughly means “through”

and in this case “from 1 to 3”) with the elements of row 1, columns 2 through 4 of Z.

4

Note also that any element that one does not explicitly assign content to will automatically

be filled with a 0 (in our example the element in the second row, fourth column

of Z).

Write in the command prompt XtimesZ = X*Z and press enter.

The preceding step is “multiplication per se.” X is 3 × 2 matrix and Z is a 2 × 4 so the

result of multiplying them is a 3 × 4 matrix.

For matrix multiplication to be possible, the matrix on the left hand side of the multiplication

has to have the same number of columns as the matrix on the right hand

side of the multiplication.

For example, type in the command prompt XtimesY = X*Y and press enter (see what

happens).

Just like with addition, the exception to this multiplication rule is when we’re dealing

with multiplication and one of the matrices involved is a scalar matrix. To see this

issue write in the command prompt XtimesW = X*W (note that each element of the

matrix X has been multiplied by the scalar W and we obtain as a result a 3 × 2 matrix).

Up until now we’ve been creating matrices and calculating stuff using the method

of naming said stuff prior to creating but if we just want to see the result of some

operation simply type, for instance, X*Z and press enter.

Now, write X*X and press enter; you’ll see that you obtain an error message since the

dimensions of the matrix do not allow multiplication per se.

Instead, type in the command prompt X.*X and press enter. What’s happened now is

that the operation does take place given the .* notation, which indicates that multiplication

must be carried out “element by element” from the point of view that each

element of a matrix is multiplied by the same corresponding element in the other

matrix.

– Of course, for element by element multiplication to be feasible the matrices that

are being multiplied have to have the same number of rows and columns.

All of the preceding regarding the difference between “per se” and “element by element”

also applies to division and exponentiation.

We’ll wrap up with a few additional things that are useful to know.

If you want to know the size of a matrix write in the command prompt, for instance,

size(Z), which will yield as a result the number of rows and columns (in that order)

of the matrix Z.

Continuing with our examples, if you want to see the elements in the first row of the

matrix Z type in the command window Z(1,:) and press enter.

5

– In this case the colon indicates that all elements of the indicated column are to

be selected.

Similarly, if you want to see all the elements of the second column of Z type in the

command prompt Z(:,2) and press enter.

Of course, if you wanted to see all elements in the first row, columns 2 through 4 of Z

you write in the command prompt Z(1,2:4) and press enter.

– Etc.

Finally, if you want to create a matrix of zeros with n rows and m columns, where n

and m are integers, you write in the command prompt zeros(n,m) and press enter.

Similarly, if you want to create a matrix of “ones” with n rows and m columns you

write in the command prompt ones(n,m) and press enter.

Instead of writing all of the previous in the command window it’s much better to create

an “.m file” to run things for us. Let’s go ahead and do that. Go to File → New →

Script and you’ll see a new window pop up like in the figure below.

This new window/file will be saved automatically with the extension .m and is known

as an “.m file.”

Now, go to “Save” and create a folder wherever you like that’s called, for example,

“Intro to Matlab” and save in this folder your .m file giving it the name, for instance,

“Practice”

6

Now, change the Matlab directory to point to the new folder that you just created.

Note that in the .m file there is a green arrow around the top middle part (see the

figure above). By clicking this arrow the .m file will “run.”

As an example, copy and paste the following (which is basically all that we’ve done so

far) in your .m file, save it, and then press the noted green arrow so that the file runs.

clear all

clc

%Create a square matrix X

X = [1 2 3; 4 5 6; 7 8 9];

%Find out what size X has

size(X)

%Create a matrix that’s equal to the third row of X

XColThree = X(:,3);

%Create a matrix that’s equal to the first row, second column of X

XRowsOneAndTwo = X(1:2,:);

%Obtain X squared

X^2

%Obtain the element-by-element square of X

X.^2

%Create a column matrix Y

Y = [10; 11; 12];

%Multiply X and Y

X*Y

%Create a 1x2 matrix of zeros

zeros1 = zeros(1,2);

%create a 2x2 matrix of ones

ones1 = ones(2,2);

In the code above some variables have been suppressed from the command window

using the semi colon notations and other have not, on purpose, just so you can see the

different combos that can be obtained.

Note that everything preceded by % is interpreted by Matlab as a comment and ignored

when running the code.

Our .m file from above creates a bunch of variables in the Workspace panel.

Find the “Save Workspace” button, which is by the “New Variable” button. This

button serves to save the variables that are in the Workspace (alternatively you can

go to File → Save Workspace as...).

Save your workspace in your Intro to Matlab folder giving it the name, for instance,

results.mat (note that the extension .mat is used automatically to save these types of

files).

7

Now, close your .m file and write and enter clear all and then clc

To load the .mat file that you created where you have your saved variables you can

now type load results

As you’ll see, all the variables that we had created are once more in the workspace.

Also, if you want to reopen your .m simply go to File → Open and select the file from

the folder you saved it in.

Similarly, instead of using the load command to load your .mat file you can go to File

→ Open and open your file by locating it where you saved it.

2 Programming II: Use of Dynare to Simulate Dynamic

Models

Dynare is not a program in itself, but, rather, a collection of Matlab Codes.

To start working with Dynare, open Matlab and check what version of Dynare you

have installed, for example, 4.5.7, and type the following in the Matlab command

promptaddpath c:\dynare\ 4.5.7\matlab and press enter.

To use Dynare you have to create .mod files (these files can be obtained by opening a

script in Matlab and then saving it using the extension .mod instead of .m).

Suppose that our Dynare file is called RBC.mod

Make sure that this file is saved where the Matlab directory is pointing to.

To run the Dynare file, simply type in the Matlab command prompt

dynare RBC

and press enter.

Note: for more examples and manuals you can go to the Dynare website: www.dynare.org.

I also posted a few lecture notes from other professor to the class website.

2.1 Baseline Theoretical Example

We’ll use as an example a typical closed economy RBC model developed from the point

of view of a centralized planning problem.

The planner’s problem is:(1)

where: C is consumption; H is work hours; K is the capital stock; Et

is the expectation

operator; β ∈ (0, 1) is the subjective parametric discount factor; γ > 0 is a parameter

that governs the disutility of labor; ε is the Frisch elasticity of labor supply; Y is

output; I is investment; G ≥ 0 is the parametric value of government consumption;

δ > 0 is the depreciation rate of capital; A is technology; α ∈ (0, 1) is a parameter;

ρ ∈ (0, 1) is the productivity process persistence parameter; and ξ ~ N(0, σ2). Of course, the constraints relevant to the optimization problem can be combined into

a single constraint by getting rid of investment and output in the first constraint:

Ct + G + Kt+1(1 δ)Kt = AtH. (2)

9

Combining the first and second first order conditions:(3)

Combining the first and third first order conditions:(4)

Jointly, equations (1), (2), (3), and (4) are a system of 4 equations in the 4 key variables:

A, C, H, and K. In steady state, these equations imply that:

– NOTE: Steady state is a situation in which any variable xt has the same

value across periods, i.e., xt = xt+k = x for any integer value of k.

– Since A simply becomes a parameter, in fact the system reduces to one of 3

equations in the 3 key unknowns: C, H, and K. Indeed, note given steady state:

ln At = ρ ln At?1 + ξt

SS

→ ln A = ρ ln A + 0,

i.e., in stead state the shocks to the productivity process are nill, meaning that

in steady state

(1 ρ) ln A = 0 → ln A = 0 → A = 1.

10

Note also that Y as a variable is not essential to solve the system because it is itself

a function of the system’s key variables. So, we can treat Y as a “definition:” Similarly, investment doesn’t appear in any useful place in steady state, so we can treat

it as a definition as well. Using the capital equation of motion: I ≡ δK.

Also, since this is a competitive model, if we were interested in backing out the rental

rate and the wage we could define. Rt ≡ (1 α) AtHα

Typically in these models all variables are normalized by a country’s population, and,

as we’ve assumed implicitly throughout, the price of consumption, and therefore output

and investment, is normalized to 1.

2.2 Steady Operationalization

In order to operationalize the model in steady state system of equations (5) through

(7) we need to assign values to the parameters: β; α; γ; δ; G; and ε. That is, we need

to calibrate the model.

The choice of certain parameters depends on the timing frequency that we assume for

the model’s operationalization. We’ll focus on a quarterly calibration.

The choice of certain parameters is obvious. For example, the Frisch elasticity is usually

set at ε = 4 so that results are consistent with empirical evidence.

Also, cross country data suggest that in general α = 2/3, i.e., the share of labor in

total income is two thirds.

Assuming that β =1

, where r is the real interest rate and historically equal to about

5% in the U.S., then given that the calibration is at quarterly frequency we obtain a

value of r such that: The quarterly depreciation rate in the U.S. is about δ = 0.025, so we assume this value.

Finally, the calibration of the parameters γ and G is not necessarily obvious.

– Recall that the parameter γ affects the disutility of labor, so we can use this

parameter to achieve some target regarding hours worked.

11

In the U.S., since 1960 average work hours per population are approximately

1700 per year. The number of hours in a year is approximately 8760. So,

we can normalize total work hours in a year to 1 and impose that in steady

state H is equal to the average fraction of work hours to total available hours.

That is, one of our calibration target will can be to choose γ such that H =

1700/8760.

Also, the parameter G reflecting government consumption will affect directly

the level of private consumption C. In the U.S. since 1960 the average ratio

of private consumption to output C/Y is approximately 0.63, so our other

calibration target is C/Y =0.63. G will be chosen to hit this target.

– With a simple enough model like this one, you can solve for the implied

values of γ and G necessary to hit these targets by hand. The resulting

values are γ = 8.2156 and G = 0.0847. In a more complex model you would

use Matlab’s algorithm for solving systems of nonlinear equations to solve for the

values of γ and G corresponding to the calibration targets (we will not be doing

this in this class).

– As an example, and to make my life easier, I solve for these values using Matlab’s

“fsolve” algorithm. I wrote a function called “RBCSTEADY.m”, which is called

by “FINDSTEADY.m”. Both can be found in Week 7 folder at class website.

You don’t have to use them, but they are useful and you can always adapt these

codes to more complicated problems.

2.3 Back to Dynamic Equations in Example Model

Recall the key equations from the dynamic version of the model that we used as an

example in the previous section:

and the definition:

It ≡ Kt+1 + (1 δ)Kt.

NOTE: I modified the third equation only for the purposes of demonstration and so

that it actually makes sense to have investment in here, otherwise investment can be

completely done without as far as solving the model is concerned.

12

2.4 Summary of Parameters and Steady State Values

We’ll use the parameters chosen before. In addition, as is standard in the literature

we’ll assume that at quarterly frequency ρ = 0.95 and σ = 0.01. So, we have:

Parameters

ε α β δ γ ρ σ G

4 2/3 0.988 0.025 8.2156 0.95 0.01 0.0847

We’ll also need for reference the steady state values from the calibrated version of the

model. As can be shown using pen and paper:

Steady State Values

C H K Y

0.3662 0.1941 5.2167 0.5813

and remember that in steady state A = 1.

2.5 Dynare Timing Convention

Dynare is set up with an internal timing convention that requires that we rewrite the

model slightly. In particular, Dynare requires that any predetermined variable (that

is, a state variable, such as the capital stock) be lagged one period. So, for instance,

if in the model a state variable appears with the subscript t then we need to rewrite

this with the subscript t ? 1, and if a state variable appears with the subscript t + 1

then we need to rewrite this with the subscript t. All told, this timing convention is

how we tell Dynare that a state variable is such.

In our model, the only state variable is the capital stock.

So, as far as coding in Dynare goes, our key equations need to be written as such:

as well as the definition:

It ≡ Kt(1 δ)Kt1.

With these “proper” equations in hand we’re ready to start writing our Dynare code

(which, recall, takes place using a .mod file).

13

Note: Just like in Matlab, if an expression starts with % Dynare will ignore it when

solving the model.

2.6 The Dynare Code

2.6.1 Declaring Variables

The first thing we have to do is declare the model variables.

Dynare automatically groups variables as follows: static variables (that is, variable

that only enter the system with a subscript t); purely predetermined variables (that

is, variables that only enter the system with subscripts t and t ? 1); variables that are

both predetermined and forward looking (that is, variables that enter the system with

subscripts t 1, t, and t + 1); and variables that are both static and forward looking

(that is, variables that only enter the system with subscripts t and t + 1).

To decare the endogenous variables we write var followed by these variables.

In our example:

var C H K Z Y I; (The semi colon is necessary!!!).

Then, we declare the exogenous variables, which in our case only amounts to the error

term in the AR(1) process ξ (xi) starting with the expression varexo.

In our example:

varexo xi; (The semi colon is necessary!!!).

2.6.2 Declaring Parameters

Immediately after declaring the variables and we declare the parameters starting with

the expression parameters and followed below by the parameter values. (In all cases

the semicolon is necessary!!!).

In our example:

parameters e a B d g rho sigma G;

e = 4;

a = 2/3;

B = 0.988;

d = 0.025;

g = 8.2156;

rho = 0.95;

sigma = 0.01;

G = 0.1025;

14

2.6.3 Declaring the Model

Having declared the variables and parameters the next step is declaring the model.

To do so, we write model followed by the model equations and finalizing with the

expression end

– The model that we’re solving is nonlinear, so Dynare will solve it using a linear

approximation whose order we can choose.

To write down the model, if a variable x appears with the subscript t you just write

x. If a variable x appears with the subscript t ? 1 you write down x (?1). And if a

variable x appears with the subscript t + 1 you write x (+1).

For our example we declare the model as follows. (The semi colons are necessary!!!).

model;

I = K - ( 1 - d )*K(-1);

Y = A*( H^a )*( K(-1)^( 1 - a ) );

log( A ) = rho*log( A(-1) ) + xi;

C + I + G = Y;

H = ( ( a/g )*( Y/C ) )^( e/( 1 + e ) );

1/C = B*( 1/C(+1) )*( ( 1 - a )*Y(+1)/K + ( 1 - d ) );

end;

One thing to note is that the IRFs that Dynare generates are in terms of level deviations

from steady state. In other words, the IRF for a variable x, x

IRF , is graphed as

x

IRF = xt x

SS, where SS denotes steady state.

However, often times what one is really interested in seeing, and what is usually presented

in research, is IRFs in terms of percent deviations from steady state. This

problem can be solved doing one of these two:

1. Declare the model as above, then after dynare runs, go and find dynare’s stored

values for each variable, then take the logarithm of those values and steady state

values, and plot the difference.

2. Or use the following trick and write every variable as if they are logs. This

way, you will not have to do any extra calculation. Resulting impulse response

functions will be in terms of percentage deviations. Thus, I will write the model

as follows:

15

2.6.4 Initial Values

Dynare will (try to) solve for the model’s steady state and use it to initialize the model

simulation. However, Dynare’s algorithm for finding steady states is very sensitive to

initial values, which is why we first find the steady state using pen and paper, or in

the case of a complex model using Matlab.

To declare the initial values we start with the command initval, then we write down

the initial values, and finally we write end as follows. (The semi colons are necessary!!!).

initval;

C = 0.3662;

H = 0.1941;

K = 5.2167;

A = 1;

Y = 0.5813;

end;

Because I declared the variables as logs, initial values should also be declared as logs:

2.6.5 Shocks

The next step is to specify the variance of the productivity shocks. This part of the

code starts with the command shocks, followed by the variance specification, and

ending with the statement end. (The semi colons are necessary!!!).

16

shocks;

var xi = sigma^2;

end;

In the next step we write steady so that Dynare calculates the steady state of the

model’s endogenous variables as follows. (The semi colon is necessary!!!).

steady;

2.6.6 Stochastic Simulation

In order to go ahead with the model’s stochastic simulation we write down the command

stoch simul, which solves the model, generates IRFs, etc.

Dynare automatically uses a second order approximation to simulate the model.

There are several options that can be used after this last command. However, if you

only write down stoch simul you will obtain the following:

1. Steady state values of the endogenous variables;

2. A summary of model variables by type;

3. A covariance matrix of the shocks (in our example we would just obtain a scalar);

4. The functions, as they’re known, of policy and transition;

5. First and second theoretical moments;

6. A matrix of theoretical correlations;

7. Theoretical autocovariances up to order 5;

8. IRFs in response to a 1 standard deviation in the productivity process (Dynare

uses a Choleski decomposition).

? In our example we’ll do the following. (The semi colon is necessary!!!).

stoch simul(order=1,irf=40,periods=240); order = 1 makes Dynare solve the model

using a first order approximation. (Unless a model is exceedingly complicated it is not necessary

to use a second order approximation, which, recall, is what Dynare does automatically);

irf=40 makes Dynare generate and graph impulse responses for 80 periods (in our case, quarters);

periods=240 makes Dynare generate simulated time series data for 240 periods (in our

case, quarters). If you want Dynare to minimize the amount of information that will appear

in Matlab’s command window following the simulation routine, use also noprint option.

17

2.6.7 Why Do All This?

Static models have closed form solutions, such as a typical micro optimization problem

in which you can solve explicitly for each variable.

Dynamic models do not have closed form solutions, unless they are imposed to be in

steady state; so, there is no way to solve the model explicitly for each variable other

than in steady state, which is ultimately why programs like Dynare come in. The true

solution to a dynamic model involves sequences of data implied by the model, i.e., time

series data.

And, to see if a dynamic model works we want to see if the data generated by the

model are consistent with their empirical counterpart.

Using empirical data we can obtain key statistical moments and, using a VAR, we can

also obtain IRFs.

In turn, using Dynare we can obtain the theoretical counterpart of these data and

therefore compare the performance of the model with the reality that it’s trying to

explain in order to gauge how good of a model we’ve come up with.

Even more importantly, if we are happy with how our dynamic model works,

then we can use the model as a laboratory for policy analysis, for instance,

understanding what happens when there is a change in productivity or, say,

government consumption.

2.6.8 Some Additional Things to Note

Given the Dynare timing convention, the IRFs that Dynare generates for predetermined

(i.e., state) variables show the evolution of these variables one period ahead.

So, in our example what we’ll see is the IRF for capital jumping at the time of the

productivity shock, while in fact since capital is a state variable it can’t move at the

moment of the shock. In the following section we’ll see how to correct for it.

In spite of the impact of Dynare’s timing convention for the IRFs that are generated,

the simulated data generated by Dynare is in the correct temporal order.

Once we run the Dynare code, Dynare saves in Matlab’s workspace the simulated time

series for variable under their name, for instance in the case of the artificial time series

for consumption you’ll see simply C, and the IRFs of each variable are stored as the

variable name followed by an underscore and the name of the shock that the variable

is responding to, so in our case the IRF for consumption will be C xi

Also, if you look at the set of variables generated by Dynare, you’ll be able to find the

steady state values under oo → dr -→ ys and the variables there appear in the same

vector column order as they do in the Matlab command window following Dynare’s

run.

18

Some items we get by running the dynare code:

1. Moments of the simulated variables:

2. Correlations between simulated variables:

3. Autocorrelations of simulated variables:

19

4. And of course Impulse Response Functions (IRFs):

2.7 Matlab Code for General Control and Obtaining Results

First I’m going to show how to use a Matlab script file to write a Dynare code and

then save it as a .mod file.

Second, we’ll run the Dynare code by writing, in our example, dynare RBCDynare in

Matlab’s command window (that’s the name I’ll give our Dynare file).

Then, instead of just writing in Matlab dynare RBCDynare to run the model we’ll create

an .m file (CONTROL.m to be more specific) that will serve the purpose of controlling

a bunch of additional things we’ll want to do, including running the Dynare file and

then generating other stuff of interest with the Dynare simulated data. In particular,

what we’ll do with the general control code is the following:

1. Make the Dynare code run;

2. Generate and graph the correct IRF for capital with it having the same length as

the other IRFs, that is, 40 periods;

3. Extract the steady states generated by Dynare;

20

4. Obtain the cyclical component of the variables Y , C, I, and H using an HP filter

with smoothing parameter 1600 (The Dynare code that we developed generates

data in levels and quarterly frequency; the HP filter code will be provided).

5. Generate graphs of the cyclical components of the variables using the Matlab

command subplot.

6. Generate a graph of the natural log of output along with its trend component.

7. Use the cyclical component of variables to create a table with:

(a) Their standard deviation;

(b) Their correlation with output;

(c) The one lag autocorrelation of each variable.

21


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp