2022-04-11 23:42:37 +02:00
\documentclass [aspectratio=169] { beamer}
2008-07-04 16:30:46 +02:00
\usepackage [utf8] { inputenc}
2015-06-08 14:55:05 +02:00
\usepackage [T1] { fontenc}
\usepackage { lmodern}
2022-04-11 23:42:37 +02:00
\usepackage { upquote}
2008-07-04 16:30:46 +02:00
\usepackage { amsmath}
2015-06-08 14:55:05 +02:00
\usepackage [copyright] { ccicons}
2008-07-04 16:30:46 +02:00
2012-06-13 18:40:03 +02:00
\usetheme { Boadilla}
2008-07-04 16:30:46 +02:00
2019-05-31 14:44:52 +02:00
\titlegraphic { \includegraphics { ../logos/dlogo.png} }
2018-06-06 15:22:12 +02:00
\title { The Dynare Macro Processor}
2019-05-31 14:44:52 +02:00
\author [S. Villemot, H.Bastani] { Sébastien Villemot \and Houtan Bastani}
\institute [CEPREMAP] { \includegraphics [scale=0.15] { ../logos/cepremap.jpg} }
2022-05-24 12:20:00 +02:00
\date { 3 June 2022}
2019-05-31 14:44:52 +02:00
\setbeamertemplate { title page}
{
\vbox { }
\begingroup
\centering
{ \usebeamercolor [fg] { titlegraphic} \inserttitlegraphic \par } \vskip 1em
\begin { beamercolorbox} [sep=8pt,center]{ title}
\usebeamerfont { title} \inserttitle \par %
\ifx \insertsubtitle \@ empty%
\else %
\vskip 0.25em%
{ \usebeamerfont { subtitle} \usebeamercolor [fg] { subtitle} \insertsubtitle \par } %
\fi %
\end { beamercolorbox} %
\vskip 1em\par
\begin { beamercolorbox} [sep=8pt,center]{ author}
\usebeamerfont { author} \insertauthor
\end { beamercolorbox}
\begin { beamercolorbox} [sep=8pt,center]{ institute}
\usebeamerfont { institute} \insertinstitute
\end { beamercolorbox}
\begin { beamercolorbox} [sep=8pt,center]{ date}
\usebeamerfont { date} \insertdate
\end { beamercolorbox}
\endgroup
\vfill
}
2008-07-04 16:30:46 +02:00
\AtBeginSection []
{
\begin { frame}
\frametitle { Outline}
\tableofcontents [currentsection]
\end { frame}
}
\begin { document}
\begin { frame}
\titlepage
2019-05-31 14:44:52 +02:00
\begin { columns} [T]
\column { 0.2\textwidth }
\column { 0.09\textwidth }
\ccbysa
\column { 0.71\textwidth }
\tiny
2022-04-11 23:42:37 +02:00
Copyright © 2008--2022 Dynare Team \\
2019-05-31 14:44:52 +02:00
Licence: \href { http://creativecommons.org/licenses/by-sa/4.0/} { Creative
Commons Attribution-ShareAlike 4.0}
\end { columns}
2008-07-04 16:30:46 +02:00
\end { frame}
\begin { frame}
\frametitle { Outline}
\tableofcontents
\end { frame}
\section { Overview}
\begin { frame}
\frametitle { Motivation}
\begin { itemize}
2019-05-31 14:44:52 +02:00
\item The \textbf { Dynare language} (used in \texttt { .mod} files) is well suited for many economic models
2008-07-04 16:30:46 +02:00
\begin { itemize}
2019-05-31 14:44:52 +02:00
\item It's a markup language that defines models
\item Lacks a programmatic element
\end { itemize}
\item The \textbf { Dynare macro language} adds a programmatic element to Dynare
\begin { itemize}
\item Introduces conditionals, loops, and other simple programmatic directives
2022-04-11 23:42:37 +02:00
\item Used to speed up model development
2019-05-31 14:44:52 +02:00
\item Useful in various situations
\begin { itemize}
\item Multi-country models
\item Creation of modular \texttt { .mod} files
\item Variable flipping
\item Conditional inclusion of equations
\item ...among others
\end { itemize}
\end { itemize}
2008-07-04 16:30:46 +02:00
\end { itemize}
\end { frame}
\begin { frame}
2019-05-31 14:44:52 +02:00
\frametitle { Design of the macro language}
2008-07-04 16:30:46 +02:00
\begin { itemize}
2021-06-18 16:53:02 +02:00
\item The Dynare macro language provides a set of \textbf { macro commands} that can be used in \texttt { .mod} files
2019-05-31 14:44:52 +02:00
\item The macro processor transforms a \texttt { .mod} file with macro commands into a \texttt { .mod} file without macro commands (doing text expansions/inclusions) and then feeds it to the Dynare parser
2018-06-06 15:22:12 +02:00
\item The key point to understand is that the macro processor only does \textbf { text substitution} (like the C preprocessor or the PHP language)
2008-07-04 16:30:46 +02:00
\end { itemize}
\end { frame}
\begin { frame}
2019-05-31 14:44:52 +02:00
\frametitle { Dynare Flowchart}
2008-07-04 16:30:46 +02:00
\includegraphics [width=0.95\linewidth] { new-design.pdf}
\end { frame}
\section { Syntax}
\begin { frame} [fragile=singleslide]
\frametitle { Macro Directives}
\begin { itemize}
2021-06-18 16:53:02 +02:00
\item Directives begin with: \verb +@#+
2019-05-31 14:44:52 +02:00
\item A directive gives instructions to the macro processor
2009-06-24 14:51:58 +02:00
\item Main directives are:
\begin { itemize}
\item file inclusion: \verb +@#include+
2021-06-18 16:53:02 +02:00
\item definition of a macro processor variable or function: \verb +@#define+
\item conditional statements: \verb +@#if/@#ifdef/@#ifndef/@#else/@#elseif/@#endif+
2018-06-06 15:22:12 +02:00
\item loop statements: \verb +@#for/@#endfor+
2009-06-24 14:51:58 +02:00
\end { itemize}
2022-05-24 12:20:00 +02:00
\item Most directives fit on one line. If needed however, two backslashes (\textit { i.e.} \verb +\\ +) at the end of a line indicate that the directive is continued on the next line.
2022-04-11 23:42:37 +02:00
\item Directives are not terminated with a semicolon
2008-07-04 16:30:46 +02:00
\end { itemize}
\end { frame}
\begin { frame}
2021-06-18 16:53:02 +02:00
\frametitle { Values}
2008-07-04 16:30:46 +02:00
\begin { itemize}
2021-06-18 16:53:02 +02:00
\item The macro processor can handle values of 5 different types:
2022-04-11 23:42:37 +02:00
\begin { enumerate}
2022-05-24 12:20:00 +02:00
\item boolean (logical value, true or false)
\item real (double precision floating point number)
\item string (of characters)
2019-05-31 14:44:52 +02:00
\item tuple
\item array
2022-04-11 23:42:37 +02:00
\end { enumerate}
2021-06-18 16:53:02 +02:00
\item Values of the types listed above can be cast to other types
2019-08-05 22:31:08 +02:00
\begin { itemize}
2021-06-18 16:53:02 +02:00
\item \texttt { (real) "3.1"} $ \rightarrow $ \texttt { 3.1}
\item \texttt { (string) 3.1} $ \rightarrow $ \texttt { "3.1"}
2019-08-06 20:16:26 +02:00
\item \texttt { (array) 4} $ \rightarrow $ \texttt { [4]}
2019-08-07 16:37:35 +02:00
\item \texttt { (real) [5]} $ \rightarrow $ \texttt { 5}
\item \texttt { (real) [6, 7]} $ \rightarrow $ \texttt { error}
2021-06-18 16:53:02 +02:00
\item \texttt { (bool) -1 \& \& (bool) 2} $ \rightarrow $ \texttt { true}
2019-08-05 22:31:08 +02:00
\end { itemize}
2019-05-31 14:44:52 +02:00
\end { itemize}
\end { frame}
\begin { frame} [fragile=singleslide]
\frametitle { Macro-expressions (1/8)}
2008-07-04 16:30:46 +02:00
\begin { itemize}
2022-05-24 12:20:00 +02:00
\item Macro-expressions are constructed using literals (\textit { i.e.} fixed values) of the 5 basic types
2021-06-18 16:53:02 +02:00
described above, macro-variables, standard operators, function calls and comprehensions.
2019-05-31 14:44:52 +02:00
\item Macro-expressions can be used in two places:
\begin { itemize}
\item inside macro directives; no special markup is required
2022-04-11 23:42:37 +02:00
\item in the body of the \texttt { .mod} file, between an ``at''-sign and curly braces (like \verb +@{ expr} +); the macro processor will substitute the expression with its value
2019-05-31 14:44:52 +02:00
\end { itemize}
2008-07-04 16:30:46 +02:00
\end { itemize}
\end { frame}
2019-05-31 14:44:52 +02:00
2008-07-04 16:30:46 +02:00
\begin { frame} [fragile=singleslide]
2019-05-31 14:44:52 +02:00
\frametitle { Macro-expressions (2/8): Boolean}
2021-06-18 16:53:02 +02:00
Boolean literals are \texttt { true} and \texttt { false} .
2019-05-31 14:44:52 +02:00
\begin { block} { Operators on booleans}
2008-07-04 16:30:46 +02:00
\begin { itemize}
2019-05-31 14:44:52 +02:00
\item comparison operators: \texttt { == !=}
2022-05-24 12:20:00 +02:00
\item logical operators:
\begin { itemize}
\item conjunction (“and”): \texttt { \& \& }
\item disjunction (“or”): \texttt { ||}
\item negation (“not”): \texttt { !}
\end { itemize}
2019-05-31 14:44:52 +02:00
\end { itemize}
\end { block}
\end { frame}
\begin { frame} [fragile=singleslide]
2019-08-07 16:37:35 +02:00
\frametitle { Macro-expressions (3/8): Real}
\begin { block} { Operators on reals}
2019-05-31 14:44:52 +02:00
\begin { itemize}
\item arithmetic operators: \texttt { + - * / \^ { } }
2009-06-24 14:51:58 +02:00
\item comparison operators: \texttt { < > <= >= == !=}
\item logical operators: \verb +& & || !+
2021-06-18 16:53:02 +02:00
\item range with unit increment: \texttt { 1:4} is equivalent to
real array \texttt { [1, 2, 3, 4]} . (NB: \texttt { [1:4]} is equivalent to an
2022-05-24 12:20:00 +02:00
array containing an array of reals, \textit { i.e.} \texttt { [[1, 2, 3, 4]]} )
2019-08-07 16:56:54 +02:00
\item range with user-defined increment: \texttt { 4:-1.1:-1} is equivalent to real array \texttt { [4, 2.9, 1.8, 0.7, -0.4]} .
2008-07-04 16:30:46 +02:00
\end { itemize}
\end { block}
2019-08-07 16:37:35 +02:00
\begin { block} { Functions for reals}
2019-05-31 14:44:52 +02:00
\begin { itemize}
2021-06-18 16:53:02 +02:00
\item \texttt { min} , \texttt { max} , \texttt { exp} , \texttt { ln} (or \texttt { log} ), \texttt { log10}
2022-05-24 12:20:00 +02:00
\item \texttt { sign} , \texttt { floor} , \texttt { ceil} , \texttt { trunc} , \texttt { round} , \texttt { mod}
2021-06-18 16:53:02 +02:00
\item \texttt { sin} , \texttt { cos} , \texttt { tan} , \texttt { asin} , \texttt { acos} , \texttt { atan}
2022-05-24 12:20:00 +02:00
\item \texttt { sqrt} , \texttt { cbrt} , \texttt { erf} , \texttt { erfc} , \texttt { normpdf} , \texttt { normcdf} , \texttt { gamma} , \texttt { lgamma}
2019-05-31 14:44:52 +02:00
\end { itemize}
\end { block}
\end { frame}
\begin { frame} [fragile=singleslide]
\frametitle { Macro-expressions (4/8): String}
2021-06-18 16:53:02 +02:00
String literals have to be declared between \textit { double} quotes, e.g. \texttt { "string"}
2008-07-04 16:30:46 +02:00
\begin { block} { Operators on character strings}
\begin { itemize}
2019-05-31 14:44:52 +02:00
\item comparison operators: \texttt { < > <= >= == !=}
2008-07-04 16:30:46 +02:00
\item concatenation: \texttt { +}
2019-05-31 14:44:52 +02:00
\item string length: \texttt { length()}
2019-08-07 17:47:50 +02:00
\item string emptiness: \texttt { isempty()}
2008-07-04 16:30:46 +02:00
\item extraction of substrings: if \texttt { s} is a string, then one can write \texttt { s[3]} or \texttt { s[4:6]}
\end { itemize}
\end { block}
\end { frame}
\begin { frame} [fragile=singleslide]
2019-05-31 14:44:52 +02:00
\frametitle { Macro-expressions (5/8): Tuple}
2022-04-11 23:42:37 +02:00
Tuples are enclosed by parentheses and elements are separated by commas (like
2019-05-31 14:44:52 +02:00
\texttt { (a,b,c)} or \texttt { (1,2.2,c)} ).
\begin { block} { Operators on tuples}
\begin { itemize}
\item comparison operators: \texttt { == !=}
2022-05-24 12:20:00 +02:00
\item functions: \texttt { length()} , \texttt { isempty()}
2019-05-31 14:44:52 +02:00
\item testing membership in tuple: \texttt { in} operator \\ (example:
2021-06-18 16:53:02 +02:00
\texttt { "b" in ("a", "b", "c")} returns \texttt { true} )
2019-05-31 14:44:52 +02:00
\end { itemize}
\end { block}
\end { frame}
\begin { frame} [fragile=singleslide]
2021-06-18 16:53:02 +02:00
\frametitle { Macro-expressions (6/8): Array (1/2)}
2019-05-31 14:44:52 +02:00
Arrays are enclosed by brackets, and their elements are separated by commas
2021-06-18 16:53:02 +02:00
(like \texttt { [1,[2,3],4]} or \texttt { ["US", "EA"]} ).
2008-07-04 16:30:46 +02:00
\begin { block} { Operators on arrays}
\begin { itemize}
2019-05-31 14:44:52 +02:00
\item comparison operators: \texttt { == !=}
2008-07-04 16:30:46 +02:00
\item dereferencing: if \texttt { v} is an array, then \texttt { v[2]} is its $ 2 ^ { \textrm { nd } } $ element
\item concatenation: \texttt { +}
2022-05-24 12:20:00 +02:00
\item functions: \texttt { sum()} , \texttt { length()} , \texttt { isempty()}
2008-07-04 16:30:46 +02:00
\item extraction of sub-arrays: \textit { e.g.} \texttt { v[4:6]}
2010-07-01 15:21:41 +02:00
\item testing membership of an array: \texttt { in} operator \\ (example:
2021-06-18 16:53:02 +02:00
\texttt { "b" in ["a", "b", "c"]} returns \texttt { true} )
\end { itemize}
\end { block}
\end { frame}
\begin { frame} [fragile=singleslide]
\frametitle { Macro-expressions (6/8): Array (2/2)}
Arrays can be seen as representing a set of elements (assuming no element
appears twice in the array). Several set operations can thus be performed on
2022-04-11 23:42:37 +02:00
arrays: union, intersection, difference, Cartesian product and power.
2021-06-18 16:53:02 +02:00
\begin { block} { Set operations on arrays}
\begin { itemize}
\item set union: \texttt { |}
\item set intersection: \texttt { \& }
\item set difference: \texttt { -}
\item Cartesian product of two arrays: \texttt { *}
\item Cartesian power of an array: \texttt { \^ }
2008-07-04 16:30:46 +02:00
\end { itemize}
\end { block}
2022-04-11 23:42:37 +02:00
For example: if \texttt { A} and \texttt { B} are arrays, then the following
2021-06-18 16:53:02 +02:00
set operations are valid: \texttt { A|B} , \texttt { A\& B} , \texttt { A-B} ,
\texttt { A*B} , \texttt { A\^ { } 3} .
2022-05-24 12:20:00 +02:00
NB: the array resulting from Cartesian product or power has tuples as its elements.
2019-05-31 14:44:52 +02:00
\end { frame}
2008-07-04 16:30:46 +02:00
2019-05-31 14:44:52 +02:00
\begin { frame} [fragile=singleslide]
\frametitle { Macro-expressions (7/8): Comprehension (1/3)}
2021-06-18 16:53:02 +02:00
Comprehensions are a shorthand way of creating arrays from other arrays. This is done by filtering, mapping, or both.
2019-05-31 14:44:52 +02:00
\begin { block} { Filtering}
\begin { itemize}
\item Allows one to choose those elements from an array for which a condition holds
2021-06-18 16:53:02 +02:00
\item Syntax: \texttt { [} \textit { variable/tuple} \texttt { in} \textit { array} \texttt { when}
\textit { condition} \texttt { ]}
2019-05-31 14:44:52 +02:00
\item Example: Choose even numbers from array
\begin { itemize}
2021-06-18 16:53:02 +02:00
\item Code: \texttt { [ i in 1:5 when mod(i,2) == 0 ]}
2019-05-31 14:44:52 +02:00
\item Result: \texttt { [2, 4]}
\end { itemize}
\end { itemize}
\end { block}
\end { frame}
\begin { frame} [fragile=singleslide]
\frametitle { Macro-expressions (7/8): Comprehension (2/3)}
\begin { block} { Mapping}
\begin { itemize}
\item Allows one to apply a transformation to every element of an array
2021-06-18 16:53:02 +02:00
\item Syntax: \texttt { [} \textit { expr} \texttt { for} \textit { variable/tuple}
\texttt { in} \textit { array} \texttt { ]}
2019-05-31 14:44:52 +02:00
\item Example: Square elements in array
\begin { itemize}
2021-06-18 16:53:02 +02:00
\item Code: \texttt { [ i\^ { } 2 for i in 1:5 ]}
2019-05-31 14:44:52 +02:00
\item Result: \texttt { [1, 4, 9, 16, 25]}
\end { itemize}
2021-06-18 16:53:02 +02:00
\item Example: Swap pairs of an array
2019-05-31 14:44:52 +02:00
\begin { itemize}
2021-06-18 16:53:02 +02:00
\item Code: \texttt { [ (j,i) for (i,j) in (1:2)\^ { } 2 ]}
2019-05-31 14:44:52 +02:00
\item Result: \texttt { [(1, 1), (2, 1), (1, 2), (2, 2)]}
\end { itemize}
\end { itemize}
\end { block}
\end { frame}
\begin { frame} [fragile=singleslide]
\frametitle { Macro-expressions (7/8): Comprehension (3/3)}
\begin { block} { Mapping and Filtering}
\begin { itemize}
\item Allows one to apply a transformation to the elements selected from an array
2021-06-18 16:53:02 +02:00
\item Syntax: \texttt { [} \textit { expr} \texttt { for} \textit { variable/tuple}
\texttt { in} \textit { array} \texttt { when} \textit { condition} \texttt { ]}
\item Example: Square of odd numbers between 1 and 5
2019-05-31 14:44:52 +02:00
\begin { itemize}
2021-06-18 16:53:02 +02:00
\item Code: \texttt { [ i\^ { } 2 for i in 1:5 when mod(i,2) == 1 ]}
2019-05-31 14:44:52 +02:00
\item Result: \texttt { [1, 9, 25]}
\end { itemize}
\end { itemize}
\end { block}
\end { frame}
\begin { frame} [fragile=singleslide]
\frametitle { Macro-expressions (8/8): Functions}
2008-07-04 16:30:46 +02:00
\begin { itemize}
2019-05-31 14:44:52 +02:00
\item Can take any number of arguments
2022-04-11 23:42:37 +02:00
\item Dynamic binding: is evaluated when invoked during the macroprocessing stage, not when defined
2019-05-31 14:44:52 +02:00
\item Can be included in expressions; valid operators depend on return type
2008-07-04 16:30:46 +02:00
\end { itemize}
2021-06-18 16:53:02 +02:00
\begin { block} { Declaration syntax}
\verb +@#define +\textit { function\_ signature} \verb + = +\textit { expression}
\end { block}
\begin { block} { Example}
If we declare the following function:
\begin { verbatim}
@#define distance(x, y) = sqrt(x^ 2 + y^ 2)
\end { verbatim}
Then \texttt { distance(3, 4)} will be equivalent to \texttt { 5} .
\end { block}
2008-07-04 16:30:46 +02:00
\end { frame}
\begin { frame} [fragile=singleslide]
2021-06-18 16:53:02 +02:00
\frametitle { Defining macro-variables}
The value of a macro-variable can be defined with the \verb +@#define+
directive.
2008-07-04 16:30:46 +02:00
2022-04-11 23:42:37 +02:00
The macro processor has its own list of variables, which are different from model variables and MATLAB/Octave variables
2008-07-04 16:30:46 +02:00
\begin { block} { Syntax}
2019-05-31 14:44:52 +02:00
\verb +@#define +\textit { variable\_ name} \verb + = +\textit { expression}
2008-07-04 16:30:46 +02:00
\end { block}
\begin { block} { Examples}
\begin { verbatim}
2019-08-07 16:56:54 +02:00
@#define x = 5 // Real
2019-05-31 14:44:52 +02:00
@#define y = "US" // String
2019-08-07 16:56:54 +02:00
@#define v = [ 1, 2, 4 ] // Real array
2019-05-31 14:44:52 +02:00
@#define w = [ "US", "EA" ] // String array
@#define z = 3 + v[2] // Equals 5
2021-06-18 16:53:02 +02:00
@#define t = ("US" in w) // Equals true
2008-07-04 16:30:46 +02:00
\end { verbatim}
\end { block}
2022-04-11 23:42:37 +02:00
NB: You can define macro variables on the Dynare command line by using the \texttt { -D} option
2008-07-04 16:30:46 +02:00
\end { frame}
\begin { frame} [fragile=singleslide]
\frametitle { Expression substitution}
\framesubtitle { Dummy example}
2019-05-31 14:44:52 +02:00
\begin { block} { Before macro processing}
2008-07-04 16:30:46 +02:00
\begin { verbatim}
2019-05-31 14:44:52 +02:00
@#define x = 1
@#define y = [ "B", "C" ]
2008-07-04 16:30:46 +02:00
@#define i = 2
2019-05-31 14:44:52 +02:00
@#define f(x) = x + " + " + y[i]
@#define i = 1
2008-07-04 16:30:46 +02:00
model;
2019-05-31 14:44:52 +02:00
A = @{ y[i] + f("D")} ;
2008-07-04 16:30:46 +02:00
end;
\end { verbatim}
\end { block}
2019-05-31 14:44:52 +02:00
\begin { block} { After macro processing}
2008-07-04 16:30:46 +02:00
\begin { verbatim}
model;
2019-05-31 14:44:52 +02:00
A = BD + B;
2008-07-04 16:30:46 +02:00
end;
\end { verbatim}
\end { block}
\end { frame}
2017-06-28 18:19:10 +02:00
\begin { frame} [fragile=singleslide]
2019-05-31 14:44:52 +02:00
\frametitle { Include directive (1/2)}
2017-06-28 18:19:10 +02:00
\begin { itemize}
2018-06-06 15:22:12 +02:00
\item This directive simply inserts the text of another file in its place
2017-06-28 18:19:10 +02:00
\begin { block} { Syntax}
\verb +@#include "+\textit { filename} \verb +"+
\end { block}
\begin { block} { Example}
\begin { verbatim}
@#include "modelcomponent.mod"
\end { verbatim}
\end { block}
2018-06-06 15:22:12 +02:00
\item Equivalent to a copy/paste of the content of the included file
2017-06-28 18:19:10 +02:00
\item Note that it is possible to nest includes (\textit { i.e.} to include a
2018-06-06 15:22:12 +02:00
file with an included file)
2017-06-28 18:19:10 +02:00
\end { itemize}
\end { frame}
\begin { frame} [fragile=singleslide]
2019-05-31 14:44:52 +02:00
\frametitle { Include directive (2/2)}
2017-06-28 18:19:10 +02:00
\begin { itemize}
\item The filename can be given by a macro-variable (useful in loops):
\begin { block} { Example with variable}
\begin { verbatim}
@#define fname = "modelcomponent.mod"
@#include fname
\end { verbatim}
\end { block}
2019-05-31 14:44:52 +02:00
\item Files to include are searched for in the current directory. Other directories can
be added with the
\verb +@#includepath+ directive, the \texttt { -I} command line option, or the
2022-04-11 23:42:37 +02:00
\texttt { [paths]} section in config files.
2017-06-28 18:19:10 +02:00
\end { itemize}
\end { frame}
2008-07-04 16:30:46 +02:00
\begin { frame} [fragile=singleslide]
2021-06-18 16:53:02 +02:00
\frametitle { Loop directive (1/4)}
\begin { block} { Syntax 1: Simple iteration over one variable}
2008-07-04 16:30:46 +02:00
\verb +@#for +\textit { variable\_ name} \verb + in +\textit { array\_ expr} \\
\verb + +\textit { loop\_ body} \\
\verb +@#endfor+
\end { block}
2021-06-18 16:53:02 +02:00
\begin { block} { Syntax 2: Iteration over several variables at the same time}
2019-08-26 15:38:08 +02:00
\verb +@#for +\textit { tuple} \verb + in +\textit { array\_ expr} \\
\verb + +\textit { loop\_ body} \\
\verb +@#endfor+
\end { block}
2021-06-18 16:53:02 +02:00
\begin { block} { Syntax 3: Iteration with some values excluded}
2019-08-26 15:38:08 +02:00
\verb +@#for +\textit { tuple\_ or\_ variable} \verb + in +\textit { array\_ expr} \verb + when +\textit { expr} \\
\verb + +\textit { loop\_ body} \\
\verb +@#endfor+
\end { block}
\end { frame}
\begin { frame} [fragile=singleslide]
2021-06-18 16:53:02 +02:00
\frametitle { Loop directive (2/4)}
2019-05-31 14:44:52 +02:00
\begin { block} { Example: before macro processing}
2008-07-04 16:30:46 +02:00
\small
\begin { verbatim}
model;
@#for country in [ "home", "foreign" ]
2009-06-24 14:51:58 +02:00
GDP_ @{ country} = A * K_ @{ country} ^ a * L_ @{ country} ^ (1-a);
2008-07-04 16:30:46 +02:00
@#endfor
end;
\end { verbatim}
\normalsize
\end { block}
2019-05-31 14:44:52 +02:00
\begin { block} { Example: after macro processing}
2008-07-04 16:30:46 +02:00
\small
\begin { verbatim}
model;
2009-06-24 14:51:58 +02:00
GDP_ home = A * K_ home^ a * L_ home^ (1-a);
GDP_ foreign = A * K_ foreign^ a * L_ foreign^ (1-a);
2008-07-04 16:30:46 +02:00
end;
\end { verbatim}
\normalsize
\end { block}
\end { frame}
2021-06-18 16:53:02 +02:00
\begin { frame} [fragile=singleslide]
\frametitle { Loop directive (3/4)}
\begin { block} { Example: loop over several variables}
\small
\begin { verbatim}
@#define A = [ "X", "Y", "Z"]
@#define B = [ 1, 2, 3]
model;
@#for (i,j) in A*B
e_ @{ i} _ @{ j} = …
@#endfor
end;
\end { verbatim}
\normalsize
This will loop over \texttt { e\_ X\_ 1} , \texttt { e\_ X\_ 2} , …, \texttt { e\_ Z\_ 3} (9
variables in total)
\end { block}
\end { frame}
\begin { frame} [fragile=singleslide]
\frametitle { Loop directive (4/4)}
\begin { block} { Example: loop over several variables with filtering}
\small
\begin { verbatim}
model;
@#for (i,j,k) in (1:10)^ 3 when i^ 2+j^ 2==k^ 2
e_ @{ i} _ @{ j} _ @{ k} = …
@#endfor
end;
\end { verbatim}
\normalsize
This loop will iterate over only 4 triplets: \texttt { (3,4,5)} ,
\texttt { (4,3,5)} , \texttt { (6,8,10)} , \texttt { (8,6,10)} .
\end { block}
\end { frame}
2008-07-04 16:30:46 +02:00
\begin { frame} [fragile=singleslide]
2019-08-19 15:13:59 +02:00
\frametitle { Conditional directives (1/3)}
2008-07-04 16:30:46 +02:00
\begin { columns} [T]
\column { 0.47\linewidth }
\begin { block} { Syntax 1}
2019-08-13 15:17:20 +02:00
\verb +@#if +\textit { bool\_ or\_ real\_ expr} \\
\verb + +\textit { body included if expr is true (or != 0)} \\
2008-07-04 16:30:46 +02:00
\verb +@#endif+
\end { block}
\column { 0.47\linewidth }
\begin { block} { Syntax 2}
2019-08-13 15:17:20 +02:00
\verb +@#if +\textit { bool\_ or\_ real\_ expr} \\
\verb + +\textit { body included if expr is true (or != 0)} \\
2008-07-04 16:30:46 +02:00
\verb +@#else+ \\
2019-08-13 15:17:20 +02:00
\verb + +\textit { body included if expr is false (or 0)} \\
2008-07-04 16:30:46 +02:00
\verb +@#endif+
\end { block}
\end { columns}
2019-08-19 15:13:59 +02:00
\end { frame}
\begin { frame} [fragile=singleslide]
\frametitle { Conditional directives (2/3)}
\begin { block} { Syntax 3}
2022-04-11 23:42:37 +02:00
\scriptsize
\verb +@#if +\textit { bool\_ or\_ real\_ expr1} \\
2019-08-19 15:13:59 +02:00
\verb + +\textit { body included if expr1 is true (or != 0)} \\
\verb +@#elseif +\textit { bool\_ or\_ real\_ expr2} \\
\verb + +\textit { body included if expr2 is true (or != 0)} \\
\verb +@#else+ \\
\verb + +\textit { body included if expr1 and expr2 are false (or 0)} \\
\verb +@#endif+
\end { block}
2008-07-04 16:30:46 +02:00
\begin { block} { Example: alternative monetary policy rules}
\scriptsize
\begin { verbatim}
2019-08-13 15:17:20 +02:00
@#define linear_ mon_ pol = false // or 0
2008-07-04 16:30:46 +02:00
...
model;
@#if linear_ mon_ pol
2009-04-27 15:39:52 +02:00
i = w*i(-1) + (1-w)*i_ ss + w2*(pie-piestar);
2008-07-04 16:30:46 +02:00
@#else
2009-04-27 15:39:52 +02:00
i = i(-1)^ w * i_ ss^ (1-w) * (pie/piestar)^ w2;
2008-07-04 16:30:46 +02:00
@#endif
...
end;
\end { verbatim}
\scriptsize
\end { block}
\end { frame}
2012-06-13 18:40:03 +02:00
\begin { frame} [fragile=singleslide]
2019-08-19 15:13:59 +02:00
\frametitle { Conditional directives (3/3)}
2012-06-13 18:40:03 +02:00
\begin { columns} [T]
\column { 0.47\linewidth }
\begin { block} { Syntax 1}
\verb +@#ifdef +\textit { variable\_ name} \\
\verb + +\textit { body included if variable defined} \\
\verb +@#endif+
\end { block}
\column { 0.47\linewidth }
\begin { block} { Syntax 2}
\verb +@#ifdef +\textit { variable\_ name} \\
\verb + +\textit { body included if variable defined} \\
\verb +@#else+ \\
\verb + +\textit { body included if variable not defined} \\
\verb +@#endif+
\end { block}
\end { columns}
2013-06-27 14:51:20 +02:00
\bigskip
2022-04-11 23:42:37 +02:00
\begin { itemize}
\item There is also \verb +@#ifndef+, which is the opposite of \verb +@#ifdef+
(\textit { i.e.} it tests whether a variable is \emph { not} defined).
\item NB: There is
2019-08-19 15:13:59 +02:00
\emph { no} \verb +@#elseifdef+ or \verb +@#elseifndef+ directive; use
\verb +elseif defined(variable_ name)+ to achieve the desired objective.
2022-04-11 23:42:37 +02:00
\end { itemize}
2012-06-13 18:40:03 +02:00
\end { frame}
2008-07-04 16:30:46 +02:00
\begin { frame} [fragile=singleslide]
2022-04-11 23:42:37 +02:00
\frametitle { Echo directives}
2008-07-04 16:30:46 +02:00
\begin { itemize}
\item The echo directive will simply display a message on standard output
2019-08-26 18:12:04 +02:00
\item The echomacrovars directive will display all of the macro variables (or
2022-04-11 23:42:37 +02:00
those specified) and their values
\item The \texttt { save} option allows saving this information to \texttt { options\_ .macrovars\_ line\_ x} , where \texttt { x} denotes the line number where the statement was encountered
2008-07-04 16:30:46 +02:00
\end { itemize}
\begin { block} { Syntax}
2022-04-11 23:42:37 +02:00
\verb +@#echo +\textit { string\_ expr} \\
\verb +@#echomacrovars +\\
\verb +@#echomacrovars +\textit { list\_ of\_ variables} \\
\verb +@#echomacrovars(save)+\\
\verb +@#echomacrovars(save) +\textit { list\_ of\_ variables} \\
2008-07-04 16:30:46 +02:00
\end { block}
\begin { block} { Examples}
\begin { verbatim}
@#echo "Information message."
2022-04-11 23:42:37 +02:00
\end { verbatim}
\end { block}
\end { frame}
\begin { frame} [fragile=singleslide]
\frametitle { Error directive}
\begin { itemize}
\item The error directive will display the message and make Dynare stop (only makes sense inside a conditional directive)
\end { itemize}
\begin { block} { Syntax}
\verb +@#error +\textit { string\_ expr} \\
\end { block}
\begin { block} { Example}
\begin { verbatim}
2008-07-04 16:30:46 +02:00
@#error "Error message!"
\end { verbatim}
\end { block}
\end { frame}
\begin { frame}
2019-05-31 14:44:52 +02:00
\frametitle { Macro-related command line options}
2008-07-04 16:30:46 +02:00
\begin { itemize}
2019-05-31 14:44:52 +02:00
\item \texttt { savemacro} : Useful for debugging or learning purposes, saves the output of the macro processor. If your \texttt { .mod} file is called \texttt { file.mod} , the output is saved to \texttt { file-macroexp.mod} .
2022-04-11 23:42:37 +02:00
\item NB: \texttt { savemacro=filename} allows a user-defined file name
\item \texttt { linemacro} : In the output of \texttt { savemacro} , print line numbers where the macro directives were placed.
2019-05-31 14:44:52 +02:00
\item \texttt { onlymacro} : Stops processing after the macro processing step.
2008-07-04 16:30:46 +02:00
\end { itemize}
\end { frame}
2018-06-06 15:22:12 +02:00
\section { Common uses}
2008-07-04 16:30:46 +02:00
\begin { frame} [fragile=singleslide]
\frametitle { Modularization}
\begin { itemize}
2019-05-31 14:44:52 +02:00
\item The \verb +@#include+ directive can be used to split \texttt { .mod} files into several modular components
2008-07-04 16:30:46 +02:00
\item Example setup:
2009-06-24 14:51:58 +02:00
\begin { description}
2022-04-11 23:42:37 +02:00
\item [\texttt{modeldesc.mod}:] contains variable declarations, model equations, and shock declarations
\item [\texttt{simulate.mod}:] includes \texttt { modeldesc.mod} , calibrates parameters, and runs stochastic simulations
\item [\texttt{estim.mod}:] includes \texttt { modeldesc.mod} , declares priors on parameters, and runs Bayesian estimation
2009-06-24 14:51:58 +02:00
\end { description}
2012-10-31 16:49:09 +01:00
\item Dynare can be called on \texttt { simulate.mod} and \texttt { estim.mod}
2009-06-24 14:51:58 +02:00
\item But it makes no sense to run it on \texttt { modeldesc.mod}
2022-04-11 23:42:37 +02:00
\item Advantage: no need to manually copy/paste the whole model (during initial development) or port model changes (during development)
2008-07-04 16:30:46 +02:00
\end { itemize}
\end { frame}
\begin { frame} [fragile=singleslide]
\frametitle { Indexed sums or products}
\framesubtitle { Example: moving average}
\begin { columns} [T]
\column { 0.47\linewidth }
2019-05-31 14:44:52 +02:00
\begin { block} { Before macro processing}
2008-07-04 16:30:46 +02:00
\begin { verbatim}
@#define window = 2
var x MA_ x;
...
model;
...
2019-05-31 14:44:52 +02:00
MA_ x = @{ 1/(2*window+1)} *(
2008-07-04 16:30:46 +02:00
@#for i in -window:window
+x(@{ i} )
@#endfor
);
...
end;
\end { verbatim}
\end { block}
\column { 0.47\linewidth }
2019-05-31 14:44:52 +02:00
\begin { block} { After macro processing}
2008-07-04 16:30:46 +02:00
\begin { verbatim}
var x MA_ x;
...
model;
...
MA_ x = 1/5*(
+x(-2)
+x(-1)
+x(0)
+x(1)
+x(2)
);
...
end;
\end { verbatim}
\end { block}
\end { columns}
\end { frame}
\begin { frame} [fragile=singleslide]
\frametitle { Multi-country models}
2019-05-31 14:44:52 +02:00
\framesubtitle { \texttt { .mod} file skeleton example}
2008-07-04 16:30:46 +02:00
\scriptsize
\begin { verbatim}
2009-06-24 14:51:58 +02:00
@#define countries = [ "US", "EA", "AS", "JP", "RC" ]
2008-07-04 16:30:46 +02:00
@#define nth_ co = "US"
@#for co in countries
var Y_ @{ co} K_ @{ co} L_ @{ co} i_ @{ co} E_ @{ co} ...;
parameters a_ @{ co} ...;
varexo ...;
@#endfor
model;
@#for co in countries
Y_ @{ co} = K_ @{ co} ^ a_ @{ co} * L_ @{ co} ^ (1-a_ @{ co} );
...
@# if co != nth_ co
(1+i_ @{ co} ) = (1+i_ @{ nth_ co} ) * E_ @{ co} (+1) / E_ @{ co} ; // UIP relation
@# else
E_ @{ co} = 1;
@# endif
@#endfor
end;
\end { verbatim}
\normalsize
\end { frame}
\begin { frame}
2009-03-13 17:34:56 +01:00
\frametitle { Endogeneizing parameters (1/4)}
2008-07-04 16:30:46 +02:00
\begin { itemize}
2022-04-11 23:42:37 +02:00
\item When calibrating the model, it may be useful to pin down parameters by targeting endogenous objects
2008-07-04 16:30:46 +02:00
\item Example:
\begin { gather*}
2022-04-11 23:42:37 +02:00
y_ t = \left (\alpha ^ { \frac { 1} { \xi } } \ell _ t^ { 1-\frac { 1} { \xi } } + (1-\alpha )^ { \frac { 1} { \xi } } k_ t^ { 1-\frac { 1} { \xi } } \right )^ { \frac { \xi } { \xi - 1} } \\
lab\_ rat_ t = \frac { w_ t \ell _ t} { p_ t y_ t}
2008-07-04 16:30:46 +02:00
\end { gather*}
2022-04-11 23:42:37 +02:00
\item In the model, $ \alpha $ is a (share) parameter, and $ lab \_ rat _ t $ is an endogenous variable
2009-03-13 17:34:56 +01:00
\item We observe that:
\begin { itemize}
2022-04-11 23:42:37 +02:00
\item setting a value for $ \alpha $ is not straightforward!
\item but we have real world data for $ lab \_ rat _ t $
\item it is clear that these two objects are economically linked
2009-03-13 17:34:56 +01:00
\end { itemize}
\end { itemize}
\end { frame}
\begin { frame} [fragile=singleslide]
\frametitle { Endogeneizing parameters (2/4)}
\begin { itemize}
2022-04-11 23:42:37 +02:00
\item Therefore, when computing the steady state by solving the static model:
2009-03-13 17:34:56 +01:00
\begin { itemize}
2022-04-11 23:42:37 +02:00
\item we make $ \alpha $ a variable and the steady state value $ lab \_ rat $ of the dynamic variable $ lab \_ rat _ t $ a parameter
\item we impose an economically sensible value for $ lab \_ rat $
2009-03-13 17:34:56 +01:00
\item the solution algorithm deduces the implied value for $ \alpha $
\end { itemize}
2022-04-11 23:42:37 +02:00
\item We call this method ``variable flipping'', because it treats $ \alpha $ as a variable and $ lab \_ rat $ as a parameter for the purpose of the static model
2008-07-04 16:30:46 +02:00
\end { itemize}
\end { frame}
\begin { frame} [fragile=singleslide]
2009-03-13 17:34:56 +01:00
\frametitle { Endogeneizing parameters (3/4)}
\framesubtitle { Example implementation}
2008-07-04 16:30:46 +02:00
\begin { itemize}
2009-03-13 17:34:56 +01:00
\item File \texttt { modeqs.mod} :
\begin { itemize}
\item contains variable declarations and model equations
\item For declaration of \texttt { alpha} and \texttt { lab\_ rat} :
2008-07-04 16:30:46 +02:00
\footnotesize
\begin { verbatim}
@#if steady
var alpha;
parameter lab_ rat;
@#else
parameter alpha;
var lab_ rat;
@#endif
\end { verbatim}
\normalsize
\end { itemize}
2009-03-13 17:34:56 +01:00
2008-07-04 16:30:46 +02:00
\end { itemize}
\end { frame}
\begin { frame} [fragile=singleslide]
2009-03-13 17:34:56 +01:00
\frametitle { Endogeneizing parameters (4/4)}
\framesubtitle { Example implementation}
2008-07-04 16:30:46 +02:00
\begin { itemize}
2012-10-31 16:49:09 +01:00
\item File \texttt { steadystate.mod} :
2009-03-13 17:34:56 +01:00
\begin { itemize}
2019-08-13 15:17:20 +02:00
\item begins with \verb +@#define steady = true+
2022-04-11 23:42:37 +02:00
\item followed by \verb +@#include "modeqs.mod"+
2009-03-13 17:34:56 +01:00
\item initializes parameters (including \texttt { lab\_ rat} , excluding \texttt { alpha} )
\item computes steady state (using guess values for endogenous, including \texttt { alpha} )
2022-04-11 23:42:37 +02:00
\item saves values of parameters and variables at steady-state in a file, using the \texttt { save\_ params\_ and\_ steady\_ state} command
2009-03-13 17:34:56 +01:00
\end { itemize}
2012-10-31 16:49:09 +01:00
\item File \texttt { simulate.mod} :
2008-07-04 16:30:46 +02:00
\begin { itemize}
2019-08-13 15:17:20 +02:00
\item begins with \verb +@#define steady = false+
2022-04-11 23:42:37 +02:00
\item followed by \verb +@#include "modeqs.mod"+
\item loads values of parameters and variables at steady-state from file, using the \texttt { load\_ params\_ and\_ steady\_ state} command
2008-07-04 16:30:46 +02:00
\item computes simulations
\end { itemize}
\end { itemize}
\end { frame}
2010-07-01 15:21:41 +02:00
\begin { frame} [fragile=singleslide]
2018-06-06 15:22:12 +02:00
\frametitle { MATLAB/Octave loops vs macro processor loops (1/3)}
2010-07-01 15:21:41 +02:00
Suppose you have a model with a parameter $ \rho $ , and you want to make
simulations for three values: $ \rho = 0 . 8 , 0 . 9 , 1 $ . There are
several ways of doing this:
2015-06-08 14:55:05 +02:00
\begin { block} { With a MATLAB/Octave loop}
2010-07-01 15:21:41 +02:00
\begin { verbatim}
rhos = [ 0.8, 0.9, 1];
for i = 1:length(rhos)
2022-04-11 23:42:37 +02:00
set_ param_ value('rho',rhos(i));
2010-07-01 15:21:41 +02:00
stoch_ simul(order=1);
2022-04-11 23:42:37 +02:00
if info(1)~=0
error('Simulation failed for parameter draw')
end
2010-07-01 15:21:41 +02:00
end
\end { verbatim}
\end { block}
\begin { itemize}
\item The loop is not unrolled
2015-06-08 14:55:05 +02:00
\item MATLAB/Octave manages the iterations
2022-05-24 12:20:00 +02:00
\item NB: always check whether the error flag \texttt { info(1)==0} to prevent erroneously relying on stale results from previous iterations
2010-07-01 15:21:41 +02:00
\end { itemize}
\end { frame}
\begin { frame} [fragile=singleslide]
2018-06-06 15:22:12 +02:00
\frametitle { MATLAB/Octave loops vs macro processor loops (2/3)}
\begin { block} { With a macro processor loop (case 1)}
2010-07-01 15:21:41 +02:00
\begin { verbatim}
rhos = [ 0.8, 0.9, 1];
@#for i in 1:3
2022-04-11 23:42:37 +02:00
set_ param_ value('rho',rhos(@{ i} ));
2010-07-01 15:21:41 +02:00
stoch_ simul(order=1);
2022-04-11 23:42:37 +02:00
if info(1)~=0
error('Simulation failed for parameter draw')
end
2010-07-01 15:21:41 +02:00
@#endfor
\end { verbatim}
\end { block}
\begin { itemize}
\item Very similar to previous example
\item Loop is unrolled
2018-06-06 15:22:12 +02:00
\item Dynare macro processor manages the loop index but not the data array (\texttt { rhos} )
2010-07-01 15:21:41 +02:00
\end { itemize}
\end { frame}
\begin { frame} [fragile=singleslide]
2018-06-06 15:22:12 +02:00
\frametitle { MATLAB/Octave loops vs macro processor loops (3/3)}
\begin { block} { With a macro processor loop (case 2)}
2010-07-01 15:21:41 +02:00
\begin { verbatim}
2019-05-31 14:44:52 +02:00
@#for rho_ val in [ 0.8, 0.9, 1]
2022-04-11 23:42:37 +02:00
set_ param_ value('rho',@{ rho_ val} );
2010-07-01 15:21:41 +02:00
stoch_ simul(order=1);
2022-04-11 23:42:37 +02:00
if info(1)~=0
error('Simulation failed for parameter draw')
end
2010-07-01 15:21:41 +02:00
@#endfor
\end { verbatim}
\end { block}
\begin { itemize}
2019-05-31 14:44:52 +02:00
\item Shorter syntax, since list of values directly given in the loop construct
\item NB: Array not stored as MATLAB/Octave variable, hence cannot be used in MATLAB/Octave
2010-07-01 15:21:41 +02:00
\end { itemize}
\end { frame}
2008-07-04 16:30:46 +02:00
2015-06-08 14:55:05 +02:00
\begin { frame}
\begin { center}
\vfill { \LARGE Thanks for your attention!} \vfill
{ \LARGE Questions?}
\vfill
\end { center}
\vfill
\end { frame}
2008-07-04 16:30:46 +02:00
\end { document}