Accessors for posterior distributions of objects, needed by the MATLAB GUI

time-shift
Sébastien Villemot 2020-03-10 18:49:59 +01:00
parent 4cc946842b
commit 05c7925620
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
3 changed files with 257 additions and 0 deletions

View File

@ -0,0 +1,74 @@
function r = get_posterior_covariance(endo1, endo2)
% Copyright (C) 2020 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/>.
global M_ options_
id1 = find(strcmp(endo1, M_.endo_names));
if nargin < 2
id2 = id1;
else
id2 = find(strcmp(endo2, M_.endo_names));
end
ivar = get_variables_list(options_,M_);
if ~ismember(id1, ivar)
error('Posterior moments have not been computed for %s', endo1)
end
if ~ismember(id2, ivar)
error('Posterior moments have not been computed for %s', endo2)
end
r = struct('draw', [], 'moment', []);
% Fetch parameter draws
NumberDrawsFiles = length(dir([M_.dname '/metropolis/' M_.fname '_posterior_draws*' ]));
if NumberDrawsFiles == 0
error('Can''t find posterior draws file(s)')
end
idx = 1;
for file = 1:NumberDrawsFiles
load([M_.dname '/metropolis/' M_.fname '_posterior_draws' int2str(file) ],'pdraws');
for i = 1:size(pdraws, 1)
r(idx).draw = pdraws{i, 1};
idx = idx+1;
end
end
% Fetch moments
symidx = symmetric_matrix_index(find(ivar==id1), find(ivar==id2), length(ivar));
NumberMomentsFiles = length(dir([M_.dname '/metropolis/' M_.fname '_Posterior2ndOrderMoments*']));
if NumberMomentsFiles == 0
error('Can''t find posterior 2nd order moments file(s)')
end
idx = 1;
for file = 1:NumberMomentsFiles
load([M_.dname '/metropolis/' M_.fname '_Posterior2ndOrderMoments' int2str(file) ],'Covariance_matrix');
for i = 1:size(Covariance_matrix, 1)
r(idx).moment = Covariance_matrix(i, symidx);
idx = idx+1;
end
end

View File

@ -0,0 +1,73 @@
function r = get_posterior_irf(endo, exo)
% Copyright (C) 2020 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/>.
global M_ options_
endo_id = find(strcmp(endo, M_.endo_names));
exo_id = find(strcmp(exo, M_.exo_names));
if isempty(exo_id)
error('Unknown exogenous: %s', exo)
end
ivar = get_variables_list(options_,M_);
if ~ismember(endo_id, ivar)
error('Posterior IRFs have not been computed for %s', endo)
end
r = struct('draw', [], 'irf', []);
% Fetch parameter draws
NumberDrawsFiles = length(dir([M_.dname '/metropolis/' M_.fname '_param_irf*' ]));
if NumberDrawsFiles == 0
error('Can''t find posterior draws file(s)')
end
idx = 1;
for file = 1:NumberDrawsFiles
load([M_.dname '/metropolis/' M_.fname '_param_irf' int2str(file) ],'stock');
for i = 1:size(stock, 1)
r(idx).draw = stock(i, :);
idx = idx+1;
end
end
% Fetch IRFs
% We use the raw files, i.e. those that are *not* the output of
% ReshapeMatFiles.m. Their filename contains 'irf_dsge' in *lowercase*.
NumberIRFsFiles = length(dir([M_.dname '/metropolis/' M_.fname '_irf_dsge*']));
if NumberIRFsFiles == 0
error('Can''t find posterior IRFs file(s)')
end
endo_id_varlist = find(ivar == endo_id);
idx = 1;
for file = 1:NumberIRFsFiles
load([M_.dname '/metropolis/' M_.fname '_irf_dsge' int2str(file) ],'stock_irf_dsge');
for i = 1:size(stock_irf_dsge, 4)
r(idx).irf = stock_irf_dsge(:, endo_id_varlist, exo_id, i);
idx = idx+1;
end
end

View File

@ -0,0 +1,110 @@
function r = get_posterior_timeseries(type, endo)
% Returns the posterior distribution of either
% smoothed/updated/filtered/forecast timeseries for a given endogenous variable.
% "type" must be either 'smoothed', 'updated', 'filtered' or 'forecast'.
%
% For filtered variables, returns a matrix with step-ahead in lines and periods
% in columns.
% For forecasts, returns the "point" forecast (i.e. with uncertainty about both
% parameters and shocks).
% Copyright (C) 2020 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/>.
global M_
endo_id = find(strcmp(endo, M_.endo_names));
if isempty(endo_id)
error('Unknown endogenous variable %s', endo)
end
r = struct('draw', [], type, []);
% Fetch parameter draws
DrawsFiles = dir([M_.dname '/metropolis/' M_.fname '_param*' ]);
NumberDrawsFiles = 0;
for i=1:length(DrawsFiles)
% We need to filter out the _param_irf* files
if ~isempty(regexp(DrawsFiles(i).name, '.*_param[0-9]+\.mat'))
NumberDrawsFiles = NumberDrawsFiles + 1;
end
end
if NumberDrawsFiles == 0
error('Can''t find posterior draws file(s)')
end
idx = 1;
for file = 1:NumberDrawsFiles
load([M_.dname '/metropolis/' M_.fname '_param' int2str(file) ],'stock');
for i = 1:size(stock, 1)
r(idx).draw = stock(i, :);
idx = idx+1;
end
end
% Fetch timeseries
switch type
case 'smoothed'
basename = 'smooth';
case 'updated'
basename = 'update';
case 'filtered'
basename = 'filter_step_ahead';
case 'forecast'
basename = 'forc_point';
otherwise
error('Unknown type requested. Should be one of: smoothed, updated, filtered, forecast')
end
if strcmp(type, 'smoothed')
SmoothedFiles = dir([M_.dname '/metropolis/' M_.fname '_smooth*']);
NumberTimeseriesFiles = 0;
for i=1:length(SmoothedFiles)
%% We need to filter out the _smoothed_{constant,trend}* files
if ~isempty(regexp(SmoothedFiles(i).name, '.*_smooth[0-9]+\.mat'))
NumberTimeseriesFiles = NumberTimeseriesFiles + 1;
end
end
else
NumberTimeseriesFiles = length(dir([M_.dname '/metropolis/' M_.fname '_' ...
basename '*']));
end
if NumberTimeseriesFiles == 0
error('Can''t find file(s) with posterior timeseries of requested type')
end
idx = 1;
for file = 1:NumberTimeseriesFiles
load([M_.dname '/metropolis/' M_.fname '_' basename int2str(file) ],'stock');
if strcmp(type, 'filtered')
for i = 1:size(stock, 4)
r(idx).(type) = reshape(stock(:, endo_id, :, i), size(stock,1), size(stock,3));
idx = idx+1;
end
else
for i = 1:size(stock, 3)
r(idx).(type) = stock(endo_id, :, i)';
idx = idx+1;
end
end
end