# Conditional Compilation in Models

Hi,
I using Dynare in conjunction with Matlab and am trying create a model which uses equations, conditional on the results from a previous iteration.
For example, if y in the previous period is less than some value use equation 1 if not then use equation 2.
I have been attempting to do this procedure using conditional compilation with a statement like:
@if yt-1 <100
eq1
@else
eq2
@end
but have not had any luck. Can anyone point me in the right direction?

Thanks.

Hi Hecticmetric,

First remark, it is not a good idea to use such tricks if you want to solve a stochastic model with Dynare, because we only consider local approximations (first order or second order) around the deterministic steady state (pertutbation method) and can not handle this kind of nonlinearity. In this case a global approximation approach would be needed.

For deterministic models (solved by a relaxation method using the dynare command simul) this kind of nonlinearity can be handled, but the new Dynare macro language can not be used for this purpose. Suppose that the equation is:

``z-g(x) = 0``

if y>0, and

``z-f(x) = 0``

otherwise, where g() and f() are two continuous functions. This can be coded as follows in the model block :

``(y>0)*(z-g(x)) + (1-(y>0))*(z-f(x)) = 0``

or equivalently:

``(y>0)*g(x) + (1-(y>0))*f(x) = z``

The test function > has been added recently in Dynare (and is still undocumented), so you need a recent version of Dynare v4. If a is greater than b, then (a>b) is equal to one otherwise (a>b) is equal to zero.

Best,
Stéphane.

Hi Stephane,

Thank you very much for your suggestions & comments. I will have to do some thinking on the nonlinear relationship that this conditionality implies.

I have implemented your suggestion, such that I now have an equation similar to the following:

``````(y_l-y*1000)*(y(-1)>0.005) + (y_l-y*100)*(y(-1)<0.005)= 0;
``````

However, this is not working as I anticipated.

The values for y are decreasing from the first iteration with a beginning value of approx. 0.01 and an ending value of approx. 0.002. However the values for y_l are equal to y*1000, regardless of the y value.

Do you have any thoughts on why this is or how I might remedy this problem?

Thanks.

If you post your mod file or a simpler version that reproduces the bug, I will give a closer look at your problem as soon as possible.

Best,
Stéphane.

[quote=“hecticmetric”]Hi Stephane,

Thank you very much for your suggestions & comments. I will have to do some thinking on the nonlinear relationship that this conditionality implies.

I have implemented your suggestion, such that I now have an equation similar to the following:

``````(y_l-y*1000)*(y(-1)>0.005) + (y_l-y*100)*(y(-1)<0.005)= 0;
``````

However, this is not working as I anticipated.

The values for y are decreasing from the first iteration with a beginning value of approx. 0.01 and an ending value of approx. 0.002. However the values for y_l are equal to y*1000, regardless of the y value.

Do you have any thoughts on why this is or how I might remedy this problem?

Thanks.[/quote]

The mod file I am writing is still in development and I have not yet added this procedure to the code.
However, I tested the code’s application in Jesus Fernandez-Villaverde’s sample mod file. I will attach the file with the slight adjustments so that you can see where I am having the problem.

Jon
rbc.mod (1.9 KB)

Hi Jon,

Here is an example of my own where I use the test function <. It is a deterministic (perfect foresight) Ramsey model with a threshold level on the physical capital stock. If the capital stock is greater than this threshold level the elasticity of output with respect to capital is increased by 10%. This nonlinearity is anticipated by the households.

As you can see, there is no problem with the test function.

In your example, the test function does not affect the simulations but I think this is normal. For the reported moments and IRFs, only the case y>0.005 matters, because the model is approximated in a neighborhood of the deterministic steady state where y is around 1. Again considering this kind of constraint with a local approach is doubtful.

Best,
Stéphane.
ramst.mod (1.02 KB)

Stephane,

Ahhh, I see. Thanks again.

I do have a quick related question, if you wouldnt mind helping me out. The logical indicator which I am using to determine the compilation of equations is returning errors.

I have the code:

``````cnstr = ((cnstr(-1)==1)*Mu + (cnstr(-1)==0)*Mc)<rnd;

estimated_params;
rnd, uniform_pdf, , , 0,1;
end;``````

where Mu and Mc are constant parameters, cnstr is a variable, and rnd is an estimated parameter - a very simple Markov process. The output from Dynare indicates that the mean and variance of the variable constr is zero and, predictably, all of the correlations are NAN. I get this output regardless of the values of Mu and Mc (including both zero and one).

If you have any suggestions on how to get this to work, they would be greatly appreciated.

Stephane,

Do to confusion, I had been trying to use logical indexing in a stochastic model (despite your warning).

I am now correcting this and wonder if it might be possible to use the logical indexing function in conjunction with a stochastic-deterministic model? I have a pre-specified index variable which is a periodic shock observable in every time period from 1:T (and labeled the variable as varexo_det). Such that:

``````varexo_det cnstr;
...
(l - d)*(cnstr) - (l - b)*(1 - (cnstr)) = 0;
...
shocks;
var cnstr;
periods 1 2 3 4 5 6 etc.;
values 0 1 0 1 1 1 etc;
end;
...
stoch_simul(periods=2100);``````

Should this run successfully or is the simul command truly the only one which will allow this type of indexing?

Thanks again.