2013-03-17 22:49:28 +01:00
|
|
|
function record=independent_metropolis_hastings(TargetFun,ProposalFun,xparam1,vv,mh_bounds,varargin)
|
2009-09-18 16:40:08 +02:00
|
|
|
|
|
|
|
% Independent Metropolis-Hastings algorithm.
|
2008-08-28 15:38:07 +02:00
|
|
|
%
|
|
|
|
% INPUTS
|
|
|
|
% o TargetFun [char] string specifying the name of the objective
|
|
|
|
% function (posterior kernel).
|
|
|
|
% 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.
|
|
|
|
% o varargin list of argument following mh_bounds
|
|
|
|
%
|
|
|
|
% OUTPUTS
|
2013-03-17 22:49:28 +01:00
|
|
|
% o record [struct] structure describing the iterations
|
2008-08-28 15:38:07 +02:00
|
|
|
%
|
2009-09-18 16:40:08 +02:00
|
|
|
% ALGORITHM
|
|
|
|
% Metropolis-Hastings.
|
2008-08-28 15:38:07 +02:00
|
|
|
%
|
|
|
|
% SPECIAL REQUIREMENTS
|
|
|
|
% None.
|
2011-02-04 17:27:33 +01:00
|
|
|
%
|
2010-05-31 11:44:00 +02:00
|
|
|
% PARALLEL CONTEXT
|
|
|
|
% See the comment in random_walk_metropolis_hastings.m funtion.
|
|
|
|
|
2013-06-12 16:42:09 +02:00
|
|
|
% Copyright (C) 2006-2013 Dynare Team
|
2008-09-02 17:18:05 +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/>.
|
|
|
|
|
2009-09-18 16:40:08 +02:00
|
|
|
global M_ options_ bayestopt_ estim_params_ oo_
|
2008-08-28 15:38:07 +02:00
|
|
|
%%%%
|
|
|
|
%%%% Initialization of the independent metropolis-hastings chains.
|
|
|
|
%%%%
|
|
|
|
[ ix2, ilogpo2, ModelName, MhDirectoryName, fblck, fline, npar, nblck, nruns, NewFile, MAX_nruns, d ] = ...
|
2009-09-18 16:40:08 +02:00
|
|
|
metropolis_hastings_initialization(TargetFun, xparam1, vv, mh_bounds, varargin{:});
|
|
|
|
|
|
|
|
xparam1 = transpose(xparam1);
|
|
|
|
InitSizeArray = min([repmat(MAX_nruns,nblck,1) fline+nruns-1],[],2);
|
|
|
|
|
|
|
|
load([MhDirectoryName '/' ModelName '_mh_history.mat'],'record');
|
|
|
|
|
2010-05-31 11:44:00 +02:00
|
|
|
%The mandatory variables for local/remote parallel computing are stored in localVars struct.
|
2009-09-18 16:40:08 +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, ...
|
|
|
|
'd', d);
|
2009-09-18 16:40:08 +02:00
|
|
|
localVars.InitSizeArray=InitSizeArray;
|
|
|
|
localVars.record=record;
|
|
|
|
localVars.varargin=varargin;
|
|
|
|
|
2010-05-31 11:44:00 +02:00
|
|
|
% Like a sequential execution!
|
2010-01-15 11:02:22 +01:00
|
|
|
if isnumeric(options_.parallel),
|
2009-09-18 16:40:08 +02:00
|
|
|
fout = independent_metropolis_hastings_core(localVars, fblck, nblck, 0);
|
|
|
|
record = fout.record;
|
2011-02-04 17:17:48 +01:00
|
|
|
% Parallel execution.
|
2009-09-18 16:40:08 +02:00
|
|
|
else
|
|
|
|
% global variables for parallel routines
|
|
|
|
globalVars = struct('M_',M_, ...
|
2009-12-16 18:17:34 +01:00
|
|
|
'options_', options_, ...
|
|
|
|
'bayestopt_', bayestopt_, ...
|
|
|
|
'estim_params_', estim_params_, ...
|
|
|
|
'oo_', oo_);
|
2009-09-18 16:40:08 +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']};
|
|
|
|
end
|
2011-02-10 17:23:22 +01:00
|
|
|
if (options_.load_mh_file~=0) && any(fline>1) ,
|
2009-09-18 16:40:08 +02:00
|
|
|
NamFileInput(length(NamFileInput)+1,:)={[M_.dname '/metropolis/'],[ModelName '_mh' int2str(NewFile(1)) '_blck*.mat']};
|
2008-08-28 15:38:07 +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-12-16 18:17:34 +01:00
|
|
|
|
2009-09-18 16:40:08 +02:00
|
|
|
% from where to get back results
|
2009-12-16 18:17:34 +01:00
|
|
|
% NamFileOutput(1,:) = {[M_.dname,'/metropolis/'],'*.*'};
|
2009-09-18 16:40:08 +02:00
|
|
|
|
2010-02-12 17:24:24 +01:00
|
|
|
[fout, nBlockPerCPU, totCPU] = masterParallel(options_.parallel, fblck, nblck,NamFileInput,'independent_metropolis_hastings_core', localVars, globalVars, options_.parallel_info);
|
2009-09-18 16:40:08 +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)),:);
|
|
|
|
record.AcceptationRates(offset+1:sum(nBlockPerCPU(1:j)))=fout(j).record.AcceptationRates(offset+1:sum(nBlockPerCPU(1:j)));
|
|
|
|
record.Seeds(offset+1:sum(nBlockPerCPU(1:j)))=fout(j).record.Seeds(offset+1:sum(nBlockPerCPU(1:j)));
|
2009-09-18 16:40:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
irun = fout(1).irun;
|
|
|
|
NewFile = fout(1).NewFile;
|
|
|
|
|
|
|
|
% record.Seeds.Normal = randn('state');
|
|
|
|
% record.Seeds.Unifor = rand('state');
|
|
|
|
save([MhDirectoryName '/' ModelName '_mh_history.mat'],'record');
|
2010-01-05 11:46:10 +01:00
|
|
|
disp(['MH: Number of mh files : ' int2str(NewFile(1)) ' per block.'])
|
|
|
|
disp(['MH: Total number of generated files : ' int2str(NewFile(1)*nblck) '.'])
|
|
|
|
disp(['MH: Total number of iterations : ' int2str((NewFile(1)-1)*MAX_nruns+irun-1) '.'])
|
2009-09-18 16:40:08 +02:00
|
|
|
disp('MH: average acceptation rate per chain : ')
|
|
|
|
disp(record.AcceptationRates);
|
2013-07-10 17:12:34 +02:00
|
|
|
skipline()
|