2015-10-13 12:11:28 +02:00
function oo_= execute_prior_posterior_function ( posterior_function_name,M_,options_,oo_,estim_params_,bayestopt_,dataset_,dataset_info,type)
%[oo_] = execute_prior_posterior_function(posterior_function_name,M_,options_,oo_,estim_params_,bayestopt_,dataset_,dataset_info,type)
% This function executes a given function on draws of the posterior or prior distribution
%
2015-04-04 19:49:21 +02:00
% INPUTS
% functionhandle Handle to the function to be executed
2015-10-13 12:11:28 +02:00
% M_ [structure] Matlab/Octave structure describing the Model (initialized by dynare, see @ref{M_}).
% options_ [structure] Matlab/Octave structure describing the options (initialized by dynare, see @ref{options_}).
% oo_ [structure] Matlab/Octave structure gathering the results (initialized by dynare, see @ref{oo_}).
% estim_params_[structure] Matlab/Octave structure describing the estimated_parameters (initialized by dynare, see @ref{estim_params_}).
% bayestopt_ [structure] Matlab/Octave structure describing the parameter options (initialized by dynare, see @ref{bayestopt_}).
% dataset_ [structure] Matlab/Octave structure storing the dataset
% dataset_info [structure] Matlab/Octave structure storing the information about the dataset
2015-04-04 19:49:21 +02:00
% type [string] 'prior' or 'posterior'
%
%
% OUTPUTS
2015-10-13 12:11:28 +02:00
% oo_ [structure] Matlab/Octave structure gathering the results (initialized by dynare, see @ref{oo_}).
2015-04-04 19:49:21 +02:00
2023-04-26 10:34:25 +02:00
% Copyright © 2013-2023 Dynare Team
2015-04-04 19:49:21 +02:00
%
% 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
2021-06-09 17:33:48 +02:00
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
2015-04-04 19:49:21 +02:00
[ directory , basename , extension ] = fileparts ( posterior_function_name ) ;
if isempty ( extension )
extension = ' .m' ;
end
fullname = [ basename extension ] ;
if ~ strcmp ( extension , ' .m' ) %if not m-file
error ( ' The Posterior Function is not an m-file.' )
elseif ~ exist ( fullname , ' file' ) %if m-file, but does not exist
error ( [ ' The Posterior Function ' , fullname , ' was not found. Check the spelling.' ] ) ;
end
%Create function handle
function handle= str2func ( posterior_function_name) ;
2015-10-13 17:40:15 +02:00
n_draws = options_ . sampling_draws ;
2023-04-26 10:34:25 +02:00
2015-04-04 19:49:21 +02:00
if strcmpi ( type , ' posterior' )
2023-04-26 10:34:25 +02:00
% Get informations about the _posterior_draws files.
% discard first mh_drop percent of the draws:
2023-09-13 18:09:38 +02:00
CutSample ( M_ , options_ , ' prior_posterior_function' ) ;
2023-04-26 10:34:25 +02:00
% initialize metropolis draws
options_ . sub_draws = n_draws ; % set draws for sampling; changed value is not returned to base workspace
[ error_flag , ~ , options_ ] = metropolis_draw ( 1 , options_ , estim_params_ , M_ ) ;
2015-04-04 19:49:21 +02:00
if error_flag
error ( ' EXECUTE_POSTERIOR_FUNCTION: The draws could not be initialized' )
end
2023-04-26 10:34:25 +02:00
n_draws = options_ . sub_draws ;
2015-04-04 19:49:21 +02:00
elseif strcmpi ( type , ' prior' )
2023-04-26 10:34:25 +02:00
% Get informations about the prior distribution.
2017-09-14 09:48:26 +02:00
if isempty ( bayestopt_ )
if ~ isempty ( estim_params_ ) && ~ ( isfield ( estim_params_ , ' nvx' ) && ( size ( estim_params_ . var_exo , 1 ) + size ( estim_params_ . var_endo , 1 ) + size ( estim_params_ . corrx , 1 ) + size ( estim_params_ . corrn , 1 ) + size ( estim_params_ . param_vals , 1 ) ) == 0 )
[ xparam1 , estim_params_ , bayestopt_ , lb , ub , M_ ] = set_prior ( estim_params_ , M_ , options_ ) ;
else
error ( ' The prior distributions are not properly set up.' )
end
2021-08-16 14:52:29 +02:00
end
if exist ( [ M_ . fname ' _prior_restrictions.m' ] )
2023-10-24 12:53:00 +02:00
warning ( ' prior_function currently does not support endogenous prior restrictions. They will be ignored. Consider using a posterior_function with nobs=1.' )
2017-09-14 09:48:26 +02:00
end
2023-04-26 10:34:25 +02:00
Prior = dprior ( bayestopt_ , options_ . prior_trunc ) ;
2015-04-04 19:49:21 +02:00
else
error ( ' EXECUTE_POSTERIOR_FUNCTION: Unknown type!' )
end
2023-04-26 10:34:25 +02:00
if strcmpi ( type , ' prior' )
parameter_mat = Prior . draws ( n_draws ) ;
else
parameter_mat = NaN ( length ( bayestopt_ . p6 ) , n_draws ) ;
for i = 1 : n_draws
parameter_mat ( : , i ) = GetOneDraw ( type , M_ , estim_params_ , oo_ , options_ , bayestopt_ ) ;
end
2015-04-04 19:49:21 +02:00
end
2023-04-26 10:34:25 +02:00
% Get output size
2015-04-04 19:49:21 +02:00
try
2023-04-26 10:34:25 +02:00
junk = functionhandle ( parameter_mat ( : , 1 ) , M_ , options_ , oo_ , estim_params_ , bayestopt_ , dataset_ , dataset_info ) ;
2015-04-04 19:49:21 +02:00
catch err
fprintf ( ' \nEXECUTE_POSTERIOR_FUNCTION: Execution of prior/posterior function led to an error. Execution cancelled.\n' )
rethrow ( err )
end
2023-04-26 10:34:25 +02:00
% Initialize cell with number of columns
results_cell = cell ( n_draws , columns ( junk ) ) ;
2015-04-04 19:49:21 +02:00
2023-04-26 10:34:25 +02:00
% Evaluate function on each draw
for i = 1 : n_draws
M_ = set_all_parameters ( parameter_mat ( : , i ) , estim_params_ , M_ ) ;
[ results_cell ( i , : ) ] = functionhandle ( parameter_mat ( : , i ) , M_ , options_ , oo_ , estim_params_ , bayestopt_ , dataset_ , dataset_info ) ;
2015-10-14 11:13:20 +02:00
end
2023-04-26 10:34:25 +02:00
% Save results under oo_
oo_ . ( sprintf ( ' %s_function_results' , type ) ) = results_cell ;