File: macroprocessor.tex

package info (click to toggle)
dynare 6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 67,632 kB
  • sloc: cpp: 79,090; ansic: 28,916; objc: 12,430; yacc: 4,528; pascal: 1,993; lex: 1,441; sh: 1,121; python: 634; makefile: 626; lisp: 163; xml: 18
file content (897 lines) | stat: -rw-r--r-- 29,120 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
\documentclass[aspectratio=169]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{upquote}
\usepackage{amsmath}
\usepackage[copyright]{ccicons}

\usetheme{Boadilla}

\graphicspath{{../logos/}}

\title{The Dynare Macro Processor}
\author{Sébastien Villemot}
\pgfdeclareimage[height=0.8cm]{logo}{dlogo}
\institute[Dynare Team]{\pgfuseimage{logo}}

\date{31 May 2024}

\AtBeginSection[]
{
  \begin{frame}
    \frametitle{Outline}
    \tableofcontents[currentsection]
  \end{frame}
}

\begin{document}

\begin{frame}
  \titlepage
\end{frame}

\begin{frame}
  \frametitle{Outline}
  \tableofcontents
\end{frame}

\section{Overview}

\begin{frame}
  \frametitle{Motivation}
  \begin{itemize}
  \item The \textbf{Dynare language} (used in \texttt{.mod} files) is well suited for many economic models
    \begin{itemize}
    \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
    \item Used to speed up model development
    \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}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Design of the macro language}
  \begin{itemize}
  \item The Dynare macro language provides a set of \textbf{macro commands} that can be used in \texttt{.mod} files
  \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
  \item The key point to understand is that the macro processor only does \textbf{text substitution} (like the C preprocessor or the PHP language)
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Dynare Flowchart}
  \includegraphics[width=0.95\linewidth]{new-design.pdf}
\end{frame}

\section{Syntax}

\begin{frame}[fragile=singleslide]
  \frametitle{Macro Directives}
  \begin{itemize}
  \item Directives begin with: \verb+@#+
  \item A directive gives instructions to the macro processor
  \item Main directives are:
    \begin{itemize}
    \item file inclusion: \verb+@#include+
    \item definition of a macro processor variable or function: \verb+@#define+
    \item conditional statements: \verb+@#if/@#ifdef/@#ifndef/@#else/@#elseif/@#endif+
    \item loop statements: \verb+@#for/@#endfor+
    \end{itemize}
  \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.
  \item Directives are not terminated with a semicolon
  \end{itemize}
\end{frame}

\begin{frame}
\frametitle{Values}
\begin{itemize}
\item The macro processor can handle values of 5 different types:
  \begin{enumerate}
  \item boolean (logical value, true or false)
  \item real (double precision floating point number)
  \item string (of characters)
  \item tuple
  \item array
  \end{enumerate}
\item Values of the types listed above can be cast to other types
  \begin{itemize}
  \item \texttt{(real) "3.1"} $\rightarrow$ \texttt{3.1}
  \item \texttt{(string) 3.1} $\rightarrow$ \texttt{"3.1"}
  \item \texttt{(array) 4} $\rightarrow$ \texttt{[4]}
  \item \texttt{(real) [5]} $\rightarrow$ \texttt{5}
  \item \texttt{(real) [6, 7]} $\rightarrow$ \texttt{error}
  \item \texttt{(bool) -1 \&\& (bool) 2} $\rightarrow$ \texttt{true}
  \end{itemize}
\end{itemize}
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{Macro-expressions (1/8)}
  \begin{itemize}
    \item Macro-expressions are constructed using literals (\textit{i.e.} fixed values) of the 5 basic types
      described above, macro-variables, standard operators, function calls and comprehensions.
    \item Macro-expressions can be used in two places:
      \begin{itemize}
      \item inside macro directives; no special markup is required
      \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
      \end{itemize}
  \end{itemize}
\end{frame}


