Loop over parameters

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.

Loop over parameters

Postby domel_s » Wed Aug 14, 2013 11:44 am

Hi,

I know that this problem was broached on this forum many times since I found some topics connected with it but I didn't find any solution for my problem.

I want to look at some statistics of variables in the model when some parameters change. The steady state is calculated by hand and included in 'initval'. For creating the loop over parameters, I used simple syntax like this:

Code: Select all
rhos = 0.8:0.05:1.05;
for i=1:length(rhos);
rho = rhos(i);
stoch_simul(order=1);
if info;
disp(['Computation fails for rho = ' num2str(rho)]);
end;
// results can be saved here
end;


This code with the loop above included is only working when I am looping over parameters that are not used for calculating steady state in the 'initval' part. Otherwise it gives me following error (though I included 'initval' part in the loop):

Code: Select all
dynare:k_order_perturbation: Caught Kord exception: NaN or Inf asserted in first order derivatives in FirstOrder::solve
Error using mexErrCheck (line 41)
Error encountered in: k_order_perturbation.

Error in k_order_pert (line 40)
    mexErrCheck('k_order_perturbation', err);

Error in stochastic_solvers (line 65)
        [dr,info] = k_order_pert(dr,M_,options_,oo_);

Error in resol (line 118)
    [dr,info] = stochastic_solvers(dr,check_flag,M,options,oo);

Error in stoch_simul (line 76)
    [oo_.dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_);

Error in b120813_1703_equity_premium_grid_search (line 407)
info = stoch_simul(var_list_);

Error in dynare (line 120)
evalin('base',fname) ;


I would be thankful for any help.
Regards
domel_s
 
Posts: 4
Joined: Wed Aug 14, 2013 10:27 am

Re: Loop over parameters

Postby jpfeifer » Thu Aug 15, 2013 6:56 am

You are not actually setting the parameter. What you should do is use the the set_param_value command. Moreover, if you have an analytic steady state, use a steady_state_model block instead of initval. You can find an example for such a loop here: http://www.dynare.org/phpBB3/viewtopic.php?f=1&t=4699. Basically, you need to write a Matlab m-file with the following parts:

Code: Select all
rhos = 0.8:0.05:1.05;
first_time = 1;
for i=1:length(rhos)
    if first_time
        set_param_value('rho',rhos(i));
        dynare your_mod_file_here noclearall;
        first_time = 0;
    else
        set_param_value('rho',rhos(i));
        info = stoch_simul(var_list_);
        if info;
          disp(['Computation fails for rho = ' num2str(rho)]);
        end;
    end
end


Inside your mod-file you still need to use the steady_state_model block.

Addendum:

The set_param_value command only refers to structural parameters. You cannot set standard deviations or measurement error defined in the shocks-block this way. You either have to set M_.Sigma_e directly, or calibrate the shock variances to 1 and use in the model
Code: Select all
+sigma_eps*eps_z

instead of
Code: Select all
+eps_z

The structural parameter sigma_eps now contains the standard deviation and can be set using the set_param_value command. Similarly, correlated shocks can be entered by specifying a joint shock.
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: Loop over parameters

Postby domel_s » Wed Aug 21, 2013 6:10 pm

Sorry for late response but I was on vacations.

Thank you for your helpful advice. I tried to implement all your hints and it paid off. I used set_param_value command and calculated steady state in the external file. Then it all worked so that I could performed loop.

Thank you one more time!
domel_s
 
Posts: 4
Joined: Wed Aug 14, 2013 10:27 am

Re: Loop over parameters

Postby kipfilet » Thu Dec 05, 2013 2:20 pm

I am implementing a poor man's version of the method of moments using this procedure: I have three AR(1) shocks, set up a grid for each of the shocks' autocorrelation and variance, and evaluate the model at each point in the grid, from where I take the moments of some variables and pick the parameter point that does the best.
My problem is that even if I pick a grid of size 4 for each parameter, this corresponds to 4^6 = 4096 evaluations of the stoch_simul function. Any way you think that I could speed up the process? Especially considering that the procedure may have to be repeated many times.
kipfilet
 
Posts: 27
Joined: Thu Oct 04, 2012 5:24 am

Re: Loop over parameters

Postby jpfeifer » Fri Dec 06, 2013 9:04 am

I am afraid there isn't. You should just make sure that Dynare does not print or plot or compute stuff you don't need within stoch_simul.
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: Loop over parameters

Postby StephaneAdjemian » Fri Dec 06, 2013 9:56 am

Hi,

If you are willing to estimate a DSGE model with a simulation based method you should not call Dynare in a loop. This is very inefficient, because there is no need to parse the model at each iteration. We do not provide any interface for SMM, I hope to release this in Dynare 4.5, so you need to write a routine that computes an objective (based on the moment conditions) that calls Dynare's simult or simult_ routines, and minimize this objective usig the provided optimization routines...

More generally it is always simpler and more efficient to perform the loops in the mod file (where the Matlab statements are legal) rather than around calls to Dynare.

Best,
Stéphane.
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: Loop over parameters

Postby jpfeifer » Fri Dec 06, 2013 12:35 pm

