Global variables and Dynare

Dear all,
I have encountered a problem with the new version of Matlab (2008b) and Dynare 4.

I need to compute the steady state of a model outside dynare to give a correct value for same parameters.
In the previous version of Matlab I could define a function that, set as global the value of the parameters read in Dynare, could compute the steady state.
However, with the new version of matlab, if I declare parameters as global inside a function, those parameters get as initial value an empty matrix.
In other words, in the previous of matlab declaring some parameters as global inside a function did not alter the value that those parameters were assigned in Dynare. In the new version of Matlab, when you declare a parameter as a global variable, its value is reset to an empty matrix.

The question is:
Is there any way to declare some variable as global INSIDE the .mod file in Dynare? That would solve the problem.
Thanks in advance

P.S. I am aware that I can “go around” the problem using some tricks, but I am really curious to know if we can actually declare global variable in Dynare. I haven’t found something about that in the help.


You should not use global variables to do this. First because globals have to be avoided if possible. Second because I am not sure that dynare (version 4, it’s ok with version 3) will use (in all situations) the new values of the parameters. Dynare (version 4) stores the values of the parameters in M_.params and use this vector when parameter values are needed (for instance to compute the likelihood). You should change the values of M_.params in the steady state file. For instance, if you want to change the value of a parameter called alpha, you just have to write something like:

idx_alpha = strmatch('alpha',M_.param_names,'exact');
M_.params(idx_alpha) = alpha;

in the steady state file (after the computation of alpha). A good idea would be to declare idx_alpha as a persistent variable, so that you don’t need to get the position of parameter alpha in M_.params each time you call the steady state file.

Your problem is (very) weird. I also use matlab 2008b and I think I do not have this problem. I tried with a simple example: a function called f1.m where I give a value to a global variable x and a script called f2.m where I call f1 and print the value of x.

function f1()
global x
x = 10
global x

When i execute this script I get x = 10. Obviously if I comment out the call to f1 in the script I obtain x = ]. Do you call the steady state file before looking at the value of the parameters ?


Stéphane, thank you for your answer. I will make use of M_.params instead.

About the example you posted, the problem is that if you comment the line global x in the main file, then with R2008b the code will crush because you cannot define anymore a global variable just in a function and not in the main file.

in fact, if you change the main file as follows (i.e. commenting the global declaration)

%global x

the output will be the empty matrix.

In the previous version of matlab, even though you define a global variable inside a function, the program will reset also the variables in the main code as global, although not changing its value. In the new version, it assigns to it an empty matrix.

This is inconvenient for Dynare because you cannot define the variables as global in the .mod file, so that you cannot use global variables anymore in the .m functions called by the mod file.

I hope I was clear enough :slight_smile:

I think there are many ways to deal with this problem, and using the M.params is one of them, but I was mainly curious if there was a way to define globals in the mod file.

Thanks for your help

I was not aware of this strange behavior of matlab (before 2008b) because I always declare the global in the main file.

But, indeed, you can declare global variables in the mod file. I must confess that I use this trick sometimes :slight_smile:

Best, Stéphane.