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

How to interface Dynare's csminwel1 in RISE? #155

Closed wangshaobobetter closed 3 years ago

wangshaobobetter 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 , #152 you mentioned. When I run this code, the program reports an error. The error content is as follows:

Error using generic/estimation_wrapper/fh_wrapper Too many output arguments.

Error in penalty_objective_function (line 34) [fval, info, exit_flag, arg1, arg2] = fcn(x, varargin{:});

Error in csminwel1 (line 94) [f0,cost_flag,arg1] = penalty_objective_function(x0,fcn,penalty,varargin{:});

Error in optimization.estimation_engine (line 194) [fi,xh,gh,H,itct,fcount,retcodeh] = optimizer(fh,x0,H0,grad,crit,nit,0,Verbose,Save_files,vargs{:});

Error in generic/estimation_wrapper (line 109) [x1,f1,H,issue]=optimization.estimationengine(PROBLEM,estim_blocks);

Error in generic/find_posterior_mode (line 89) [x1,f1,H,issue,viol,obj,funevals]=estimation_wrapper(obj,'estimate',x0,lb,ub,funevals);

Error in generic/estimate (line 348) [x1,f1,H,x0,f0,viol,funevals,issue,obj]=find_posterior_mode(obj,x0,lb_short,ub_short); %#ok

Error in dsge/estimate (line 47) obj=estimate@generic(obj,varargin{:});

Error in howto (line 26) sw_old=estimate(sw,'optimizer','csminwel1','optimset',myoptim); %'optimizer':'fmincon' 'bee_gate' 'fminunc'


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)


When I'm debugging this code,after debugging the penalty_objective_function,“[fval, info, exit_flag, arg1, arg2] = fcn(x, varargin{:});”,the "fcn function" points to the h_wrapper function,but I know the fh_wrapper function(function[minus_log_post,retcode,max_pen]=fh_wrapper(xtilde)) is called correctly in the estimation_wrappe , so the input and output parameters are obviously not corresponding。

What specific details should I pay attention to when interfaceing Dynare's csminwel1 to RISE?

Looking forward to your reply, thanks in advance.

jmaih commented 3 years ago

There is the original version of csminwel and the dynare adaptation. You need to write a wrapping function that RISE will call and the wrapping function in turn will call your version of csminwel.

Here is an example of a wrapping function to be used with the original csminwel.

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

On Wed, Dec 23, 2020 at 3:11 PM wangshaobobetter 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 https://github.com/jmaih/RISE_toolbox/issues/12 , #13 https://github.com/jmaih/RISE_toolbox/issues/13 , #21 https://github.com/jmaih/RISE_toolbox/issues/21 , #26 https://github.com/jmaih/RISE_toolbox/issues/26 , #132 https://github.com/jmaih/RISE_toolbox/issues/132 , #152 https://github.com/jmaih/RISE_toolbox/issues/152 you mentioned. When I run this code, the program reports an error. The error content is as follows:

Error using generic/estimation_wrapper/fh_wrapper Too many output arguments.

Error in penalty_objective_function (line 34) [fval, info, exit_flag, arg1, arg2] = fcn(x, varargin{:});

Error in csminwel1 (line 94) [f0,cost_flag,arg1] = penalty_objective_function(x0,fcn,penalty,varargin{:});

Error in optimization.estimation_engine (line 194) [fi,xh,gh,H,itct,fcount,retcodeh] = optimizer(fh,x0,H0,grad,crit,nit,0,Verbose,Save_files,vargs{:});

Error in generic/estimation_wrapper (line 109) [x1,f1,H,issue]=optimization.estimationengine(PROBLEM,estim_blocks);

Error in generic/find_posterior_mode (line 89)

[x1,f1,H,issue,viol,obj,funevals]=estimation_wrapper(obj,'estimate',x0,lb,ub,funevals);

Error in generic/estimate (line 348) [x1,f1,H,x0,f0,viol,funevals,issue,obj]=find_posterior_mode(obj,x0,lb_short,ub_short); %#ok

Error in dsge/estimate (line 47) obj=estimate@generic(obj,varargin{:});