Either Stéphane or I misunderstood you. To clarify: my original post shows how to do exactly what Stéphane proposes. Dynare is only called once to parse the model. Subsequently, only the Matlab routines are called. In your case, you are trying to run an SMM. This requires setting the parameters, computing the steady state, solving the model, and then simulating it. This is the sequence you get when repeatedly calling stoch_simul from a Matlab file (as opposed to running full Dynare everytime). My second post recommends that when calling stoch_simul several times, you shut off all things there that you do not need (alternatively, the fastest way is just calling the routines from stoch_simul you need (basically resol(0,M_,options_,oo_); and simult), but that is more advanced).
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: Loop over parameters

Postby kipfilet » Fri Dec 06, 2013 3:59 pm

Ok, so what I am using is pretty much a poor man's version of SMM as you pointed out. I run

dynare modelname

once, before the loop, and then call stoch_simul for each point in the parameter grid. Keep the moments I need from the simulations and then use a simple criterion function to pick the best point in the parameter space.

I was reading the function description for simult but am not totally sure if I understood exactly what it does: the description says that I can feed exogenous shocks and decision rules, and it gives me the path for endogenous variables. I might be wrong, but I have the impression that decision rules may change for different parameter values, and I have little hope of getting closed forms for anything since this is a 2nd order approximation.

I am going to check resol out as well. And I didn't know you could write loops inside a mod file!! How would that work? Would I need a modle block for each grid point? Can a loop-inside-mod be used for what I am trying to implement? Sorry for all these questions, and thank you very much for your time.
kipfilet
 
Posts: 27
Joined: Thu Oct 04, 2012 5:24 am

Re: Loop over parameters

Postby kipfilet » Fri Dec 06, 2013 4:26 pm

Since I am only changing parameters that govern the persistence and variance of the shocks, the steady state should be unaffected and there is no need to recompute it every time. So from what I gathered, the fastest way with external loops would be
Code: Select all
dynare mymodel

for i = 1:N

set_param_value('paramx',paramxgrid(i))

[dr,~,M,~,oo] = resol(0,M_,options_,oo_);
y_            = simult_(oo_.steady_state,dr,oo_.exo_simul,2);

storey(i) = y_
end


Any problem with this approach? I am just not sure at what stage I should use the set_param_value ! Thanks in advance!
kipfilet
 
Posts: 27
Joined: Thu Oct 04, 2012 5:24 am

Re: Loop over parameters

Postby kipfilet » Fri Dec 06, 2013 4:47 pm

I just tried what I described above and it seems to work. Furthermore, I have just collapsed a 9-hour job to 10 minutes. If you can offer me confirmation that the above pseudo-code is doing the right stuff, you, sirs, have just saved my life :)
kipfilet
 
Posts: 27
Joined: Thu Oct 04, 2012 5:24 am

Re: Loop over parameters

Postby jpfeifer » Fri Dec 06, 2013 5:11 pm

The basic structure looks correct. However, as noted above, the set_param_value-command only works for standard deviations if they enter the model as an explicit parameter (and not in the shocks-block). Without seeing the mod-file and what paramx is it is hard to tell.

Moreover, I you are not correctly feeding in the shocks for your simulation. oo_.exo_simul is typically not a shock series that you are trying to simulate.

Rather, if you have all your standard deviations as explicit model parameters, you should be providing a standard normal shock matrix like
Code: Select all
shock_mat=randn(M_.exo_nbr,1000)

for 1000 periods of random numbers. As you are doing SMM, you should always use the same random number. Thus, move the generation of shock_mat outside of the loop. For an example on how to use simult_, see the AguiarGopinath2007.mod on my homepage.

Last comment: calling resol will also update the steady state, so your procedure is even valid when the steady stM_ate changes.
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: Loop over parameters

Postby kipfilet » Fri Dec 06, 2013 5:58 pm

Thank you for your swift reply. I am writing standard deviations as explicit parameters due to the issue you discuss, and indeed I was feeding the wrong shock sequence. Thank you so much, it is so so faster that I am considering just writing a function and feeding it to a numerical optimizer instead of doing it over a grid.
kipfilet
 
Posts: 27
Joined: Thu Oct 04, 2012 5:24 am

Re: Loop over parameters

Postby GOODLUCK » Tue Jan 07, 2014 5:35 am

Hi, all.
It seems you are discussing sth advance. I am wondering what's the purpose to loop over parameter in such a way? I mean, if we want to compare model results with different parameters, can we just simply run the different codes separately? and then combine the IRF figures together with plot code by hand. I am just new to DYNARE and am now confused by how to combine figures. I don't know what's the advantage of doing loop over parameters. Will it just produce combined figures with different parameters automatically?
Many thanks indeed.
GOODLUCK
GOODLUCK
 
Posts: 16
Joined: Mon Dec 30, 2013 7:40 pm

Re: Loop over parameters

Postby jpfeifer » Tue Jan 07, 2014 9:25 am

No, it does not help you with plotting.
Looping over parameters is useful if you want to embedd Dynare in more complicated settings like using for a simulated method of moments or impulse response function matching approach.
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: Loop over parameters

Postby GOODLUCK » Wed Jan 08, 2014 5:25 am

Got it . It just has some programming edge.Many thanks for your explanation, jpfeifer.
GOODLUCK
 
Posts: 16
Joined: Mon Dec 30, 2013 7:40 pm

Next

Return to Dynare help

Who is online

Users browsing this forum: Google [Bot] and 11 guests