I am adding a version that should run. The core parts are a call to on optimizer with bound provided:
target_name='J';
x_start=[rho_TR del_pi del_y]';
x_opt_name={'rho_TR',0,1
'del_pi',1,Inf
'del_y',0,Inf
};
if length(x_start)~=length(x_opt_name)
error('Name vector does not match')
end
stoch_simul(order=2,periods=0) J;
%make sure Dynare does not print out stuff during runs
options_.nomoments=0;
options_.nofunctions=1;
options_.nograph=1;
options_.verbosity=0;
%set noprint option to suppress error messages within optimizer
options_.noprint=1;
options_.TeX=0;
@#ifndef CMAES
@#define CMAES=0
@#endif
@#if CMAES==0
% set csminwel options
H0 = 1e-2*eye(length(x_start)); %Initial Hessian
crit = 1e-8; %Tolerance
nit = 1000; %Number of iterations
[fhat,x_opt_hat] = csminwel(@get_minus_welfare_objective,x_start,H0,[],crit,nit,x_opt_name,target_name);
@#else
%set CMAES options
H0=0.2*ones(size(x_start,1),1)
cmaesOptions = options_.cmaes;
cmaesOptions.LBounds = cell2mat(x_opt_name(:,2));
cmaesOptions.UBounds = cell2mat(x_opt_name(:,3));
[x_opt_hat, fhat, COUNTEVAL, STOPFLAG, OUT, BESTEVER] = cmaes('get_minus_welfare_objective',x_start,H0,cmaesOptions,x_opt_name,target_name);
x_opt_hat=BESTEVER.x;
@#endif
fprintf('Optimal parameter values are:\n')
for ii=1:length(x_opt_name)
set_param_value(x_opt_name{ii},x_opt_hat(ii));
fprintf('%-20s = %8.4f;\n',x_opt_name{ii},x_opt_hat(ii))
end
and the objective function that returns minus the unconditional welfare measure (if you want conditional welfare, this needs to be adjusted)
function fval=get_minus_welfare_objective(x_opt,x_opt_name,target_name)
% function outvalue=get_minus_welfare_objective(x_opt)
% computes minus the welfare objective given the parameters
% Inputs:
% - x_opt [double] vector of parameters over which to optimize
% - x_opt_name [cell] cell array with name in first column, lower and
% upper bounds in second and third
% - target_name [string] name of the variable storing the welfare
% Copyright (C) 2018 Johannes Pfeifer
%
% This 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.
%
% It 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.
%
% For a copy of the GNU General Public License,
% see <http://www.gnu.org/licenses/>.
global oo_ options_ M_
for ii=1:size(x_opt_name,1)
set_param_value(x_opt_name{ii,1},x_opt(ii));
end
if any(x_opt<=cell2mat(x_opt_name(:,2))) || any(x_opt>=cell2mat(x_opt_name(:,3))) %make sure parameters are within bounds
fval=10e5+sum([x_opt].^2); %penalty function
return
end
var_list_ = target_name;
info=stoch_simul(var_list_); %run stoch_simul to generate IRFs with the options specified in the mod-file
if info %solution was not successful
fval=10e6+sum([x_opt].^2); %return with penalty
else
fval=-oo_.mean;
end
osr_Branch_McGough.mod (6.0 KB)
get_minus_welfare_objective.m (1.6 KB)