\begin{frame}[fragile=singleslide]
  \frametitle{Macro-expressions (2/8): Boolean}
  Boolean literals are \texttt{true} and \texttt{false}.
  \begin{block}{Operators on booleans}
    \begin{itemize}
    \item comparison operators: \texttt{== !=}
    \item logical operators:
      \begin{itemize}
      \item conjunction (“and”): \texttt{\&\&}
      \item disjunction (“or”): \texttt{||}
      \item negation (“not”): \texttt{!}
      \end{itemize}
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{Macro-expressions (3/8): Real}
  \begin{block}{Operators on reals}
    \begin{itemize}
    \item arithmetic operators: \texttt{+ - * / \^{}}
    \item comparison operators: \texttt{< > <= >= == !=}
    \item logical operators: \verb+&& || !+
    \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
      array containing an array of reals, \textit{i.e.} \texttt{[[1, 2, 3, 4]]})
    \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]}
    \end{itemize}
  \end{block}

  \begin{block}{Functions for reals}
    \begin{itemize}
    \item \texttt{min}, \texttt{max}, \texttt{exp}, \texttt{ln} (or \texttt{log}), \texttt{log10}
    \item \texttt{sign}, \texttt{floor}, \texttt{ceil}, \texttt{trunc}, \texttt{round}, \texttt{mod}
    \item \texttt{sin}, \texttt{cos}, \texttt{tan}, \texttt{asin}, \texttt{acos}, \texttt{atan}
    \item \texttt{sqrt}, \texttt{cbrt}, \texttt{erf}, \texttt{erfc}, \texttt{normpdf}, \texttt{normcdf}, \texttt{gamma}, \texttt{lgamma}
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{Macro-expressions (4/8): String}
  String literals have to be declared between \textit{double} quotes, e.g. \texttt{"string"}
  \begin{block}{Operators on character strings}
    \begin{itemize}
    \item comparison operators: \texttt{< > <= >= == !=}
    \item concatenation: \texttt{+}
    \item string length: \texttt{length()}
    \item string emptiness: \texttt{isempty()}
    \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]
  \frametitle{Macro-expressions (5/8): Tuple}
  Tuples are enclosed by parentheses and elements are separated by commas (like
  \texttt{(a,b,c)} or \texttt{(1,2.2,c)}).
  \begin{block}{Operators on tuples}
    \begin{itemize}
    \item comparison operators: \texttt{== !=}
    \item functions: \texttt{length()}, \texttt{isempty()}
    \item testing membership in tuple: \texttt{in} operator \\ (example:
      \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 (1/2)}
  Arrays are enclosed by brackets, and their elements are separated by commas
  (like \texttt{[1,[2,3],4]} or \texttt{["US", "EA"]}).
  \begin{block}{Operators on arrays}
    \begin{itemize}
    \item comparison operators: \texttt{== !=}
    \item dereferencing: if \texttt{v} is an array, then \texttt{v[2]} is its $2^{\textrm{nd}}$ element
    \item concatenation: \texttt{+}
    \item functions: \texttt{sum()}, \texttt{length()}, \texttt{isempty()}
    \item extraction of sub-arrays: \textit{e.g.} \texttt{v[4:6]}
    \item testing membership of an array: \texttt{in} operator \\ (example:
      \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
  arrays: union, intersection, difference, Cartesian product and power.
  \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{\^}
    \end{itemize}
  \end{block}
  For example: if \texttt{A} and \texttt{B} are arrays, then the following
  set operations are valid: \texttt{A|B}, \texttt{A\&B}, \texttt{A-B},
  \texttt{A*B}, \texttt{A\^{}3}.

  NB: the array resulting from Cartesian product or power has tuples as its elements.
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{Macro-expressions (7/8): Comprehension (1/3)}
  Comprehensions are a shorthand way of creating arrays from other arrays. This is done by filtering, mapping, or both.
  \begin{block}{Filtering}
    \begin{itemize}
    \item Allows one to choose those elements from an array for which a condition holds
    \item Syntax: \texttt{[} \textit{variable/tuple} \texttt{in} \textit{array} \texttt{when}
      \textit{condition} \texttt{]}
    \item Example: Choose even numbers from array
      \begin{itemize}
      \item Code: \texttt{[ i in 1:5 when mod(i,2) == 0 ]}
      \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
    \item Syntax: \texttt{[} \textit{expr} \texttt{for} \textit{variable/tuple}
      \texttt{in} \textit{array} \texttt{]}
    \item Example: Square elements in array
      \begin{itemize}
      \item Code: \texttt{[ i\^{}2 for i in 1:5 ]}
      \item Result: \texttt{[1, 4, 9, 16, 25]}
      \end{itemize}
    \item Example: Swap pairs of an array
      \begin{itemize}
      \item Code: \texttt{[ (j,i) for (i,j) in (1:2)\^{}2 ]}
      \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
    \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
      \begin{itemize}
      \item Code: \texttt{[ i\^{}2 for i in 1:5 when mod(i,2) == 1 ]}
      \item Result: \texttt{[1, 9, 25]}
      \end{itemize}
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{Macro-expressions (8/8): Functions}
  \begin{itemize}
  \item Can take any number of arguments
  \item Dynamic binding: is evaluated when invoked during the macroprocessing stage, not when defined
  \item Can be included in expressions; valid operators depend on return type
  \end{itemize}

  \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}
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{Defining macro-variables (1/2)}

  The value of a macro-variable can be defined with the \verb+@#define+
  directive.

  The macro processor has its own list of variables, which are different from model variables and MATLAB/Octave variables

  \begin{block}{Syntax}
    \verb+@#define +\textit{variable\_name}\verb+ = +\textit{expression}
  \end{block}

  \begin{block}{Examples}
