Remove old independent_metropolis_hastings routines
parent
3073988ac1
commit
781ea45777
|
@ -1,119 +0,0 @@
|
||||||
function independent_metropolis_hastings(TargetFun,ProposalFun,xparam1,vv,mh_bounds,varargin)
|
|
||||||
|
|
||||||
% Independent Metropolis-Hastings algorithm.
|
|
||||||
%
|
|
||||||
% 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
|
|
||||||
%
|
|
||||||
% ALGORITHM
|
|
||||||
% Metropolis-Hastings.
|
|
||||||
%
|
|
||||||
% SPECIAL REQUIREMENTS
|
|
||||||
% None.
|
|
||||||
%
|
|
||||||
% PARALLEL CONTEXT
|
|
||||||
% See the comment in random_walk_metropolis_hastings.m funtion.
|
|
||||||
|
|
||||||
% Copyright (C) 2006-2013 Dynare Team
|
|
||||||
%
|
|
||||||
% 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/>.
|
|
||||||
|
|
||||||
global M_ options_ bayestopt_ estim_params_ oo_
|
|
||||||
|
|
||||||
% Initialization of the independent metropolis-hastings chains.
|
|
||||||
|
|
||||||
[ ix2, ilogpo2, ModelName, MetropolisFolder, fblck, fline, npar, nblck, nruns, NewFile, MAX_nruns, d ] = ...
|
|
||||||
metropolis_hastings_initialization(TargetFun, xparam1, vv, mh_bounds, varargin{:});
|
|
||||||
|
|
||||||
xparam1 = transpose(xparam1);
|
|
||||||
InitSizeArray = min([repmat(MAX_nruns,nblck,1) fline+nruns-1],[],2);
|
|
||||||
|
|
||||||
% Load last mh history file
|
|
||||||
load_last_mh_history_file(MetropolisFolder, ModelName);
|
|
||||||
|
|
||||||
%The mandatory variables for local/remote parallel computing are stored in localVars struct.
|
|
||||||
|
|
||||||
localVars = struct('TargetFun', TargetFun, ...
|
|
||||||
'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);
|
|
||||||
localVars.InitSizeArray=InitSizeArray;
|
|
||||||
localVars.record=record;
|
|
||||||
localVars.varargin=varargin;
|
|
||||||
|
|
||||||
% Like a sequential execution!
|
|
||||||
if isnumeric(options_.parallel),
|
|
||||||
fout = independent_metropolis_hastings_core(localVars, fblck, nblck, 0);
|
|
||||||
record = fout.record;
|
|
||||||
% Parallel execution.
|
|
||||||
else
|
|
||||||
% global variables for parallel routines
|
|
||||||
globalVars = struct('M_',M_, ...
|
|
||||||
'options_', options_, ...
|
|
||||||
'bayestopt_', bayestopt_, ...
|
|
||||||
'estim_params_', estim_params_, ...
|
|
||||||
'oo_', oo_);
|
|
||||||
% 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
|
|
||||||
if (options_.load_mh_file~=0) && any(fline>1) ,
|
|
||||||
NamFileInput(length(NamFileInput)+1,:)={[MetropolisFolder filesesep],[ModelName '_mh' int2str(NewFile(1)) '_blck*.mat']};
|
|
||||||
end
|
|
||||||
if exist([ModelName '_optimal_mh_scale_parameter.mat'])
|
|
||||||
NamFileInput(length(NamFileInput)+1,:)={'',[ModelName '_optimal_mh_scale_parameter.mat']};
|
|
||||||
end
|
|
||||||
% from where to get back results
|
|
||||||
% NamFileOutput(1,:) = {[M_.dname,'/metropolis/'],'*.*'};
|
|
||||||
[fout, nBlockPerCPU, totCPU] = masterParallel(options_.parallel, fblck, nblck,NamFileInput,'independent_metropolis_hastings_core', localVars, globalVars, options_.parallel_info);
|
|
||||||
for j=1:totCPU,
|
|
||||||
offset = sum(nBlockPerCPU(1:j-1))+fblck-1;
|
|
||||||
record.LastLogPost(offset+1:sum(nBlockPerCPU(1:j)))=fout(j).record.LastLogPost(offset+1:sum(nBlockPerCPU(1:j)));
|
|
||||||
record.LastParameters(offset+1:sum(nBlockPerCPU(1:j)),:)=fout(j).record.LastParameters(offset+1:sum(nBlockPerCPU(1:j)),:);
|
|
||||||
record.AcceptanceRatio(offset+1:sum(nBlockPerCPU(1:j)))=fout(j).record.AcceptationRates(offset+1:sum(nBlockPerCPU(1:j)));
|
|
||||||
record.LastSeeds(offset+1:sum(nBlockPerCPU(1:j)))=fout(j).record.LastSeeds(offset+1:sum(nBlockPerCPU(1:j)));
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
irun = fout(1).irun;
|
|
||||||
NewFile = fout(1).NewFile;
|
|
||||||
|
|
||||||
update_last_mh_history_file(MetropolisFolder, ModelName, record);
|
|
||||||
|
|
||||||
skipline()
|
|
||||||
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) '.'])
|
|
||||||
disp('Estimation::mcmc: Current acceptance ratio per chain : ')
|
|
||||||
disp(record.AcceptanceRatio);
|
|
||||||
skipline()
|
|
|
@ -1,260 +0,0 @@
|
||||||
function myoutput = independent_metropolis_hastings_core(myinputs,fblck,nblck,whoiam, ThisMatlab)
|
|
||||||
% 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.
|
|
||||||
%
|
|
||||||
% INPUTS
|
|
||||||
% See See the comment in random_walk_metropolis_hastings_core.m funtion.
|
|
||||||
|
|
||||||
% OUTPUTS
|
|
||||||
% See See the comment in random_walk_metropolis_hastings_core.m funtion.
|
|
||||||
%
|
|
||||||
% ALGORITHM
|
|
||||||
% Portion of Independing Metropolis-Hastings.
|
|
||||||
%
|
|
||||||
% SPECIAL REQUIREMENTS.
|
|
||||||
% None.
|
|
||||||
%
|
|
||||||
% Copyright (C) 2006-2013 Dynare Team
|
|
||||||
%
|
|
||||||
% 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
|
|
||||||
|
|
||||||
global bayestopt_ estim_params_ options_ M_ oo_ objective_function_penalty_base
|
|
||||||
|
|
||||||
% 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;
|
|
||||||
InitSizeArray=myinputs.InitSizeArray;
|
|
||||||
record=myinputs.record;
|
|
||||||
varargin=myinputs.varargin;
|
|
||||||
|
|
||||||
if whoiam
|
|
||||||
Parallel=myinputs.Parallel;
|
|
||||||
% initialize persistent variables in priordens()
|
|
||||||
priordens(xparam1',bayestopt_.pshape,bayestopt_.p6,bayestopt_.p7, ...
|
|
||||||
bayestopt_.p3,bayestopt_.p4,1);
|
|
||||||
end
|
|
||||||
|
|
||||||
% (re)Set the penalty.
|
|
||||||
objective_function_penalty_base = Inf;
|
|
||||||
|
|
||||||
MetropolisFolder = CheckPath('metropolis',M_.dname);
|
|
||||||
BaseName = [MetropolisFolder filesep ModelName];
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
% Now I run the (nblck-fblck+1) metropolis-hastings chains
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
jloop=0;
|
|
||||||
|
|
||||||
for b = fblck:nblck,
|
|
||||||
jloop=jloop+1;
|
|
||||||
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.
|
|
||||||
|
|
||||||
% 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
|
|
||||||
set_dynare_random_generator_state(record.InitialSeeds(b).Unifor, ...
|
|
||||||
record.InitialSeeds(b).Normal);
|
|
||||||
catch
|
|
||||||
% if the state set by master is incompatible with the slave, we
|
|
||||||
% only reseed
|
|
||||||
set_dynare_seed(options_.DynareRandomStreams.seed+b);
|
|
||||||
end
|
|
||||||
if (options_.load_mh_file~=0) && (fline(b)>1) && OpenOldFile(b)
|
|
||||||
load([BaseName '_mh' int2str(NewFile(b)) '_blck' int2str(b) '.mat'])
|
|
||||||
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
|
|
||||||
if isoctave || options_.console_mode
|
|
||||||
diary off
|
|
||||||
skipline()
|
|
||||||
elseif whoiam
|
|
||||||
% keyboard;
|
|
||||||
waitbarString = ['Please wait... Metropolis-Hastings (' int2str(b) '/' int2str(options_.mh_nblck) ')...'];
|
|
||||||
% waitbarTitle=['Metropolis-Hastings ',options_.parallel(ThisMatlab).ComputerName];
|
|
||||||
if options_.parallel(ThisMatlab).Local,
|
|
||||||
waitbarTitle=['Local '];
|
|
||||||
else
|
|
||||||
waitbarTitle=[options_.parallel(ThisMatlab).ComputerName];
|
|
||||||
end
|
|
||||||
fMessageStatus(0,whoiam,waitbarString, waitbarTitle, options_.parallel(ThisMatlab));
|
|
||||||
else,
|
|
||||||
hh = waitbar(0,['Please wait... Metropolis-Hastings (' int2str(b) '/' int2str(options_.mh_nblck) ')...']);
|
|
||||||
set(hh,'Name','Metropolis-Hastings');
|
|
||||||
|
|
||||||
end
|
|
||||||
isux = 0;
|
|
||||||
jsux = 0;
|
|
||||||
irun = fline(b);
|
|
||||||
j = 1;
|
|
||||||
while j <= nruns(b)
|
|
||||||
par = feval(ProposalFun, xparam1, proposal_covariance, n);
|
|
||||||
if all( par(:) > mh_bounds.lb ) && all( par(:) < mh_bounds.ub )
|
|
||||||
try
|
|
||||||
logpost = - feval(TargetFun, par(:),varargin{:});
|
|
||||||
catch,
|
|
||||||
logpost = -inf;
|
|
||||||
end
|
|
||||||
else
|
|
||||||
logpost = -inf;
|
|
||||||
end
|
|
||||||
r = logpost - ilogpo2(b) + ...
|
|
||||||
log(feval(ProposalDensity, ix2(b,:), xparam1, proposal_covariance, n)) - ...
|
|
||||||
log(feval(ProposalDensity, par, xparam1, proposal_covariance, n));
|
|
||||||
if (logpost > -inf) && (log(rand) < r)
|
|
||||||
x2(irun,:) = par;
|
|
||||||
ix2(b,:) = par;
|
|
||||||
logpo2(irun) = logpost;
|
|
||||||
ilogpo2(b) = logpost;
|
|
||||||
isux = isux + 1;
|
|
||||||
jsux = jsux + 1;
|
|
||||||
else
|
|
||||||
x2(irun,:) = ix2(b,:);
|
|
||||||
logpo2(irun) = ilogpo2(b);
|
|
||||||
end
|
|
||||||
prtfrc = j/nruns(b);
|
|
||||||
if isoctave || options_.console_mode
|
|
||||||
if mod(j, 10) == 0
|
|
||||||
if isoctave
|
|
||||||
if (whoiam==0),
|
|
||||||
printf('Estimation::mcmc: Computing Metropolis-Hastings (chain %d/%d): %3.f%% done, Current acceptance ratio: %3.f%%\r', b, nblck, 100 * prtfrc, 100 * isux / j);
|
|
||||||
end
|
|
||||||
else
|
|
||||||
fprintf('Estimation::mcmc: Computing Metropolis-Hastings (chain %d/%d): %3.f \b%% done, Current acceptance ratio: %3.f \b%%\r', b, nblck, 100 * prtfrc, 100 * isux / j);
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if mod(j,50)==0 && whoiam,
|
|
||||||
% keyboard;
|
|
||||||
waitbarString = [ '(' int2str(b) '/' int2str(options_.mh_nblck) '), ' sprintf('accept. %3.f%%%%', 100 * isux/j)];
|
|
||||||
fMessageStatus(prtfrc,whoiam,waitbarString, '', options_.parallel(ThisMatlab))
|
|
||||||
end
|
|
||||||
else
|
|
||||||
if mod(j, 3)==0 && ~whoiam
|
|
||||||
waitbar(prtfrc,hh,[ '(' int2str(b) '/' int2str(options_.mh_nblck) ') ' sprintf('%f done, Current acceptance ratio %f',prtfrc,isux/j)]);
|
|
||||||
elseif mod(j,50)==0 && whoiam,
|
|
||||||
% keyboard;
|
|
||||||
waitbarString = [ '(' int2str(b) '/' int2str(options_.mh_nblck) ') ' sprintf('%f done, Current acceptance ratio %f',prtfrc,isux/j)];
|
|
||||||
fMessageStatus(prtfrc,whoiam,waitbarString, waitbarTitle, options_.parallel(ThisMatlab))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if (irun == InitSizeArray(b)) || (j == nruns(b)) % Now I save the simulations
|
|
||||||
save([BaseName '_mh' int2str(NewFile(b)) '_blck' int2str(b) '.mat'],'x2','logpo2');
|
|
||||||
fidlog = fopen([MetropolisFolder '/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,[' Acceptance ratio......: ' 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.LastLogPost(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.AcceptanceRatio(b) = isux/j;
|
|
||||||
if isoctave || options_.console_mode
|
|
||||||
printf('\n');
|
|
||||||
diary on
|
|
||||||
elseif ~whoiam
|
|
||||||
close(hh);
|
|
||||||
end
|
|
||||||
[record.LastSeeds(b).Unifor, record.LastSeeds(b).Normal] = get_dynare_random_generator_state();
|
|
||||||
OutputFileName(jloop,:) = {[MetropolisFolder,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;
|
|
Loading…
Reference in New Issue