2014-06-16 17:41:59 +02:00
|
|
|
function random_walk_metropolis_hastings(TargetFun,ProposalFun,xparam1,vv,mh_bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_)
|
2015-04-24 18:12:46 +02:00
|
|
|
% function random_walk_metropolis_hastings(TargetFun,ProposalFun,xparam1,vv,mh_bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_)
|
|
|
|
% Random Walk Metropolis-Hastings algorithm.
|
2008-03-11 21:55:16 +01:00
|
|
|
%
|
|
|
|
% INPUTS
|
|
|
|
% o TargetFun [char] string specifying the name of the objective
|
|
|
|
% function (posterior kernel).
|
2015-04-24 18:12:46 +02:00
|
|
|
% o ProposalFun [char] string specifying the name of the proposal
|
|
|
|
% density
|
2008-03-11 21:55:16 +01:00
|
|
|
% o xparam1 [double] (p*1) vector of parameters to be estimated (initial values).
|
|
|
|
% o vv [double] (p*p) matrix, posterior covariance matrix (at the mode).
|
|
|
|
% o mh_bounds [double] (p*2) matrix defining lower and upper bounds for the parameters.
|
2012-08-05 15:10:21 +02:00
|
|
|
% o dataset_ data structure
|
2015-04-24 18:12:46 +02:00
|
|
|
% o dataset_info dataset info structure
|
2012-08-05 15:10:21 +02:00
|
|
|
% o options_ options structure
|
|
|
|
% o M_ model structure
|
|
|
|
% o estim_params_ estimated parameters structure
|
|
|
|
% o bayestopt_ estimation options structure
|
|
|
|
% o oo_ outputs structure
|
2008-03-11 21:55:16 +01:00
|
|
|
%
|
|
|
|
% ALGORITHM
|
2015-04-24 18:12:46 +02:00
|
|
|
% Random-Walk Metropolis-Hastings.
|
2008-03-11 21:55:16 +01:00
|
|
|
%
|
|
|
|
% SPECIAL REQUIREMENTS
|
|
|
|
% None.
|
2011-02-04 17:27:33 +01:00
|
|
|
%
|
2010-05-31 11:55:25 +02:00
|
|
|
% PARALLEL CONTEXT
|
|
|
|
% The most computationally intensive part of this function may be executed
|
2015-04-24 18:12:46 +02:00
|
|
|
% in parallel. The code suitable to be executed in
|
|
|
|
% parallel on multi core or cluster machine (in general a 'for' cycle)
|
|
|
|
% has been removed from this function and been placed in the random_walk_metropolis_hastings_core.m funtion.
|
|
|
|
%
|
|
|
|
% The DYNARE parallel packages comprise a i) set of pairs of Matlab functions that can be executed in
|
|
|
|
% parallel and called name_function.m and name_function_core.m and ii) a second set of functions used
|
|
|
|
% to manage the parallel computations.
|
2010-05-31 11:55:25 +02:00
|
|
|
%
|
2015-04-24 18:12:46 +02:00
|
|
|
% This function was the first function to be parallelized. Later, other
|
2010-05-31 11:55:25 +02:00
|
|
|
% functions have been parallelized using the same methodology.
|
|
|
|
% Then the comments write here can be used for all the other pairs of
|
2015-04-24 18:12:46 +02:00
|
|
|
% parallel functions and also for management functions.
|
2010-05-31 11:55:25 +02:00
|
|
|
|
2015-04-24 18:12:46 +02:00
|
|
|
% Copyright (C) 2006-2015 Dynare Team
|
2008-08-01 14:40:33 +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
|
|
|
|
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-08-05 15:27:14 +02:00
|
|
|
|
2015-04-24 18:12:46 +02:00
|
|
|
% In Metropolis, we set penalty to Inf so as to reject all parameter sets triggering an error during target density computation
|
2012-08-28 12:17:07 +02:00
|
|
|
global objective_function_penalty_base
|
|
|
|
objective_function_penalty_base = Inf;
|
2012-08-05 15:27:14 +02:00
|
|
|
|
2013-11-20 18:03:12 +01:00
|
|
|
% Initialization of the random walk metropolis-hastings chains.
|
|
|
|
[ ix2, ilogpo2, ModelName, MetropolisFolder, fblck, fline, npar, nblck, nruns, NewFile, MAX_nruns, d ] = ...
|
2014-06-16 17:41:59 +02:00
|
|
|
metropolis_hastings_initialization(TargetFun, xparam1, vv, mh_bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_);
|
2009-05-15 18:36:51 +02:00
|
|
|
|
2008-03-30 11:31:06 +02:00
|
|
|
InitSizeArray = min([repmat(MAX_nruns,nblck,1) fline+nruns-1],[],2);
|
2009-05-15 18:36:51 +02:00
|
|
|
|
2013-11-20 18:03:12 +01:00
|
|
|
% Load last mh history file
|
|
|
|
load_last_mh_history_file(MetropolisFolder, ModelName);
|
2010-10-22 11:27:26 +02:00
|
|
|
|
|
|
|
% Only for test parallel results!!!
|
|
|
|
|
2010-12-17 09:22:12 +01:00
|
|
|
% To check the equivalence between parallel and serial computation!
|
2010-10-22 11:27:26 +02:00
|
|
|
% First run in serial mode, and then comment the follow line.
|
|
|
|
% save('recordSerial.mat','-struct', 'record');
|
|
|
|
|
2015-04-24 18:12:46 +02:00
|
|
|
% For parallel runs after serial runs with the abobe line active.
|
2010-10-22 11:27:26 +02:00
|
|
|
% TempRecord=load('recordSerial.mat');
|
|
|
|
% record.Seeds=TempRecord.Seeds;
|
|
|
|
|
|
|
|
|
2011-02-04 17:17:48 +01:00
|
|
|
|
2010-05-31 11:55:25 +02:00
|
|
|
% Snapshot of the current state of computing. It necessary for the parallel
|
2015-04-24 18:12:46 +02:00
|
|
|
% execution (i.e. to execute in a corretct way a portion of code remotely or
|
|
|
|
% on many cores). The mandatory variables for local/remote parallel
|
|
|
|
% computing are stored in the localVars struct.
|
2009-05-15 18:36:51 +02:00
|
|
|
|
|
|
|
localVars = struct('TargetFun', TargetFun, ...
|
2009-12-16 18:17:34 +01:00
|
|
|
'ProposalFun', ProposalFun, ...
|
|
|
|
'xparam1', xparam1, ...
|
|
|
|
'vv', vv, ...
|
|
|
|
'mh_bounds', mh_bounds, ...
|
|
|
|
'ix2', ix2, ...
|
|
|
|
'ilogpo2', ilogpo2, ...
|
|
|
|
'ModelName', ModelName, ...
|
|
|
|
'fline', fline, ...
|
|
|
|
'npar', npar, ...
|
|
|
|
'nruns', nruns, ...
|
|
|
|
'NewFile', NewFile, ...
|
|
|
|
'MAX_nruns', MAX_nruns, ...
|
2012-08-05 15:10:21 +02:00
|
|
|
'd', d, ...
|
|
|
|
'InitSizeArray',InitSizeArray, ...
|
|
|
|
'record', record, ...
|
|
|
|
'dataset_', dataset_, ...
|
2014-06-16 17:41:59 +02:00
|
|
|
'dataset_info', dataset_info, ...
|
2012-08-05 15:10:21 +02:00
|
|
|
'options_', options_, ...
|
|
|
|
'M_',M_, ...
|
|
|
|
'bayestopt_', bayestopt_, ...
|
|
|
|
'estim_params_', estim_params_, ...
|
|
|
|
'oo_', oo_,...
|
|
|
|
'varargin',[]);
|
2009-05-15 18:36:51 +02:00
|
|
|
|
|
|
|
|
2015-04-24 18:12:46 +02:00
|
|
|
% User doesn't want to use parallel computing, or wants to compute a
|
|
|
|
% single chain compute Random walk Metropolis-Hastings algorithm sequentially.
|
2009-05-15 18:36:51 +02:00
|
|
|
|
2010-02-10 18:52:16 +01:00
|
|
|
if isnumeric(options_.parallel) || (nblck-fblck)==0,
|
2009-05-15 18:36:51 +02:00
|
|
|
fout = random_walk_metropolis_hastings_core(localVars, fblck, nblck, 0);
|
|
|
|
record = fout.record;
|
2010-05-31 11:55:25 +02:00
|
|
|
|
2011-02-04 17:17:48 +01:00
|
|
|
% Parallel in Local or remote machine.
|
2010-05-31 11:55:25 +02:00
|
|
|
else
|
|
|
|
% Global variables for parallel routines.
|
2012-08-05 15:10:21 +02:00
|
|
|
globalVars = struct();
|
2009-05-15 18:36:51 +02:00
|
|
|
% which files have to be copied to run remotely
|
|
|
|
NamFileInput(1,:) = {'',[ModelName '_static.m']};
|
|
|
|
NamFileInput(2,:) = {'',[ModelName '_dynamic.m']};
|
|
|
|
if options_.steadystate_flag,
|
|
|
|
NamFileInput(length(NamFileInput)+1,:)={'',[ModelName '_steadystate.m']};
|
2008-03-11 21:55:16 +01:00
|
|
|
end
|
2011-02-10 15:54:23 +01:00
|
|
|
if (options_.load_mh_file~=0) && any(fline>1) ,
|
2009-05-27 12:26:45 +02:00
|
|
|
NamFileInput(length(NamFileInput)+1,:)={[M_.dname '/metropolis/'],[ModelName '_mh' int2str(NewFile(1)) '_blck*.mat']};
|
2008-06-24 20:20:48 +02:00
|
|
|
end
|
2010-10-18 14:39:48 +02:00
|
|
|
if exist([ModelName '_optimal_mh_scale_parameter.mat'])
|
|
|
|
NamFileInput(length(NamFileInput)+1,:)={'',[ModelName '_optimal_mh_scale_parameter.mat']};
|
|
|
|
end
|
2009-05-15 18:36:51 +02:00
|
|
|
% from where to get back results
|
2009-12-16 18:17:34 +01:00
|
|
|
% NamFileOutput(1,:) = {[M_.dname,'/metropolis/'],'*.*'};
|
2010-02-12 17:37:28 +01:00
|
|
|
[fout, nBlockPerCPU, totCPU] = masterParallel(options_.parallel, fblck, nblck,NamFileInput,'random_walk_metropolis_hastings_core', localVars, globalVars, options_.parallel_info);
|
2009-05-15 18:36:51 +02:00
|
|
|
for j=1:totCPU,
|
2009-12-16 18:17:34 +01:00
|
|
|
offset = sum(nBlockPerCPU(1:j-1))+fblck-1;
|
2013-03-17 22:49:28 +01:00
|
|
|
record.LastLogPost(offset+1:sum(nBlockPerCPU(1:j)))=fout(j).record.LastLogPost(offset+1:sum(nBlockPerCPU(1:j)));
|
2009-12-16 18:17:34 +01:00
|
|
|
record.LastParameters(offset+1:sum(nBlockPerCPU(1:j)),:)=fout(j).record.LastParameters(offset+1:sum(nBlockPerCPU(1:j)),:);
|
2013-11-27 11:34:49 +01:00
|
|
|
record.AcceptanceRatio(offset+1:sum(nBlockPerCPU(1:j)))=fout(j).record.AcceptanceRatio(offset+1:sum(nBlockPerCPU(1:j)));
|
2013-11-20 18:03:12 +01:00
|
|
|
record.LastSeeds(offset+1:sum(nBlockPerCPU(1:j)))=fout(j).record.LastSeeds(offset+1:sum(nBlockPerCPU(1:j)));
|
2008-06-24 20:20:48 +02:00
|
|
|
end
|
2009-05-15 18:36:51 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
irun = fout(1).irun;
|
|
|
|
NewFile = fout(1).NewFile;
|
|
|
|
|
2013-11-20 18:03:12 +01:00
|
|
|
update_last_mh_history_file(MetropolisFolder, ModelName, record);
|
2009-12-16 18:17:34 +01:00
|
|
|
|
2015-04-24 18:12:46 +02:00
|
|
|
% Provide diagnostic output
|
2013-07-10 17:12:34 +02:00
|
|
|
skipline()
|
2013-11-20 18:03:12 +01:00
|
|
|
disp(['Estimation::mcmc: Number of mh files: ' int2str(NewFile(1)) ' per block.'])
|
|
|
|
disp(['Estimation::mcmc: Total number of generated files: ' int2str(NewFile(1)*nblck) '.'])
|
|
|
|
disp(['Estimation::mcmc: Total number of iterations: ' int2str((NewFile(1)-1)*MAX_nruns+irun-1) '.'])
|
2013-11-27 11:56:38 +01:00
|
|
|
disp(['Estimation::mcmc: Current acceptance ratio per chain: '])
|
2013-11-20 18:03:12 +01:00
|
|
|
for i=1:nblck
|
|
|
|
if i<10
|
2013-11-27 11:34:49 +01:00
|
|
|
disp([' Chain ' num2str(i) ': ' num2str(100*record.AcceptanceRatio(i)) '%'])
|
2013-11-20 18:03:12 +01:00
|
|
|
else
|
2013-11-27 11:34:49 +01:00
|
|
|
disp([' Chain ' num2str(i) ': ' num2str(100*record.AcceptanceRatio(i)) '%'])
|
2013-11-20 18:03:12 +01:00
|
|
|
end
|
|
|
|
end
|