\begin{verbatim}
@#define x = 5              // Real
@#define y = "US"           // String
@#define v = [ 1, 2, 4 ]    // Real array
@#define w = [ "US", "EA" ] // String array
@#define z = 3 + v[2]       // Equals 5
@#define t = ("US" in w)    // Equals true
\end{verbatim}
  \end{block}
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{Defining macro-variables (2/2)}
  Macro variables can also be defined on the Dynare command line by using the
  \texttt{-D} option, for easily switching between different flavours of a model.

  \begin{block}{Example 1}
\begin{verbatim}
dynare myfile.mod -Dx=5
\end{verbatim}
The macro-variable \texttt{x} will be equal to \texttt{5} when running \texttt{myfile.mod}.
\end{block}

\begin{block}{Example 2}
  Use single quotes around the \texttt{-D} option when there are spaces or
  special characters in the variable definition.
\begin{verbatim}
dynare myfile.mod '-DA=[ i in [1,2,3] when i > 1 ]'
\end{verbatim}
The macro-variable \texttt{A} will be equal to \texttt{[2,3]} when running \texttt{myfile.mod}.
\end{block}
\end{frame}


\begin{frame}[fragile=singleslide]
  \frametitle{Expression substitution}
  \framesubtitle{Dummy example}
  \begin{block}{Before macro processing}
\begin{verbatim}
@#define x = 1
@#define y = [ "B", "C" ]
@#define i = 2
@#define f(x) = x + " + " + y[i]
@#define i = 1

model;
  A = @{y[i] + f("D")};
end;
\end{verbatim}
  \end{block}
  \begin{block}{After macro processing}
\begin{verbatim}
model;
  A = BD + B;
end;
\end{verbatim}
  \end{block}
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{Include directive (1/2)}
  \begin{itemize}
  \item This directive simply inserts the text of another file in its place
    \begin{block}{Syntax}
      \verb+@#include "+\textit{filename}\verb+"+
    \end{block}
    \begin{block}{Example}
\begin{verbatim}
@#include "modelcomponent.mod"
\end{verbatim}
    \end{block}
  \item Equivalent to a copy/paste of the content of the included file
  \item Note that it is possible to nest includes (\textit{i.e.} to include a
    file with an included file)
  \end{itemize}
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{Include directive (2/2)}
  \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}
  \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
    \texttt{[paths]} section in config files.
  \end{itemize}
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{Loop directive (1/4)}
  \begin{block}{Syntax 1: Simple iteration over one variable}
\verb+@#for +\textit{variable\_name}\verb+ in +\textit{array\_expr} \\
\verb+   +\textit{loop\_body} \\
\verb+@#endfor+
  \end{block}
  \begin{block}{Syntax 2: Iteration over several variables at the same time}
\verb+@#for +\textit{tuple}\verb+ in +\textit{array\_expr} \\
\verb+   +\textit{loop\_body} \\
\verb+@#endfor+
  \end{block}
  \begin{block}{Syntax 3: Iteration with some values excluded}
\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]
  \frametitle{Loop directive (2/4)}
  \begin{block}{Example: before macro processing}
    \small
\begin{verbatim}
model;
@#for country in [ "home", "foreign" ]
  GDP_@{country} = A * K_@{country}^a * L_@{country}^(1-a);
@#endfor
end;
\end{verbatim}
    \normalsize
  \end{block}

  \begin{block}{Example: after macro processing}
    \small
\begin{verbatim}
model;
  GDP_home = A * K_home^a * L_home^(1-a);
  GDP_foreign = A * K_foreign^a * L_foreign^(1-a);
end;
\end{verbatim}
    \normalsize
  \end{block}
\end{frame}

\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}

\begin{frame}[fragile=singleslide]
  \frametitle{Conditional directives (1/3)}

  \begin{columns}[T]
    \column{0.47\linewidth}
    \begin{block}{Syntax 1}
\verb+@#if +\textit{bool\_or\_real\_expr} \\
\verb+   +\textit{body included if expr is true (or != 0)} \\
\verb+@#endif+
    \end{block}

    \column{0.47\linewidth}
    \begin{block}{Syntax 2}
\verb+@#if +\textit{bool\_or\_real\_expr} \\
\verb+   +\textit{body included if expr is true (or != 0)} \\
\verb+@#else+ \\
\verb+   +\textit{body included if expr is false (or 0)} \\
\verb+@#endif+
    \end{block}
  \end{columns}
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{Conditional directives (2/3)}
  \begin{block}{Syntax 3}
     \scriptsize
   \verb+@#if +\textit{bool\_or\_real\_expr1} \\
    \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}

  \begin{block}{Example: alternative monetary policy rules}
    \scriptsize
\begin{verbatim}
@#define linear_mon_pol = false // or 0
...
model;
@#if linear_mon_pol
  i = w*i(-1) + (1-w)*i_ss + w2*(pie-piestar);
@#else
  i = i(-1)^w * i_ss^(1-w) * (pie/piestar)^w2;
@#endif
...
end;
\end{verbatim}
    \scriptsize
  \end{block}
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{Conditional directives (3/3)}

  \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}

\bigskip
\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
\emph{no} \verb+@#elseifdef+ or \verb+@#elseifndef+ directive; use
\verb+elseif defined(variable_name)+ to achieve the desired objective.
\end{itemize}
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{Echo directives}

  \begin{itemize}
  \item The echo directive will simply display a message on standard output
  \item The echomacrovars directive will display all of the macro variables (or
    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
  \end{itemize}

  \begin{block}{Syntax}
    \verb+@#echo +\textit{string\_expr} \\
    \verb+@#echomacrovars +\\
    \verb+@#echomacrovars +\textit{list\_of\_variables}\\
    \verb+@#echomacrovars(save)+\\
    \verb+@#echomacrovars(save) +\textit{list\_of\_variables}\\
  \end{block}

  \begin{block}{Examples}
\begin{verbatim}
@#echo "Information message."
\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}
@#error "Error message!"
\end{verbatim}
  \end{block}
\end{frame}

\begin{frame}
  \frametitle{Macro-related command line options}
  \begin{itemize}
  \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}.
  \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.
  \item \texttt{onlymacro}: Stops processing after the macro processing step.
  \end{itemize}
\end{frame}

\section{Common uses}

\begin{frame}[fragile=singleslide]
  \frametitle{Modularization}
  \begin{itemize}
  \item The \verb+@#include+ directive can be used to split \texttt{.mod} files into several modular components
  \item Example setup:
    \begin{description}
    \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
    \end{description}
  \item Dynare can be called on \texttt{simulate.mod} and \texttt{estim.mod}
  \item But it makes no sense to run it on \texttt{modeldesc.mod}
  \item Advantage: no need to manually copy/paste the whole model (during initial development) or port model changes (during development)
  \end{itemize}
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{Indexed sums or products}
  \framesubtitle{Example: moving average}
  \begin{columns}[T]
    \column{0.47\linewidth}
    \begin{block}{Before macro processing}
\begin{verbatim}
@#define window = 2

var x MA_x;
...
model;
...
MA_x = @{1/(2*window+1)}*(
@#for i in -window:window
        +x(@{i})
@#endfor
       );
...
end;
\end{verbatim}
    \end{block}
    \column{0.47\linewidth}
    \begin{block}{After macro processing}
\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}
  \framesubtitle{\texttt{.mod} file skeleton example}
  \scriptsize
