Skip to content
Snippets Groups Projects
Commit 2476ffdb authored by Johannes Pfeifer's avatar Johannes Pfeifer
Browse files

Add indicator for successful run of MCMC and provide error message if...

Add indicator for successful run of MCMC and provide error message if load_mh_file is used with crashed chain

Closes #917
parent 7bdc6de4
No related branches found
No related tags found
1 merge request!1025Add indicator for successful run of MCMC
function info = load_last_mh_history_file(MetropolisFolder, ModelName) function info = load_last_mh_history_file(MetropolisFolder, ModelName)
% function info = load_last_mh_history_file(MetropolisFolder, ModelName)
% Loads the last mh_history_file
% Inputs:
% MetropolisFolder [char] Name of the metropolis subfolder
% ModelName [char] Name of the mod-file
% Outputs:
% info [struct] structure storing the MH history
%
% Notes: The record structure is written to the caller workspace via an
% assignin statement.
% Copyright (C) 2013 Dynare Team % Copyright (C) 2013 Dynare Team
% %
...@@ -27,7 +37,7 @@ mh_history_files = dir([BaseName '_mh_history_*.mat']); ...@@ -27,7 +37,7 @@ mh_history_files = dir([BaseName '_mh_history_*.mat']);
% Consistency with older versions of Dynare. % Consistency with older versions of Dynare.
if isequal(length(mh_history_files),0) if isequal(length(mh_history_files),0)
if exist([BaseName '_mh_history.mat']) if exist([BaseName '_mh_history.mat'])
format_mh_history_file = 1; format_mh_history_file = 1; % old Dynare format
else else
error(['Estimation::load_mh_file: I cannot find any mh-history file in ' MetropolisFolder '!']) error(['Estimation::load_mh_file: I cannot find any mh-history file in ' MetropolisFolder '!'])
end end
...@@ -35,13 +45,14 @@ else ...@@ -35,13 +45,14 @@ else
format_mh_history_file = 0; format_mh_history_file = 0;
end end
if format_mh_history_file if format_mh_history_file %needed to preserve backward compatibility
load([BaseName '_mh_history.mat']); load([BaseName '_mh_history.mat']);
record.LastLogPost = record.LastLogLiK; record.LastLogPost = record.LastLogLiK;
record.InitialLogPost = record.InitialLogLiK; record.InitialLogPost = record.InitialLogLiK;
record.LastSeeds = record.Seeds; record.LastSeeds = record.Seeds;
record.AcceptanceRatio = record.AcceptationRates; record.AcceptanceRatio = record.AcceptationRates;
record.InitialSeeds = NaN; % This information is forever lost... record.InitialSeeds = NaN; % This information is forever lost...
record.MCMCConcludedSuccessfully = NaN; % This information is forever lost...
record = rmfield(record,'LastLogLiK'); record = rmfield(record,'LastLogLiK');
record = rmfield(record,'InitialLogLiK'); record = rmfield(record,'InitialLogLiK');
record = rmfield(record,'Seeds'); record = rmfield(record,'Seeds');
...@@ -49,6 +60,10 @@ if format_mh_history_file ...@@ -49,6 +60,10 @@ if format_mh_history_file
save([BaseName '_mh_history_0.mat'],'record'); save([BaseName '_mh_history_0.mat'],'record');
else else
load([BaseName '_mh_history_' num2str(length(mh_history_files)-1) '.mat']); load([BaseName '_mh_history_' num2str(length(mh_history_files)-1) '.mat']);
% add fields that have later been introduced
if ~isfield(record,'MCMCConcludedSuccessfully')
record.MCMCConcludedSuccessfully = NaN; % This information is forever lost...
end
end end
if isequal(nargout,0) if isequal(nargout,0)
......
...@@ -210,6 +210,7 @@ if ~options_.load_mh_file && ~options_.mh_recover ...@@ -210,6 +210,7 @@ if ~options_.load_mh_file && ~options_.mh_recover
record.LastLogPost = zeros(nblck,1); record.LastLogPost = zeros(nblck,1);
record.LastFileNumber = AnticipatedNumberOfFiles ; record.LastFileNumber = AnticipatedNumberOfFiles ;
record.LastLineNumber = AnticipatedNumberOfLinesInTheLastFile; record.LastLineNumber = AnticipatedNumberOfLinesInTheLastFile;
record.MCMCConcludedSuccessfully = 0;
fprintf('Ok!\n'); fprintf('Ok!\n');
id = write_mh_history_file(MetropolisFolder, ModelName, record); id = write_mh_history_file(MetropolisFolder, ModelName, record);
disp(['Estimation::mcmc: Details about the MCMC are available in ' BaseName '_mh_history_' num2str(id) '.mat']) disp(['Estimation::mcmc: Details about the MCMC are available in ' BaseName '_mh_history_' num2str(id) '.mat'])
...@@ -234,6 +235,10 @@ elseif options_.load_mh_file && ~options_.mh_recover ...@@ -234,6 +235,10 @@ elseif options_.load_mh_file && ~options_.mh_recover
% Here we consider previous mh files (previous mh did not crash). % Here we consider previous mh files (previous mh did not crash).
disp('Estimation::mcmc: I am loading past Metropolis-Hastings simulations...') disp('Estimation::mcmc: I am loading past Metropolis-Hastings simulations...')
load_last_mh_history_file(MetropolisFolder, ModelName); load_last_mh_history_file(MetropolisFolder, ModelName);
if ~isnan(record.MCMCConcludedSuccessfully) && ~record.MCMCConcludedSuccessfully
error('Estimation::mcmc: You are trying to load an MCMC that did not finish successfully. Please use mh_recover.')
end
record.MCMCConcludedSuccessfully=0; %reset indicator for this run
mh_files = dir([ MetropolisFolder filesep ModelName '_mh*.mat']); mh_files = dir([ MetropolisFolder filesep ModelName '_mh*.mat']);
if ~length(mh_files) if ~length(mh_files)
error('Estimation::mcmc: I cannot find any MH file to load here!') error('Estimation::mcmc: I cannot find any MH file to load here!')
......
...@@ -163,6 +163,8 @@ end ...@@ -163,6 +163,8 @@ end
irun = fout(1).irun; irun = fout(1).irun;
NewFile = fout(1).NewFile; NewFile = fout(1).NewFile;
record.MCMCConcludedSuccessfully = 1; %set indicator for successful run
update_last_mh_history_file(MetropolisFolder, ModelName, record); update_last_mh_history_file(MetropolisFolder, ModelName, record);
% Provide diagnostic output % Provide diagnostic output
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment