# Multi-Country Conditional Definitions

Hi,

I’m trying to move from standard multi-country notation on paper to dynare code. For example, if output from country j to country i is denoted as y_ji, then the expression of it is equal to say x_ji. However, if it is output from country i that is sold in country i (i.e. y_ii), then it is equal to say expression z_ii.

Suppose I use

@#define n = [ “i”, “j” ]

then

var
@#for k in n
y_@{k}@{k}
@#endfor

produces y_ii, y_ji, y_ij and y_jj. However, how can I use a conditional expression for selecting y’s if the first k is equal to the second k (i.e. y_ii and y_jj, but not y_ij or y_ji) and vice versa (i.e. y_ij and y_ji, but not y_ii or y_jj)? This would come in handy when specifying the model equations that are country-specific parsimoniously. That is, without typing one for every country separately - especially when you have a large number of countries n.

Should I specify another denomi>ator such as g and use the following?

@#for k,g in n
@#if k=g
y_@{k}@{g}=z_@{k}@{g}
@#else
y_@{k}@{g}=x_@{k}@{g}
@#endif
@#endfor

I haven’t tried running this and I really doubt that k,g in n is admissible in matlab, so do let me know if you know anything workable.

Also, is it somehow possible to then define a summation of all variables with index k for only one element of g? For example, \sum_{k\in n}y_{kg} for all g\in n?

Thank you!

Did you really try your first loop?. Normally it should only produce the variables with common values for the two indices (i.e. y_{ii} and y_{jj}). To obtain the four variables you would have to use two nested loops:

var
@#for k in n
@#for l in n
y_@{k}@{l}
@#endfor
@#endfor


Then you can add a condition in the inner loop to filter out some of the variables.

Best,
Stéphane

Hi Stéphane,

Yes, the first loop does indeed work (at least it did on 4.3.3 version). Perhaps it is not very good practice, because you are right - matlab would normally only produce the ‘diagonals’, but not the ‘off-diagonals’. In dynare, these loops produce both diagonals and off-diagonals, which is why I am a little confused and hence the question about conditionality. Thanks for a quick response!

Justas

Thanks Justas. I must check. I wonder if this is not a bug. This feature would be quite unintuitive… Why don’t you use 4.5.1?

Best,
Stéphane.

I am currently working on a Mac since I am away from the office for the summer where I have windows, but I will switch to 4.5.1 as soon as I come back. Although I wonder if this issue hasn’t carried over to 4.5.1 as well?

Okay, so I made the test. Put the following in a mod file (file attached below):

@#define MaxIndex = [3]
@#define Indices = 1:MaxIndex[1]

// Expansion with only one index
@#for i in Indices
A_@{i}_@{i}
@#endfor

// Expansion with two indices
@#for i in Indices
@#for j in Indices
B_@{i}_@{j}
@#endfor
@#endfor

// Remove all the terms on the diagonal
@#for i in Indices
@#for j in Indices
@#if i != j
B_@{i}_@{j}
@#endif
@#endfor
@#endfor

// The same without the conditional statement (use a diff between two arrays instead)
@#for i in Indices
@#for j in Indices-[i]
B_@{i}_@{j}
@#endfor
@#endfor


This has no sense for Dynare, but the macro processor can expand the loops (and then you will obtain an error because Dynare does not understand what you want to do). You can call dynare from the terminal. First you need to know where is located the dynare binary. I will assume that

/Users/toto/dynare/4.5.1/matlab/preprocessor64/dynare_m


is the full path to the dynare binary (depending on your architecture you may have to replace preprocessor64 by preprocessor32). Then in a terminal (assuming you are in the folder where you saved the mod file), just type:

~$/Users/toto/dynare/4.5.1/matlab/preprocessor64/dynare_m example.mod savemacro  You will obtain an error, like this one: Starting Dynare (version 4.6-unstable). Starting preprocessing of the model file ... ERROR: example.mod: line 35, cols 1-0: syntax error, unexpected$end


But you will have a new file in the same folder called example-macroexp.mod. The content of this file contains the expanded version of the loops:

@#line "example.mod" 1

// Expansion with only one index

@#line "example.mod" 6
A_1_1

@#line "example.mod" 6
A_2_2

@#line "example.mod" 6
A_3_3

@#line "example.mod" 8

// Expansion with two indices

@#line "example.mod" 11

@#line "example.mod" 12
B_1_1

@#line "example.mod" 12
B_1_2

@#line "example.mod" 12
B_1_3

@#line "example.mod" 14

@#line "example.mod" 11

@#line "example.mod" 12
B_2_1

@#line "example.mod" 12
B_2_2

@#line "example.mod" 12
B_2_3

@#line "example.mod" 14

