function [f0, x, ig] = mr_gstep(h1,x,bounds,func0,penalty,htol0,Verbose,Save_files,gradient_epsilon,parameter_names,robust,varargin)
% [f0, x, ig] = mr_gstep(h1,x,bounds,func0,penalty,htol0,Verbose,Save_files,gradient_epsilon,parameter_names,robust,varargin)
%
% Gibbs type step in optimisation
%
% varargin{1} --> dataset_
% varargin{2} --> dataset_info
% varargin{3} --> options_
% varargin{4} --> M_
% varargin{5} --> estim_params_
% varargin{6} --> bayestopt_
% varargin{7} --> BoundsInfo
% varargin{8} --> oo_
% Copyright © 2006-2023 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see .
n=size(x,1);
if isempty(h1)
h1=gradient_epsilon*ones(n,1);
end
if isempty(htol0)
htol = 1.e-6;
else
htol = htol0;
end
if length(htol)==1
htol=htol*ones(n,1);
end
f0=penalty_objective_function(x,func0,penalty,varargin{:});
xh1=x;
f1=zeros(size(f0,1),n);
f_1=f1;
i=0;
ig=zeros(n,1);
while i htol(i)
[ff, xx,~,retcode] = csminit1(func0,x,penalty,f0,gg,0,diag(hh),Verbose,varargin{:});
if retcode && robust
if abs(x(i))<1.e-6
xa=transpose(linspace(x(i)/2, sign(x(i))*1.e-6*3/2, 7));
else
xa=transpose(linspace(x(i)/2, x(i)*3/2, 7));
end
fa=NaN(7,1);
for k=1:7
xh1(i)=xa(k);
fa(k,1) = penalty_objective_function(xh1,func0,penalty,varargin{:});
end
b=[ones(7,1) xa xa.*xa./2]\fa;
gg(i)=x(i)*b(3)+b(2);
hh(i)=1/b(3);
[ff2, xx2] = csminit1(func0,x,penalty,f0,gg,0,diag(hh),Verbose,varargin{:});
if ff2=bounds(:,2));
if ~isempty(inx)
x(inx) = bounds(inx,2)-1.e-10;
end
inx = find(x<=bounds(:,1));
if ~isempty(inx)
x(inx) = bounds(inx,1)+1.e-10;
end