\begin{verbatim}
@#define countries = [ "US", "EA", "AS", "JP", "RC" ]
@#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}
  \frametitle{Endogeneizing parameters (1/4)}
  \begin{itemize}
  \item When calibrating the model, it may be useful to pin down parameters by targeting endogenous objects
  \item Example:
    \begin{gather*}
      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}
    \end{gather*}
  \item In the model, $\alpha$ is a (share) parameter, and $lab\_rat_t$ is an endogenous variable
  \item We observe that:
    \begin{itemize}
    \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
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{Endogeneizing parameters (2/4)}
  \begin{itemize}
  \item Therefore, when computing the steady state by solving the static model:
    \begin{itemize}
    \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$
    \item the solution algorithm deduces the implied value for $\alpha$
    \end{itemize}
  \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
  \end{itemize}
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{Endogeneizing parameters (3/4)}
  \framesubtitle{Example implementation}
  \begin{itemize}
  \item File \texttt{modeqs.mod}:
    \begin{itemize}
    \item contains variable declarations and model equations
    \item For declaration of \texttt{alpha} and \texttt{lab\_rat}:
    \footnotesize
\begin{verbatim}
@#if steady
 var alpha;
 parameter lab_rat;
@#else
 parameter alpha;
 var lab_rat;
@#endif
\end{verbatim}
    \normalsize
    \end{itemize}

  \end{itemize}
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{Endogeneizing parameters (4/4)}
  \framesubtitle{Example implementation}
  \begin{itemize}
  \item File \texttt{steadystate.mod}:
    \begin{itemize}
    \item begins with \verb+@#define steady = true+
    \item followed by \verb+@#include "modeqs.mod"+
    \item initializes parameters (including \texttt{lab\_rat}, excluding \texttt{alpha})
    \item computes steady state (using guess values for endogenous, including \texttt{alpha})
    \item saves values of parameters and variables at steady-state in a file, using the \texttt{save\_params\_and\_steady\_state} command
    \end{itemize}
  \item File \texttt{simulate.mod}:
    \begin{itemize}
    \item begins with \verb+@#define steady = false+
    \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
    \item computes simulations
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{MATLAB/Octave loops vs macro processor loops (1/3)}
  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:
  \begin{block}{With a MATLAB/Octave loop}
\begin{verbatim}
rhos = [ 0.8, 0.9, 1];
for i = 1:length(rhos)
  set_param_value('rho',rhos(i));
  stoch_simul(order=1);
  if info(1)~=0
    error('Simulation failed for parameter draw')
  end
end
\end{verbatim}
  \end{block}
  \begin{itemize}
  \item The loop is not unrolled
  \item MATLAB/Octave manages the iterations
  \item NB: always check whether the error flag \texttt{info(1)==0} to prevent erroneously relying on stale results from previous iterations
  \end{itemize}
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{MATLAB/Octave loops vs macro processor loops (2/3)}
  \begin{block}{With a macro processor loop (case 1)}
\begin{verbatim}
rhos = [ 0.8, 0.9, 1];
@#for i in 1:3
  set_param_value('rho',rhos(@{i}));
  stoch_simul(order=1);
  if info(1)~=0
    error('Simulation failed for parameter draw')
  end
@#endfor
\end{verbatim}
  \end{block}
  \begin{itemize}
  \item Very similar to previous example
  \item Loop is unrolled
  \item Dynare macro processor manages the loop index but not the data array (\texttt{rhos})
  \end{itemize}
\end{frame}

\begin{frame}[fragile=singleslide]
  \frametitle{MATLAB/Octave loops vs macro processor loops (3/3)}
  \begin{block}{With a macro processor loop (case 2)}
\begin{verbatim}
@#for rho_val in [ 0.8, 0.9, 1]
  set_param_value('rho',@{rho_val});
  stoch_simul(order=1);
  if info(1)~=0
    error('Simulation failed for parameter draw')
  end
@#endfor
\end{verbatim}
  \end{block}
  \begin{itemize}
  \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
  \end{itemize}
\end{frame}

\begin{frame}
  \begin{center}
    \vfill {\LARGE Thanks for your attention! \\
    Questions?}
    \vfill
    {\LARGE My email: \texttt{sebastien@dynare.org}}
    \vfill
  \end{center}
  \vfill
  \begin{columns}[T]
    \column{0.2\textwidth}
    \column{0.09\textwidth}

    \ccbysa
    \column{0.71\textwidth}
    \tiny
    Copyright © 2008-2024 Dynare Team \\
    License: \href{http://creativecommons.org/licenses/by-sa/4.0/}{Creative
      Commons Attribution-ShareAlike 4.0}
  \end{columns}
\end{frame}

\end{document}