@#line "example.mod" 11

@#line "example.mod" 12
B_3_1

@#line "example.mod" 12
B_3_2

@#line "example.mod" 12
B_3_3

@#line "example.mod" 14

@#line "example.mod" 15

// Remove all the terms on the diagonal

@#line "example.mod" 18

@#line "example.mod" 19

@#line "example.mod" 22

@#line "example.mod" 19

@#line "example.mod" 20
B_1_2

@#line "example.mod" 22

@#line "example.mod" 19

@#line "example.mod" 20
B_1_3

@#line "example.mod" 22

@#line "example.mod" 23

@#line "example.mod" 18

@#line "example.mod" 19

@#line "example.mod" 20
B_2_1

@#line "example.mod" 22

@#line "example.mod" 19

@#line "example.mod" 22

@#line "example.mod" 19

@#line "example.mod" 20
B_2_3

@#line "example.mod" 22

@#line "example.mod" 23

@#line "example.mod" 18

@#line "example.mod" 19

@#line "example.mod" 20
B_3_1

@#line "example.mod" 22

@#line "example.mod" 19

@#line "example.mod" 20
B_3_2

@#line "example.mod" 22

@#line "example.mod" 19

@#line "example.mod" 22

@#line "example.mod" 23

@#line "example.mod" 24

// The same without the conditional statement

@#line "example.mod" 27

@#line "example.mod" 28
B_1_2

@#line "example.mod" 28
B_1_3

@#line "example.mod" 30

@#line "example.mod" 27

@#line "example.mod" 28
B_2_1

@#line "example.mod" 28
B_2_3

@#line "example.mod" 30

@#line "example.mod" 27

@#line "example.mod" 28
B_3_1

@#line "example.mod" 28
B_3_2

@#line "example.mod" 30

@#line "example.mod" 31


So it works like a charm (as expected) with the stable version of Dynare (I used the unstable version, but for this matter they are identical). As you can see we can build the list of variables with different index values without using a conditional statement in the inner loop (but a set difference).

I do not have time to test with 4.3.3 (I would have to grab the sources and recompile), I am on my summer vacation too

Best,
Stéphane.

example.mod (588 Bytes)

Thank you ever so much Stéphane! Much appreciated.

The solution is very similar to How to write a finite sum in dynare

Thanks, I will have a look!

Ok, so Stéphane’s advice to use ‘savemacro’ is really invaluable and I can now understand how to avoid if statements by simply using [-i]. However, I’m still struggling with the conditional summation. For example:

@#define n = [ “1”, “2”]
@#for i in n
@#for j in n-[i]
nx@{i}=y@{i}@{j}-y@{j}@{i}
@#endfor
@#endfor

computes the net exports in economy i, which is a sum of output from i to j (i.e. y_ij) less output from j to i (i.e. y_ji). However, suppose I have three (or more) countries instead of just two. Then how do I sum over all j’s given that I still want all the net exports computed for all i’s? I’ve tried

@#define n = [ “1”, “2”, “3” ]
@#for i in n
@#for j in n-[i]

nx@{i}=
@#for k in j
+y@{i}@{k}-y@{k}@{i}
@#endfor
;
@#endfor
@#endfor

but I got an error:

Starting preprocessing of the model file …
ERROR in macro-processor: grahab.mod:97.23-32: Argument of @#for loop must be an array expression

I’ve also tried replacing @#for k in j with @#for k in n[-i] and get another error

Starting preprocessing of the model file …
ERROR in macro-processor: grahab.mod:98.34-35: Unary operator - does not exist for this type

I had a look at the link that Johannes sent me, but I couldn’t come up with anything workable.

Any further suggestions would be very welcome. Thanks

@#for k in j


is wrong because j is not an array. Also

@#for k in n[-i]


is wrong because the macro processor does not know how to interpret n[-i], which should probably be replaced by n-[i], but as I am not sure to understand what you are trying to do…

It is possible to write equations with \LaTeX on this forum, if you insert them between double dollars (or single dollar for inline mathematical expressions). Could you write the maths of what you are trying to do?

Best,
Stéphane.

Yes, here is the idea:

nx_{i,t}=\sum\limits_{k\in n_{-i}}y_{ik,t}-y_{ki,t}=y_{i1,t}+...+y_{i(n-1),t}-\left[y_{1i,t}+...+y_{(n-1)i,t}\right]

But I realised that I was very close and the only part that was stopping me was the typo you pointed out. So this computes what I want:

@#define n = [ “1”, “2”, “3” ]
@#for i in n

nx@{i}=
@#for k in n-[i]
+y@{i}@{k}-y@{k}@{i}
@#endfor
;
@#endfor

Thanks again for the help.