Error in howto (line 26) sw_old=estimate(sw,'optimizer','csminwel1','optimset',myoptim); %'optimizer':'fmincon' 'bee_gate' 'fminunc'

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)

When I'm debugging this code,after debugging the penalty_objective_function,“[fval, info, exit_flag, arg1, arg2] = fcn(x, varargin{:});”,the "fcn function" points to the h_wrapper function,but I know the fh_wrapper function(function[minus_log_post,retcode,max_pen]=fh_wrapper(xtilde)) is called correctly in the estimation_wrappe , so the input and output parameters are obviously not corresponding。

What specific details should I pay attention to when interfaceing Dynare's csminwel1 to RISE?

Looking forward to your reply, thanks in advance.

— 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/155, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATKBT5ZICQBPWQIZU6CJ6LSWH3CPANCNFSM4VHBEP5Q .

wangshaobobetter commented 3 years ago

There is the original version of csminwel and the dynare adaptation. You need to write a wrapping function that RISE will call and the wrapping function in turn will call your version of csminwel. Here is an example of a wrapping function to be used with the original csminwel. 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 On Wed, Dec 23, 2020 at 3:11 PM wangshaobobetter @.> wrote: Hi Junior, Recently, I have been trying to interface Dynare's csminwel1 to RISE. I have read all the related issues #12 <#12> , #13 <#13> , #21 <#21> , #26 <#26> , #132 <#132> , #152 <#152> you mentioned. When I run this code, the program reports an error. The error content is as follows: Error using generic/estimation_wrapper/fh_wrapper Too many output arguments. Error in penalty_objective_function (line 34) [fval, info, exit_flag, arg1, arg2] = fcn(x, varargin{:}); Error in csminwel1 (line 94) [f0,cost_flag,arg1] = penalty_objective_function(x0,fcn,penalty,varargin{:}); Error in optimization.estimation_engine (line 194) [fi,xh,gh,H,itct,fcount,retcodeh] = optimizer(fh,x0,H0,grad,crit,nit,0,Verbose,Save_files,vargs{:}); Error in generic/estimation_wrapper (line 109) [x1,f1,H,issue]=optimization.estimationengine(PROBLEM,estim_blocks); Error in generic/find_posterior_mode (line 89) [x1,f1,H,issue,viol,obj,funevals]=estimation_wrapper(obj,'estimate',x0,lb,ub,funevals); Error in generic/estimate (line 348) [x1,f1,H,x0,f0,viol,funevals,issue,obj]=find_posterior_mode(obj,x0,lb_short,ub_short); %#ok Error in dsge/estimate (line 47) @.(obj,varargin{:}); Error in howto (line 26) sw_old=estimate(sw,'optimizer','csminwel1','optimset',myoptim); %'optimizer':'fmincon' 'bee_gate' 'fminunc' ------------------------------ 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) ------------------------------ When I'm debugging this code,after debugging the penalty_objective_function,“[fval, info, exit_flag, arg1, arg2] = fcn(x, varargin{:});”,the "fcn function" points to the h_wrapper function,but I know the fh_wrapper function(function[minus_log_post,retcode,max_pen]=fh_wrapper(xtilde)) is called correctly in the estimation_wrappe , so the input and output parameters are obviously not corresponding。 What specific details should I pay attention to when interfaceing Dynare's csminwel1 to RISE? Looking forward to your reply, thanks in advance. — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#155>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATKBT5ZICQBPWQIZU6CJ6LSWH3CPANCNFSM4VHBEP5Q .

Hi Junior, I modified the program code according to what you said, and the program run successfully, but the result is different from Dynare. Is there anything you should pay attention to during the initialization? This "epsilon" value, in particular, affected the results very much. The process was used as follows Dynare' result parameters prior mean post. mean 90% HPD interval prior pstdev

beta 0.500 0.0014 0.0011 0.0017 beta 0.2000 delta 0.500 0.5224 0.1983 0.8378 beta 0.2000

Rise' result beta BETA 0.5 0.00140901272969809 0+1598.62057453798i delta BETA 0.5 0.437014490153793 0+5.15433756949308i


Looking forward to your reply, thanks a lot.