jmaih / RISE_toolbox

Solution and estimation of Markov Switching Rational Expectations / DSGE Models
BSD 3-Clause "New" or "Revised" License
106 stars 78 forks source link

Failed to interface Dynare's opitimizers to RISE #152

Closed Kairey closed 3 years ago

Kairey commented 3 years ago

Hi Junior, Recently, I have been trying to interface Dynare's csminwel1 to RISE. I have read all the related issues #12, #13, #21, #26, #132 you mentioned. Then I write optimization function as you said at #132, but I failed in corresponding output parameters to Dynare's csminwel1 function, it seems that Dynare's own opitimizers have no some output parameters that wrapping rountines need, such as "exitflag". I arbitrary assigned value to exitflag, the code can run, but has no results. So I guess the parameter is very important. As described, exitflag is a flag similar to the ones provided by matlab's optimization functions. What exactly does this sentence mean? What specific details should I pay attention when interfaceing Dynare's optimizitions to RISE? For example, csminwel1.

A user-defined optimization function you referred: [xfinal,ffinal,exitflag,H]=optimizer(fh,x0,lb,ub,options,varargin);

Dynare's csminwel1 optimization function in Dynare's Matlab optimization toolbox: function [fh,xh,gh,H,itct,fcount,retcodeh] = csminwel1(fcn,x0,H0,grad,crit,nit,method,epsilon,Verbose,Save_files,varargin)

Looking forward to your reply, thanks in advance.

Best,

Kairey

jmaih commented 3 years ago

Hi Karey,

I attach a example

On Thu, Dec 17, 2020 at 4:42 AM Kairey notifications@github.com wrote:

Hi Junior, Recently, I have been trying to interface Dynare's csminwel1 to RISE. I have read all the related issues #12 http://url,#13 http://url,#21 http://url,#26 http://url,#132 http://url you mentioned. Then I write optimization function as you said at issue #132 http://url, but I failed in corresponding output parameters to Dynare's csminwel1 function, it seems that Dynare's own opitimizers have no some output parameters that wrapping rountines need, such as "exitflag". I arbitrary assigned value to exitflag, the code can run, but has no results. So I guess the parameter is very important. As described, exitflag is a flag similar to the ones provided by matlab's optimization functions. What exactly does this sentence mean? What specific details should I pay attention when interfaceing Dynare's optimizitions to RISE? For example, csminwel1.

A user-defined optimization function you referred: [xfinal,ffinal,exitflag,H]=optimizer(fh,x0,lb,ub,options,varargin);

Dynare's csminwel1 optimization function in Dynare's Matlab optimization toolbox: function [fh,xh,gh,H,itct,fcount,retcodeh] = csminwel1(fcn,x0,H0,grad,crit,nit,method,epsilon,Verbose,Save_files,varargin)

Looking forward to your reply, thanks in advance.

Best,

Kairey

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jmaih/RISE_toolbox/issues/152, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATKBT7S2QDAJPIHSQJOZPLSVF4ZDANCNFSM4U66QLGQ .

function [x1,f1,exitflag]=csminwelwrap(fh_handle,x0,lb,ub,options,varargin) % translate the inputs from RISE into inputs for csminwel %-------------------------------------------------------- nx=numel(x0); H0 = 1e-4*eye(nx); crit = 1e-7; nit = options.MaxIter; % using Matlab's MaxIter, which RISE will send analytic_grad=[];

[fh,xh,gh,H,itct,funcCount,retcodeh] = csminwel(... @myobjective,x0,H0,analytic_grad,crit,nit,varargin{:});

% translate the outputs from csminwel into outputs for RISE %---------------------------------------------------------- exitflag=1; x1=xh; f1=fh;

function fval=myobjective(x)
    % RISE expect your optimizer to correctly handle parameter bounds
    % restrictions. If your optimizer does not, you have to do
    % something about it yourself as I do in this subfunction.

    if any(x<lb)||any(x>ub)

        fval=1e+12;

    else

        fval=fh_handle(x,varargin{:});

    end

end

end