Skip to content

Commit

Permalink
Add functionality for executing function on posterior
Browse files Browse the repository at this point in the history
See #332
  • Loading branch information
JohannesPfeifer authored and Johannes Pfeifer committed May 29, 2013
1 parent 7bbe121 commit 99c863c
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
28 changes: 28 additions & 0 deletions matlab/dynare_estimation_1.m
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,34 @@ function dynare_estimation_1(var_list_,dname)
if options_.smoother || ~isempty(options_.filter_step_ahead) || options_.forecast
prior_posterior_statistics('posterior',dataset_);
end
if ~isempty(options_.posterior_function)
if ~options_.moments_varendo
fprintf('\nExecution of Posterior Functions requires the moments_varendo option. I skip the computation.\n')
else
posterior_function_error=0;
[directory,basename,extension] = fileparts(options_.posterior_function);
if isempty(extension)
extension = '.m';
end
fullname = [basename extension];
if ~strcmp(extension,'.m') %if not m-file
fprintf('\nThe Posterior Function is not an m-file. I skip the computation.\n')
posterior_function_error=1;
elseif ~exist(fullname,'file') %if m-file, but does not exist
fprintf(['\nThe Posterior Function ', fullname ,' was not found. I skip the computation. Check the spelling.\n']);
posterior_function_error=1;
end
if ~posterior_function_error
try
oo_.Posterior_function=execute_posterior_function(str2func(options_.posterior_function),M_,options_,oo_,dataset_,estim_params_,bayestopt_,'posterior');
catch err
fprintf('\nPosterior Function lead to an error. Execution cancelled.\n')
disp(err.message)
fprintf('\n')
end
end
end
end
xparam = get_posterior_parameters('mean');
M_ = set_all_parameters(xparam,estim_params_,M_);
end
Expand Down
77 changes: 77 additions & 0 deletions matlab/execute_posterior_function.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
function [results_cell] = execute_posterior_function(functionhandle,M_,options_,oo_,dataset_,estim_params_,bayestopt_,type)
%[results_cell] = execute_posterior_function(functionhandle,M_,options_,oo_,dataset_,estim_params_,bayestopt_,type)% This function executes a given function on draws of the posterior or prior distribution
%
% INPUTS
% functionhandle Handle to the function to be executed
% M_ [structure] Matlab's structure describing the Model (initialized by dynare, see @ref{M_}).
% options_ [structure] Matlab's structure describing the options (initialized by dynare, see @ref{options_}).
% oo_ [structure] Matlab's structure gathering the results (initialized by dynare, see @ref{oo_}).
% dataset_ [structure] Matlab's structure storing the dataset
% estim_params_[structure] Matlab's structure describing the estimated_parameters (initialized by dynare, see @ref{estim_params_}).
% bayestopt_ [structure] Matlab's structure describing the parameter options (initialized by dynare, see @ref{bayestopt_}).
% type [string] 'prior' or 'posterior'
%
%
% OUTPUTS
% results_cell [cell] ndrawsx1 cell array storing the results
% of the prior/posterior computations

% Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.

% Get informations about the _posterior_draws files.
if strcmpi(type,'posterior')
DrawsFiles = dir([M_.dname '/metropolis/' M_.fname '_' type '_draws*' ]);
posterior = 1;
elseif strcmpi(type,'prior')
DrawsFiles = dir([M_.dname '/prior/draws/' type '_draws*' ]);
CheckPath('prior/moments',M_.dname);
posterior = 0;
else
disp('Execute_posterior_function:: Unknown type!')
error('');
end
NumberOfDrawsFiles = length(DrawsFiles);

%% initialize output structure
if posterior
load([M_.dname '/metropolis/' DrawsFiles(1).name ],'pdraws');
else
load([M_.dname '/prior/draws/' DrawsFiles(1).name ],'pdraws');
end
xparam1=pdraws{1,1};
% get output size
junk=functionhandle(xparam1,M_,options_,oo_,dataset_,estim_params_,bayestopt_);
%initialize cell with number of columns
results_cell=cell(options_.PosteriorSampleSize,size(junk,2));

%% compute function on draws
iter = 1;
for file = 1:NumberOfDrawsFiles
if posterior
load([M_.dname '/metropolis/' DrawsFiles(file).name ],'pdraws');
else
load([M_.dname '/prior/draws/' DrawsFiles(file).name ],'pdraws');
end
NumberOfDraws = rows(pdraws);
for linee = 1:NumberOfDraws
M_ = set_all_parameters(pdraws{linee,1},estim_params_,M_);
[results_cell(iter,:)]=functionhandle(pdraws{linee,1},M_,options_,oo_,dataset_,estim_params_,bayestopt_);
iter=iter+1;
end
end

1 change: 1 addition & 0 deletions matlab/global_initialization.m
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ function global_initialization()
options_.MaxNumberOfBytes = 1e6;
options_.MaximumNumberOfMegaBytes = 111;
options_.PosteriorSampleSize = 1000;
options_.posterior_function = '';
options_.analytic_derivation = 0;
options_.analytic_derivation_mode = 0;
options_.bayesian_irf = 0;
Expand Down

0 comments on commit 99c863c

Please sign in to comment.