2009-12-16 18:17:34 +01:00
|
|
|
function myoutput = independent_metropolis_hastings_core(myinputs,fblck,nblck,whoiam, ThisMatlab)
|
2010-05-31 11:45:04 +02:00
|
|
|
% PARALLEL CONTEXT
|
|
|
|
% The most computationally intensive portion of code in
|
|
|
|
% independent_metropolis_hastings (the 'for xxx = fblck:nblck' cycle).
|
|
|
|
% See the comment in random_walk_metropolis_hastings_core.m funtion.
|
2011-02-02 14:13:11 +01:00
|
|
|
%
|
|
|
|
% INPUTS
|
2010-05-31 11:45:04 +02:00
|
|
|
% See See the comment in random_walk_metropolis_hastings_core.m funtion.
|
2009-12-16 18:17:34 +01:00
|
|
|
|
2010-05-31 11:45:04 +02:00
|
|
|
% OUTPUTS
|
|
|
|
% See See the comment in random_walk_metropolis_hastings_core.m funtion.
|
|
|
|
%
|
2011-02-02 14:13:11 +01:00
|
|
|
% ALGORITHM
|
|
|
|
% Portion of Independing Metropolis-Hastings.
|
2010-05-31 11:45:04 +02:00
|
|
|
%
|
|
|
|
% SPECIAL REQUIREMENTS.
|
|
|
|
% None.
|
|
|
|
%
|
2011-02-04 17:27:33 +01:00
|
|
|
% Copyright (C) 2006-2011 Dynare Team
|
2009-12-16 18:17:34 +01: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/>.
|
|
|
|
|
|
|
|
if nargin<4,
|
|
|
|
whoiam=0;
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-08-28 12:17:07 +02:00
|
|
|
global bayestopt_ estim_params_ options_ M_ oo_ objective_function_penalty_base
|
2009-12-16 18:17:34 +01:00
|
|
|
|
2010-05-31 11:45:04 +02:00
|
|
|
% Reshape 'myinputs' for local computation.
|
|
|
|
% In order to avoid confusion in the name space, the instruction struct2local(myinputs) is replaced by:
|
|
|
|
|
|
|
|
TargetFun=myinputs.TargetFun;
|
|
|
|
ProposalFun=myinputs.ProposalFun;
|
|
|
|
xparam1=myinputs.xparam1;
|
|
|
|
vv=myinputs.vv;
|
|
|
|
mh_bounds=myinputs.mh_bounds;
|
|
|
|
ix2=myinputs.ix2;
|
|
|
|
ilogpo2=myinputs.ilogpo2;
|
|
|
|
ModelName=myinputs.ModelName;
|
|
|
|
fline=myinputs.fline;
|
|
|
|
npar=myinputs.npar;
|
|
|
|
nruns=myinputs.nruns;
|
|
|
|
NewFile=myinputs.NewFile;
|
|
|
|
MAX_nruns=myinputs.MAX_nruns;
|
|
|
|
d=myinputs.d;
|
2010-10-22 11:27:26 +02:00
|
|
|
InitSizeArray=myinputs.InitSizeArray;
|
2010-05-31 11:45:04 +02:00
|
|
|
record=myinputs.record;
|
|
|
|
varargin=myinputs.varargin;
|
|
|
|
|
|
|
|
if whoiam
|
|
|
|
Parallel=myinputs.Parallel;
|
2010-08-25 13:39:48 +02:00
|
|
|
% initialize persistent variables in priordens()
|
2010-10-27 11:24:23 +02:00
|
|
|
priordens(xparam1',bayestopt_.pshape,bayestopt_.p6,bayestopt_.p7, ...
|
2011-02-04 17:17:48 +01:00
|
|
|
bayestopt_.p3,bayestopt_.p4,1);
|
2010-05-31 11:45:04 +02:00
|
|
|
end
|
2009-12-16 18:17:34 +01:00
|
|
|
|
2010-05-31 11:45:04 +02:00
|
|
|
% (re)Set the penalty.
|
2012-08-28 12:17:07 +02:00
|
|
|
objective_function_penalty_base = Inf;
|
2009-12-16 18:17:34 +01:00
|
|
|
|
2011-12-15 11:56:23 +01:00
|
|
|
MhDirectoryName = CheckPath('metropolis',M_.dname);
|
2009-12-16 18:17:34 +01:00
|
|
|
|
|
|
|
OpenOldFile = ones(nblck,1);
|
|
|
|
if strcmpi(ProposalFun,'rand_multivariate_normal')
|
|
|
|
n = npar;
|
|
|
|
ProposalDensity = 'multivariate_normal_pdf';
|
|
|
|
elseif strcmpi(ProposalFun,'rand_multivariate_student')
|
|
|
|
n = options_.student_degrees_of_freedom;
|
|
|
|
ProposalDensity = 'multivariate_student_pdf';
|
|
|
|
end
|
|
|
|
% load([MhDirectoryName '/' ModelName '_mh_history.mat'],'record');
|
|
|
|
%%%%
|
|
|
|
%%%% NOW i run the (nblck-fblck+1) metropolis-hastings chains
|
|
|
|
%%%%
|
2010-09-15 09:07:52 +02:00
|
|
|
|
2010-10-18 14:39:48 +02:00
|
|
|
if any(isnan(bayestopt_.jscale))
|
|
|
|
if exist([ModelName '_optimal_mh_scale_parameter.mat'])% This file is created by mode_compute=6.
|
|
|
|
load([ModelName '_optimal_mh_scale_parameter'])
|
|
|
|
proposal_covariance = d*Scale;
|
|
|
|
else
|
|
|
|
error('mh:: Something is wrong. I can''t figure out the value of the scale parameter.')
|
|
|
|
end
|
|
|
|
else
|
|
|
|
proposal_covariance = d*diag(bayestopt_.jscale);
|
|
|
|
end
|
2009-12-16 18:17:34 +01:00
|
|
|
|
|
|
|
jloop=0;
|
|
|
|
|
|
|
|
for b = fblck:nblck,
|
|
|
|
jloop=jloop+1;
|
2012-08-30 12:24:05 +02:00
|
|
|
try
|
|
|
|
% this will not work if the master uses a random generator not
|
|
|
|
% available in the slave (different Matlab version or
|
|
|
|
% Matlab/Octave cluster). Therefor the trap.
|
2012-08-30 12:44:46 +02:00
|
|
|
|
|
|
|
% this set the random generator type (the seed is useless but
|
|
|
|
% needed by the function)
|
|
|
|
set_dynare_seed(options_.DynareRandomStreams.algo,...
|
|
|
|
options_.DynareRandomStreams.seed);
|
|
|
|
% this set the state
|
2012-08-30 12:24:05 +02:00
|
|
|
set_dynare_random_generator_state(record.Seeds(b).Unifor, ...
|
|
|
|
record.Seeds(b).Normal);
|
|
|
|
catch
|
2012-08-30 12:44:46 +02:00
|
|
|
% if the state set by master is incompatible with the slave, we
|
|
|
|
% only reseed
|
2012-08-30 12:24:05 +02:00
|
|
|
set_dynare_seed(options_.DynareRandomStreams.seed+b);
|
|
|
|
end
|
2011-02-10 17:23:22 +01:00
|
|
|
if (options_.load_mh_file~=0) && (fline(b)>1) && OpenOldFile(b)
|
2009-12-16 18:17:34 +01:00
|
|
|
load(['./' MhDirectoryName '/' ModelName '_mh' int2str(NewFile(b)) ...
|
2011-02-04 17:17:48 +01:00
|
|
|
'_blck' int2str(b) '.mat'])
|
2009-12-16 18:17:34 +01:00
|
|
|
x2 = [x2;zeros(InitSizeArray(b)-fline(b)+1,npar)];
|
|
|
|
logpo2 = [logpo2;zeros(InitSizeArray(b)-fline(b)+1,1)];
|
|
|
|
OpenOldFile(b) = 0;
|
|
|
|
else
|
|
|
|
x2 = zeros(InitSizeArray(b),npar);
|
|
|
|
logpo2 = zeros(InitSizeArray(b),1);
|
|
|
|
end
|
2010-06-25 14:58:53 +02:00
|
|
|
if exist('OCTAVE_VERSION') || options_.console_mode
|
|
|
|
diary off
|
|
|
|
disp(' ')
|
2009-12-16 18:17:34 +01:00
|
|
|
elseif whoiam
|
|
|
|
% keyboard;
|
|
|
|
waitbarString = ['Please wait... Metropolis-Hastings (' int2str(b) '/' int2str(options_.mh_nblck) ')...'];
|
2010-10-22 11:27:26 +02:00
|
|
|
% waitbarTitle=['Metropolis-Hastings ',options_.parallel(ThisMatlab).ComputerName];
|
2009-12-16 18:17:34 +01:00
|
|
|
if options_.parallel(ThisMatlab).Local,
|
|
|
|
waitbarTitle=['Local '];
|
|
|
|
else
|
2010-10-22 11:27:26 +02:00
|
|
|
waitbarTitle=[options_.parallel(ThisMatlab).ComputerName];
|
2011-02-02 14:13:11 +01:00
|
|
|
end
|
2010-10-22 11:27:26 +02:00
|
|
|
fMessageStatus(0,whoiam,waitbarString, waitbarTitle, options_.parallel(ThisMatlab));
|
2009-12-16 18:17:34 +01:00
|
|
|
else,
|
|
|
|
hh = waitbar(0,['Please wait... Metropolis-Hastings (' int2str(b) '/' int2str(options_.mh_nblck) ')...']);
|
|
|
|
set(hh,'Name','Metropolis-Hastings');
|
2011-02-02 14:13:11 +01:00
|
|
|
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
|
|
|
isux = 0;
|
|
|
|
jsux = 0;
|
|
|
|
irun = fline(b);
|
|
|
|
j = 1;
|
|
|
|
while j <= nruns(b)
|
2011-02-02 14:13:11 +01:00
|
|
|
par = feval(ProposalFun, xparam1, proposal_covariance, n);
|
2011-02-10 17:23:22 +01:00
|
|
|
if all( par(:) > mh_bounds(:,1) ) && all( par(:) < mh_bounds(:,2) )
|
2009-12-16 18:17:34 +01:00
|
|
|
try
|
2011-02-02 14:13:11 +01:00
|
|
|
logpost = - feval(TargetFun, par(:),varargin{:});
|
2009-12-16 18:17:34 +01:00
|
|
|
catch,
|
|
|
|
logpost = -inf;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
logpost = -inf;
|
|
|
|
end
|
|
|
|
r = logpost - ilogpo2(b) + ...
|
2010-10-22 11:27:26 +02:00
|
|
|
log(feval(ProposalDensity, ix2(b,:), xparam1, proposal_covariance, n)) - ...
|
|
|
|
log(feval(ProposalDensity, par, xparam1, proposal_covariance, n));
|
2009-12-16 18:17:34 +01:00
|
|
|
if (logpost > -inf) && (log(rand) < r)
|
|
|
|
x2(irun,:) = par;
|
|
|
|
ix2(b,:) = par;
|
2011-02-02 14:13:11 +01:00
|
|
|
logpo2(irun) = logpost;
|
2009-12-16 18:17:34 +01:00
|
|
|
ilogpo2(b) = logpost;
|
|
|
|
isux = isux + 1;
|
|
|
|
jsux = jsux + 1;
|
2011-02-02 14:13:11 +01:00
|
|
|
else
|
2009-12-16 18:17:34 +01:00
|
|
|
x2(irun,:) = ix2(b,:);
|
|
|
|
logpo2(irun) = ilogpo2(b);
|
|
|
|
end
|
|
|
|
prtfrc = j/nruns(b);
|
2010-06-25 14:58:53 +02:00
|
|
|
if exist('OCTAVE_VERSION') || options_.console_mode
|
|
|
|
if mod(j, 10) == 0
|
|
|
|
if exist('OCTAVE_VERSION')
|
2011-02-02 14:13:11 +01:00
|
|
|
if (whoiam==0),
|
|
|
|
printf('MH: Computing Metropolis-Hastings (chain %d/%d): %3.f%% done, acception rate: %3.f%%\r', b, nblck, 100 * prtfrc, 100 * isux / j);
|
|
|
|
end
|
2010-06-25 14:58:53 +02:00
|
|
|
else
|
|
|
|
fprintf(' MH: Computing Metropolis-Hastings (chain %d/%d): %3.f \b%% done, acception rate: %3.f \b%%\r', b, nblck, 100 * prtfrc, 100 * isux / j);
|
|
|
|
end
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
2011-02-10 17:23:22 +01:00
|
|
|
if mod(j,50)==0 && whoiam,
|
2009-12-16 18:17:34 +01:00
|
|
|
% keyboard;
|
|
|
|
waitbarString = [ '(' int2str(b) '/' int2str(options_.mh_nblck) '), ' sprintf('accept. %3.f%%%%', 100 * isux/j)];
|
2010-10-22 11:27:26 +02:00
|
|
|
fMessageStatus(prtfrc,whoiam,waitbarString, '', options_.parallel(ThisMatlab))
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
|
|
|
else
|
2011-02-10 17:23:22 +01:00
|
|
|
if mod(j, 3)==0 && ~whoiam
|
2009-12-16 18:17:34 +01:00
|
|
|
waitbar(prtfrc,hh,[ '(' int2str(b) '/' int2str(options_.mh_nblck) ') ' sprintf('%f done, acceptation rate %f',prtfrc,isux/j)]);
|
2011-02-10 17:23:22 +01:00
|
|
|
elseif mod(j,50)==0 && whoiam,
|
2009-12-16 18:17:34 +01:00
|
|
|
% keyboard;
|
|
|
|
waitbarString = [ '(' int2str(b) '/' int2str(options_.mh_nblck) ') ' sprintf('%f done, acceptation rate %f',prtfrc,isux/j)];
|
2010-10-22 11:27:26 +02:00
|
|
|
fMessageStatus(prtfrc,whoiam,waitbarString, waitbarTitle, options_.parallel(ThisMatlab))
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
|
|
|
end
|
2011-02-02 14:13:11 +01:00
|
|
|
|
2011-02-10 17:23:22 +01:00
|
|
|
if (irun == InitSizeArray(b)) || (j == nruns(b)) % Now I save the simulations
|
2009-12-16 18:17:34 +01:00
|
|
|
save([MhDirectoryName '/' ModelName '_mh' int2str(NewFile(b)) '_blck' int2str(b) '.mat'],'x2','logpo2');
|
|
|
|
fidlog = fopen([MhDirectoryName '/metropolis.log'],'a');
|
|
|
|
fprintf(fidlog,['\n']);
|
|
|
|
fprintf(fidlog,['%% Mh' int2str(NewFile(b)) 'Blck' int2str(b) ' (' datestr(now,0) ')\n']);
|
|
|
|
fprintf(fidlog,' \n');
|
|
|
|
fprintf(fidlog,[' Number of simulations.: ' int2str(length(logpo2)) '\n']);
|
|
|
|
fprintf(fidlog,[' Acceptation rate......: ' num2str(jsux/length(logpo2)) '\n']);
|
|
|
|
fprintf(fidlog,[' Posterior mean........:\n']);
|
|
|
|
for i=1:length(x2(1,:))
|
|
|
|
fprintf(fidlog,[' params:' int2str(i) ': ' num2str(mean(x2(:,i))) '\n']);
|
|
|
|
end
|
|
|
|
fprintf(fidlog,[' log2po:' num2str(mean(logpo2)) '\n']);
|
|
|
|
fprintf(fidlog,[' Minimum value.........:\n']);;
|
|
|
|
for i=1:length(x2(1,:))
|
|
|
|
fprintf(fidlog,[' params:' int2str(i) ': ' num2str(min(x2(:,i))) '\n']);
|
|
|
|
end
|
|
|
|
fprintf(fidlog,[' log2po:' num2str(min(logpo2)) '\n']);
|
|
|
|
fprintf(fidlog,[' Maximum value.........:\n']);
|
|
|
|
for i=1:length(x2(1,:))
|
|
|
|
fprintf(fidlog,[' params:' int2str(i) ': ' num2str(max(x2(:,i))) '\n']);
|
|
|
|
end
|
|
|
|
fprintf(fidlog,[' log2po:' num2str(max(logpo2)) '\n']);
|
|
|
|
fprintf(fidlog,' \n');
|
|
|
|
fclose(fidlog);
|
|
|
|
jsux = 0;
|
|
|
|
if j == nruns(b) % I record the last draw...
|
|
|
|
record.LastParameters(b,:) = x2(end,:);
|
|
|
|
record.LastLogLiK(b) = logpo2(end);
|
|
|
|
end
|
|
|
|
% size of next file in chain b
|
|
|
|
InitSizeArray(b) = min(nruns(b)-j,MAX_nruns);
|
|
|
|
% initialization of next file if necessary
|
|
|
|
if InitSizeArray(b)
|
|
|
|
x2 = zeros(InitSizeArray(b),npar);
|
|
|
|
logpo2 = zeros(InitSizeArray(b),1);
|
|
|
|
NewFile(b) = NewFile(b) + 1;
|
|
|
|
irun = 0;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
j=j+1;
|
|
|
|
irun = irun + 1;
|
|
|
|
end% End of the simulations for one mh-block.
|
|
|
|
record.AcceptationRates(b) = isux/j;
|
2010-06-25 14:58:53 +02:00
|
|
|
if exist('OCTAVE_VERSION') || options_.console_mode
|
2009-12-16 18:17:34 +01:00
|
|
|
printf('\n');
|
2010-06-25 14:58:53 +02:00
|
|
|
diary on
|
2009-12-16 18:17:34 +01:00
|
|
|
elseif ~whoiam
|
|
|
|
close(hh);
|
|
|
|
end
|
2012-08-29 17:54:16 +02:00
|
|
|
[record.Seeds(b).Unifor, record.Seeds(b).Normal] = get_dynare_random_generator_state();
|
2009-12-16 18:17:34 +01:00
|
|
|
OutputFileName(jloop,:) = {[MhDirectoryName,filesep], [ModelName '_mh*_blck' int2str(b) '.mat']};
|
|
|
|
end% End of the loop over the mh-blocks.
|
|
|
|
|
|
|
|
|
|
|
|
myoutput.record = record;
|
|
|
|
myoutput.irun = irun;
|
|
|
|
myoutput.NewFile = NewFile;
|
|
|
|
myoutput.OutputFileName = OutputFileName;
|
|
|
|
|
|
|
|
|