Commit 5c3b0ebc authored by Marco Ratto's avatar Marco Ratto
Browse files

better handle newrat options, avoiding to treat newratflag in two places

parent 915502ce
......@@ -195,38 +195,13 @@ end
%% Estimation of the posterior mode or likelihood mode
if ~isequal(options_.mode_compute,0) && ~options_.mh_posterior_mode_estimation
%prepare settings for newrat
if options_.mode_compute==5
%get whether outer product Hessian is requested
newratflag=[];
if ~isempty(options_.optim_opt)
options_list = read_key_value_string(options_.optim_opt);
for i=1:rows(options_list)
if strcmp(options_list{i,1},'Hessian')
newratflag=options_list{i,2};
end
end
end
if options_.analytic_derivation
options_analytic_derivation_old = options_.analytic_derivation;
options_.analytic_derivation = -1;
if ~isempty(newratflag) && newratflag~=0 %numerical hessian explicitly specified
error('newrat: analytic_derivation is incompatible with numerical Hessian.')
else %use default
newratflag=0; %exclude DYNARE numerical hessian
end
elseif ~options_.analytic_derivation
if isempty(newratflag)
newratflag=options_.newrat.hess; %use default numerical dynare hessian
end
end
end
[xparam1, fval, exitflag, hh, options_, Scale, new_rat_hess_info] = dynare_minimize_objective(objective_function,xparam1,options_.mode_compute,options_,[bounds.lb bounds.ub],bayestopt_.name,bayestopt_,hh,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,bounds,oo_);
fprintf('\nFinal value of minus the log posterior (or likelihood):%f \n', fval);
if isnumeric(options_.mode_compute) && options_.mode_compute==5 && options_.analytic_derivation==-1 %reset options changed by newrat
options_.analytic_derivation = options_analytic_derivation_old; %reset
if isnumeric(options_.mode_compute) && options_.mode_compute==5
newratflag = new_rat_hess_info.newratflag;
new_rat_hess_info = new_rat_hess_info.new_rat_hess_info;
elseif isnumeric(options_.mode_compute) && options_.mode_compute==6 %save scaling factor
save([M_.fname '_optimal_mh_scale_parameter.mat'],'Scale');
options_.mh_jscale = Scale;
......@@ -240,8 +215,8 @@ if ~isequal(options_.mode_compute,0) && ~options_.mh_posterior_mode_estimation
[~,~,~,~,hh] = feval(objective_function,xparam1, ...
dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,bounds,oo_);
options_.analytic_derivation = ana_deriv_old;
elseif ~isnumeric(options_.mode_compute) || ~(isequal(options_.mode_compute,5) && newratflag==1 && strcmp(func2str(objective_function),'dsge_likelihood'))
% with flag==0, we force to use the hessian from outer product gradient of optimizer 5
elseif ~isnumeric(options_.mode_compute) || ~(isequal(options_.mode_compute,5) && newratflag~=1 && strcmp(func2str(objective_function),'dsge_likelihood'))
% with flag==0 or 2, we force to use the hessian from outer product gradient of optimizer 5
if options_.hessian.use_penalized_objective
penalized_objective_function = str2func('penalty_objective_function');
hh = hessian(penalized_objective_function, xparam1, options_.gstep, objective_function, fval, dataset_, dataset_info, options_, M_, estim_params_, bayestopt_, bounds,oo_);
......@@ -260,7 +235,7 @@ if ~isequal(options_.mode_compute,0) && ~options_.mh_posterior_mode_estimation
% with diagonal elements computed with numerical second order derivatives
%
% uses univariate filters, so to get max # of available
% densitities for outer product gradient
% densities for outer product gradient
kalman_algo0 = options_.kalman_algo;
compute_hessian = 1;
if ~((options_.kalman_algo == 2) || (options_.kalman_algo == 4))
......
......@@ -274,7 +274,9 @@ switch minimizer_algorithm
if isempty(prior_information) %mr_hessian requires it, but can be NaN
prior_information.p2=NaN(n_params,1);
end
if options_.analytic_derivation==-1 %set outside as code for use of analytic derivation
if options_.analytic_derivation
old_analytic_derivation = options_.analytic_derivation;
options_.analytic_derivation=-1; %force analytic outer product gradient hessian for each iteration
analytic_grad=1;
crit = options_.newrat.tolerance.f_analytic;
newratflag = 0; %analytical Hessian
......@@ -317,7 +319,14 @@ switch minimizer_algorithm
hess_info.gstep=options_.gstep;
hess_info.htol = 1.e-4;
hess_info.h1=options_.gradient_epsilon*ones(n_params,1);
% here we force 7th input argument (flagg) to be 0, since outer product
% gradient Hessian is handled in dynare_estimation_1
[opt_par_values,hessian_mat,gg,fval,invhess,new_rat_hess_info] = newrat(objective_function,start_par_value,bounds,analytic_grad,crit,nit,0,Verbose,Save_files,hess_info,prior_information.p2,options_.gradient_epsilon,parameter_names,varargin{:}); %hessian_mat is the plain outer product gradient Hessian
new_rat_hess_info.new_rat_hess_info = new_rat_hess_info;
new_rat_hess_info.newratflag = newratflag;
if options_.analytic_derivation
options_.analytic_derivation = old_analytic_derivation;
end
case 6
if isempty(prior_information) %Inf will be reset
prior_information.p2=Inf(n_params,1);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment