Call estimation in a Matlab loop

Hello community,

When looping over parameter values, stoch_simul can nicely be called in Matlab without running the whole mod-file again. Is there a similar way to do this with the estimation command? The example below shows what I have in mind. I am trying to see how certain estimates change if calibrated parameters change.

Best regards,
Tiljim

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_); %this is known to work.
        %THE FOLLOWING COMMAND DOES NOT WORK, BUT IS WHAT I AM LOOKING FOR:
        info = estimation(datafile=data_estimation) pi, y, i;
        if info;
          disp('Computation fails for rho = ' num2str(rho)]);
        end;
    end
end

Could you (privately) supply me with the files?

Thanks, @jpfeifer. So far it was a conceptual question, but I have setup m/mod-files which make the point. The mod-file runs both with stoch_simul and estimation on its own. I can also loop through different values of rho with the simulation but not the estimation. If I run the m-file as shown, Matlab understandably complains:

Error: File: Run_test.m Line: 12 Column: 35
Incorrect use of β€˜=’ operator. To assign a value to a variable, use β€˜=’. To compare values for equality, use β€˜==’.

Using β€˜==’ does obviously not solve the issue either. Is there another command I could call form Matlab?

Best regards,
Tiljim

test.mod (787 Bytes)
Run_test.m (540 Bytes)
data_estimation.mat (683 Bytes)

Use

dynare test noclearall;
options_.silent_optimizer=1;
rhos = 0.3:0.3:0.9;
for i=1:length(rhos)
    set_param_value('rho',rhos(i));
    try
        oo_recursive_=dynare_estimation(var_list_);
    catch
        disp(['Computation fails for rho = ' num2str(rho)]);
    end
end

test.mod (788 Bytes)

1 Like

Great, thanks a lot @jpfeifer. At first, I got the following error message:

You did not declare endogenous variables after the estimation/calib_smoother command.

I got around it by including the estimation command (which was commented out) in the mod-file. I assume that the mod-file first hast to run once properly with the estimation before I can call it again in loops with your suggested command. In this way the necessary structure is built. The same logic applies for stoch_simul as I figured out.

Thanks again. This saves me a big amount of computation time!

Edit: I just see that you did exactly this in your attached mod-file. Sorry and thanks. :slight_smile:

Indeed. But I had attached the modified mod-file above.

Just realised that as well - but my edit came to late. :sweat_smile:

Just for future reference: you can do this approach for pretty much every Dynare command. What you need to do is run the mod-file once and then check the driver.m file in the +folder (Dynare 4.6) for the relevant Matlab code of the command that is executed. You can then copy that command into the loop.

This is great to know. Thanks a lot!

Thanks to your explanation, @jpfeifer, I figured out that check; and info = stoch_simul(var_list_); have to be replaced by the following since Dynare 4.6:

oo_.dr.eigval = check(M_,options_,oo_);
[info, oo_, options_, M_] = stoch_simul(M_, options_, oo_, var_list_);

Both work but look obviously not as elegant as before - not sure, though, whether elegance is important here. More importantly, older codes will have to be adjusted for this.

Indeed, it’s not about elegance. You can see in the codes that the main structures are not global variables anymore but function arguments. In the context of loops, this allows running parfor.