Conditional Compilation in Models

This forum is closed. You can read the posts but cannot write. We have migrated the forum to a new location where you will have to reset your password.
Forum rules
This forum is closed. You can read the posts but cannot write. We have migrated the forum to a new location (https://forum.dynare.org) where you will have to reset your password.

Conditional Compilation in Models

Postby hecticmetric » Wed May 21, 2008 11:18 pm

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.
hecticmetric
 
Posts: 5
Joined: Wed May 21, 2008 9:37 pm

Re: Conditional Compilation in Models

Postby StephaneAdjemian » Thu May 22, 2008 7:49 am

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:

Code: Select all
z-g(x) = 0


if y>0, and

Code: Select all
z-f(x) = 0


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

Code: Select all
(y>0)*(z-g(x)) + (1-(y>0))*(z-f(x)) = 0


or equivalently:

Code: Select all
(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.
StephaneAdjemian
 
Posts: 429
Joined: Wed Jan 05, 2005 4:24 pm
Location: Paris, France.

Re: Conditional Compilation in Models

Postby hecticmetric » Thu May 22, 2008 6:51 pm

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.
hecticmetric
 
Posts: 5
Joined: Wed May 21, 2008 9:37 pm

Re: Conditional Compilation in Models

Postby StephaneAdjemian » Thu May 22, 2008 8:23 pm

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.

hecticmetric wrote: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.
Stéphane Adjemian
Université du Maine, GAINS and DynareTeam
https://stepan.adjemian.eu
StephaneAdjemian
 
Posts: 429
Joined: Wed Jan 05, 2005 4:24 pm
Location: Paris, France.

Re: Conditional Compilation in Models

Postby hecticmetric » Thu May 22, 2008 8:51 pm

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.

Thanks in advance.
Jon
Attachments
rbc.mod
Jesus Fernandez-Villaverde's modified rbc.mod file.
(1.9 KiB) Downloaded 92 times
hecticmetric
 
Posts: 5
Joined: Wed May 21, 2008 9:37 pm

Re: Conditional Compilation in Models

Postby StephaneAdjemian » Fri May 23, 2008 8:21 am

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.
Attachments
ramst.mod
Ramsey with threshold.
(1.02 KiB) Downloaded 120 times
Stéphane Adjemian
Université du Maine, GAINS and DynareTeam
https://stepan.adjemian.eu
StephaneAdjemian
 
Posts: 429
Joined: Wed Jan 05, 2005 4:24 pm
Location: Paris, France.

Re: Conditional Compilation in Models

Postby hecticmetric » Wed May 28, 2008 12:13 pm

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:

Code: Select all
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.

Thanks in advance.
hecticmetric
 
Posts: 5
Joined: Wed May 21, 2008 9:37 pm

Re: Conditional Compilation in Models

Postby hecticmetric » Wed May 28, 2008 7:18 pm

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:

Code: Select all
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.
hecticmetric
 
Posts: 5
Joined: Wed May 21, 2008 9:37 pm


Return to Dynare help

Who is online

Users browsing this forum: No registered users and 13 guests