Commit 0718a3eb authored by Johannes Pfeifer 's avatar Johannes Pfeifer

dynare_minimize_objective: fix use of analytic derivatives by employing wrapper function

parent ff427807
......@@ -77,15 +77,27 @@ switch minimizer_algorithm
% is not able to even move away from the initial point.
optim_options = optimoptions(optim_options, 'Algorithm','active-set');
end
if options_.analytic_derivation || options_.mom.analytic_jacobian==1
optim_options = optimoptions(optim_options,'GradObj','on','TolX',1e-7); %alter default TolX
end
if ~isempty(options_.optim_opt)
eval(['optim_options = optimoptions(optim_options,' options_.optim_opt ');']);
end
if options_.silent_optimizer
optim_options = optimoptions(optim_options,'display','off');
end
if options_.analytic_derivation
optim_options = optimoptions(optim_options,'GradObj','on','TolX',1e-7); %alter default TolX
if options_.analytic_derivation || options_.mom.analytic_jacobian==1 %use wrapper
func = @(x) analytic_gradient_wrapper(x,objective_function,varargin{:});
if ~isoctave
[opt_par_values,fval,exitflag,output,lamdba,grad,hessian_mat] = ...
fmincon(func,start_par_value,[],[],[],[],bounds(:,1),bounds(:,2),[],optim_options);
else
% Under Octave, use a wrapper, since fmincon() does not have an 11th
% arg. Also, only the first 4 output arguments are available.
[opt_par_values,fval,exitflag,output] = ...
fmincon(func,start_par_value,[],[],[],[],bounds(:,1),bounds(:,2),[],optim_options);
end
else
if ~isoctave
[opt_par_values,fval,exitflag,output,lamdba,grad,hessian_mat] = ...
fmincon(objective_function,start_par_value,[],[],[],[],bounds(:,1),bounds(:,2),[],optim_options,varargin{:});
......@@ -96,6 +108,7 @@ switch minimizer_algorithm
[opt_par_values,fval,exitflag,output] = ...
fmincon(func,start_par_value,[],[],[],[],bounds(:,1),bounds(:,2),[],optim_options);
end
end
case 2
%simulating annealing
......@@ -159,12 +172,20 @@ switch minimizer_algorithm
if ~isempty(options_.optim_opt)
eval(['optim_options = optimoptions(optim_options,' options_.optim_opt ');']);
end
if options_.analytic_derivation
optim_options = optimoptions(optim_options,'GradObj','on');
end
if options_.silent_optimizer
optim_options = optimoptions(optim_options,'display','off');
end
if options_.analytic_derivation || options_.mom.analytic_jacobian==1
optim_options = optimoptions(optim_options,'GradObj','on');
if ~isoctave
func = @(x) analytic_gradient_wrapper(x,objective_function,varargin{:});
[opt_par_values,fval,exitflag] = fminunc(func,start_par_value,optim_options);
else
% Under Octave, use a wrapper, since fminunc() does not have a 4th arg
func = @(x) analytic_gradient_wrapper(x,objective_function,varargin{:});
[opt_par_values,fval,exitflag] = fminunc(func,start_par_value,optim_options);
end
else
if ~isoctave
[opt_par_values,fval,exitflag] = fminunc(objective_function,start_par_value,optim_options,varargin{:});
else
......@@ -172,7 +193,7 @@ switch minimizer_algorithm
func = @(x) objective_function(x,varargin{:});
[opt_par_values,fval,exitflag] = fminunc(func,start_par_value,optim_options);
end
end
case 4
% Set default options.
H0 = 1e-4*eye(n_params);
......@@ -505,7 +526,12 @@ switch minimizer_algorithm
if options_.silent_optimizer
solveoptoptions.verbosity = 0;
end
if options_.analytic_derivation || options_.mom.analytic_jacobian==1
func = @(x) analytic_gradient_wrapper(x,objective_function,varargin{:});
[opt_par_values,fval]=solvopt(start_par_value,func,1,[],[],solveoptoptions);
else
[opt_par_values,fval]=solvopt(start_par_value,objective_function,[],[],[],solveoptoptions,varargin{:});
end
case 102
if isoctave
error('Optimization algorithm 2 is not available under Octave')
......@@ -534,8 +560,14 @@ switch minimizer_algorithm
eval(['optim_options = optimoptions(optim_options,' options_.optim_opt ');']);
end
if options_.silent_optimizer
optim_options = optimoptions(optim_options,'Display','off');
optim_options.Display='off';
end
if options_.analytic_derivation || options_.mom.analytic_jacobian==1
optim_options.SpecifyObjectiveGradient=true;
func = @(x) analytic_gradient_wrapper(x,objective_function,varargin{:});
[opt_par_values,Resnorm,fval,exitflag,OUTPUT,LAMBDA,JACOB] = ...
lsqnonlin(func,start_par_value,bounds(:,1),bounds(:,2),optim_options);
else
if ~isoctave
[opt_par_values,Resnorm,fval,exitflag,OUTPUT,LAMBDA,JACOB] = lsqnonlin(objective_function,start_par_value,bounds(:,1),bounds(:,2),optim_options,varargin{:});
else
......@@ -543,6 +575,7 @@ switch minimizer_algorithm
func = @(x)objective_function(x,varargin{:});
[opt_par_values,Resnorm,fval,exitflag,OUTPUT,LAMBDA,JACOB] = lsqnonlin(func,start_par_value,bounds(:,1),bounds(:,2),optim_options);
end
end
otherwise
if ischar(minimizer_algorithm)
if exist(minimizer_algorithm)
......
Markdown is supported
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