Code to loop over different dynare optimisers

Hi
I thought perhaps some of us would find the following code useful. It will loop over different types of numerical optimisers in sequence - the output of one optimiser will serve as the input of the next - to get a better estimate of the posterior mode. In the sequence of optimisers, I use mode_compute=6, the MCMC-based optimiser, as the final one, because it usually returns a nicely behaved hessian matrix.

The final estimated mode file named ‘finalmode.mat’ can be used as input for the MCMC step. The code can be tweaked depending on the user’s preferences to add more options for each optimiser-type.

cheers
Reuben

****************************************************************
%====================
% Find posterior mode
%====================


optimtypevals=[4 7 1 6]; % Declare types of Dynare optimisers used
 
   
for jj=1:length(optimtypevals)
        stropt=optimtypevals(jj);
        if optimtypevals(jj)==4,     disp('RUNNING OPTIMISER-TYPE: Sims');
        elseif optimtypevals(jj)==2, disp('RUNNING OPTIMISER-TYPE: Simulated annealing'); 
        elseif optimtypevals(jj)==1, disp('RUNNING OPTIMISER-TYPE: Matlab fmincon');           
        elseif optimtypevals(jj)==5, disp('RUNNING OPTIMISER-TYPE: Marco Ratto optimiser');  
        elseif optimtypevals(jj)==7, disp('RUNNING OPTIMISER-TYPE: fminsearch');  
        elseif optimtypevals(jj)==6, disp('RUNNING OPTIMISER-TYPE: Dynare Monte Carlo');  
        elseif optimtypevals(jj)==8, disp('RUNNING OPTIMISER-TYPE: Nelson-Mead Simplex');
        end
        options_.console_mode=1;
        options_.nograph=1; % Suppress graphs
        options_.silent_optimizer=1;% Suppress display of optimisation
        options_.cova_compute=0;% Do not compute std errors of parameter estimates
        options_.plot_priors=0;% Suppress prior plots
        options_.mh_replic= 0;% No MCMC
        options_.noprint=1;
        options_.lik_init=1;
        if jj==1
            eval(['options_.mode_compute=' num2str(stropt) ';']) ;
            estimation(datafile= ju2018demeaned,optim=('MaxIter',500)); 
            movefile([M_.fname '_mode.mat'], 'newmode.mat'); % rename mode file
            
        elseif jj>1 && jj<length(optimtypevals) 
            eval(['options_.mode_compute=' num2str(stropt) ';']) ;
            estimation(mode_file=newmode,datafile= ju2018demeaned,optim=('MaxIter',5000));  
            movefile([M_.fname '_mode.mat'], 'newmode.mat'); % rename mode file
        
        elseif jj==length(optimtypevals)
            eval(['options_.mode_compute=' num2str(stropt) ';']) ;
            
            options_.gmhmaxlik.target=0.2;
            options_.gmhmaxlik.iterations = 1; % [default=3] to call repeatedly the new optimization routine. Improves the estimates of the posterior covariance matrix and of the posterior mode.
            options_.gmhmaxlik.number = 100000; %[default=20000] to set the number of simulations used in estimation of the covariance matrix.
            options_.gmhmaxlik.nscale = 200000; % [default=200000] to set the number of simulations used in the tuning of the scale parameter.
            options_.gmhmaxlik.nclimb = 200000; % [default=200000] to set the number of simulations used in the hill climbing simulations.

            estimation(mode_file=newmode,datafile= ju2018demeaned, cova_compute=1);  
            movefile([M_.fname '_mode.mat'], 'finalmode.mat'); % rename mode file
        
        end
end
4 Likes

I have been using that one. It seems that the new Dynare 6 has a built-in option for that. You can specify a vector “additional_optimizer_steps”. Great!

1 Like