Grid search inside the model block


I am coming from a Julia/Matlab background and I am new to Dynare, so I hope my question makes sense in this context.

I am considering a model in which wages (called phiZero(x) below) are implicitly defined by the relation
where x is a variable indexing skills on a grid (with nX different values)

To find the corresponding wage phiZero(x), I have defined a fine grid for wages (wGrid in the code below, with nWages different values), and I was hoping to perform a naive grid search:

//Loop over skills
@#for iX in 1 : nX
        //For each skill level, look for the corresponding wage phiZero_@{iX}
		//Initial guess
		deltaPhiZeroGuess = 9999

		//loop over possible wages
		//to find the index of the wage iW
		//such that workersSurplus_@{iX}_@{iW} = 0
		@#for iW in 1 : nWages
			//Compare values
			@# if abs(workersSurplus_@{iX}_@{iW}) < deltaPhiZeroGuess
				phiZero_@{iX} = wGrid_@{iW}; //Wage value
				phiZeroIndex_@{iX} = iW;//Index on the grid

				//Update the guess
				deltaPhiZeroGuess = abs(workersSurplus_@{iX}_@{iW})

			@# endif

where workersSurplus_@{iX}_@{iW} are defined in another part of my code. Can Dynare accommodate such a setting? If not, is there any to calculate the wages which are implicitly defined in my model.

Any help or suggestion would be greatly appreciated.

Many thanks.

Hi. from your description, it is not clear what you are trying to do. Are you trying to solve a stochastic model on a discretized state space? If yes, Dynare does not really handle this kind of setup. The focus is on perturbation solutions.


Thank you for your answer.

I am considering using the method described in Winberry (2016) to solve a stochastic rational expectation model with heterogeneous agents in Dynare (

I am able to solve (numerically) for the steady-state of my model. As in Reiter (2009) or Winberry (2016), I want to approximate out-of-the-steady-state dynamics using perturbation.

The model I am considering has a structure very similar to the one in Krusell Smith (1998). The uncertainty comes from an aggregate productivity shock. But because of the heterogeneous agent framework, the state space contains a density. I define my state variable as the aggregate productivity level and some moments characterising the distribution of employment along an ability dimension (the _@{iX} dimension in the code above).

The main challenge I face so far is that wages are implicitly defined by the equation above. I don’t know if it makes sense to perform a “grid search” within the model block. I have the feeling that Dynare is expecting an expression of the form phiZero_@{iX} = someFunction(...). Is that correct?

Thank you for your help.

I haven’t really looked at Winberry’s algorithm (still on my to-do list), so I may be of limited help here. Have you considered asking him directly?
What do you mean with grid search? If you want to pin down a parameter of your model, you can always use a steady state file to do numerical computations like a search over a grid. In contrast, things become really complicated if endogenous variables outside of the steady state are involved. In this case, the endogenous reaction of these variables in equilibrium needs to be taken into account.


Thank you for taking the time to answer me.

By grid search I mean that the model implies that wages are endogeneously determined at time t by the relation workersSurplus(x,phiZero(x))=0 where workersSurplus(x,w) is a value function at time t , which I can calculate for any x and w. I cannot find an analytical expression of the form phiZero(x)=someFunction(...). I have to rely on the implicit definition above.

I know that the function workersSurplus(x,w) is decreasing in w. Wages belong to an interval W = [w_min,w_max]. I define a grid of nWages points in W (wGrid_@{1}, wGrid_@{2}, ..., wGrid_@{nWages} in my code above).

For a fixed x, I am looking for the wage on that grid that sets asb(workersSurplus(x,w)) the closest to zero. If my grid of wages is fine enough, then the value I find should be a good approximation to the true value. To do so, I am using a for loop using Dynare macro processor.

The thing is that I am not sure that using this grid search (or brute force minimisation) within the model block makes sense. I haven’t seen such a thing so far.

I will try to get in contact with Winberry. You are right, he would certainly be of great help here.


Dear Julien,

I probably do not understand the details of your problem, but I do not think that developing the grid in the model block is the right approach. This should be done in a matlab routine that is called as an external function in the model block.


@JulienP I agree with @stepan-a. Your model solution needs to take this relationship into account, both for the level and the derivatives. It is essentially a first order condition. Because of this, you need to rely on an external function where the derivatives are computed via finite differences. The approach is similar to some financial accelerator models where the (log-)normal CDF/PDF of endogenous variables shows up.

Thank you both for your answers.

I will use an external function to “do the job”.

jpfeifer, you mentioned models where the (log-)normal CDF/PDF of endogenous variables shows up. This is extremely interesting for what I am currently doing. Do you have references papers and/or Dynare codes that I could look at to gain further insights?

Many thanks.

The best I could find was Integration function - dynare v4?