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

Closes #917
time-shift
Johannes Pfeifer 2015-08-11 14:37:37 +02:00
parent 7bdc6de481
commit 2476ffdbb9
3 changed files with 25 additions and 3 deletions

View File

@ -1,5 +1,15 @@
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
%
% This file is part of Dynare.
@ -27,7 +37,7 @@ mh_history_files = dir([BaseName '_mh_history_*.mat']);
% Consistency with older versions of Dynare.
if isequal(length(mh_history_files),0)
if exist([BaseName '_mh_history.mat'])
format_mh_history_file = 1;
format_mh_history_file = 1; % old Dynare format
else
error(['Estimation::load_mh_file: I cannot find any mh-history file in ' MetropolisFolder '!'])
end
@ -35,13 +45,14 @@ else
format_mh_history_file = 0;
end
if format_mh_history_file
if format_mh_history_file %needed to preserve backward compatibility
load([BaseName '_mh_history.mat']);
record.LastLogPost = record.LastLogLiK;
record.InitialLogPost = record.InitialLogLiK;
record.LastSeeds = record.Seeds;
record.AcceptanceRatio = record.AcceptationRates;
record.InitialSeeds = NaN; % This information is forever lost...
record.MCMCConcludedSuccessfully = NaN; % This information is forever lost...
record = rmfield(record,'LastLogLiK');
record = rmfield(record,'InitialLogLiK');
record = rmfield(record,'Seeds');
@ -49,6 +60,10 @@ if format_mh_history_file
save([BaseName '_mh_history_0.mat'],'record');
else
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
if isequal(nargout,0)

View File

@ -210,6 +210,7 @@ if ~options_.load_mh_file && ~options_.mh_recover
record.LastLogPost = zeros(nblck,1);
record.LastFileNumber = AnticipatedNumberOfFiles ;
record.LastLineNumber = AnticipatedNumberOfLinesInTheLastFile;
record.MCMCConcludedSuccessfully = 0;
fprintf('Ok!\n');
id = write_mh_history_file(MetropolisFolder, ModelName, record);
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
% Here we consider previous mh files (previous mh did not crash).
disp('Estimation::mcmc: I am loading past Metropolis-Hastings simulations...')
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']);
if ~length(mh_files)
error('Estimation::mcmc: I cannot find any MH file to load here!')

View File

@ -163,6 +163,8 @@ end
irun = fout(1).irun;
NewFile = fout(1).NewFile;
record.MCMCConcludedSuccessfully = 1; %set indicator for successful run
update_last_mh_history_file(MetropolisFolder, ModelName, record);
% Provide diagnostic output