2015-04-04 19:36:57 +02:00
function [xparams, logpost, options_]= metropolis_draw ( init,options_,estim_params_,M_)
2023-07-13 20:05:52 +02:00
% function [xparams, logpost, options_]=metropolis_draw(init,options_,estim_params_,M_)
2009-12-16 18:17:34 +01:00
% Builds draws from metropolis
%
% INPUTS:
2017-05-16 15:10:20 +02:00
% init: scalar equal to
% 1: first call to store the required information
% on files, lines, and chains to be read
% in persistent variables to make them available
2015-04-04 19:36:57 +02:00
% for future calls
% 0: load a parameter draw
% Additional Inputs required for initialization
% options_ [structure] Matlab's structure describing the options (initialized by dynare, see @ref{options_}).
% M_ [structure] Matlab's structure describing the Model (initialized by dynare, see @ref{M_}).
% estim_params_ [structure] Matlab's structure describing the estimated_parameters (initialized by dynare, see @ref{estim_params_}).
2009-12-16 18:17:34 +01:00
%
% OUTPUTS:
2015-04-04 19:36:57 +02:00
% xparams: if init==0: vector of estimated parameters
% if init==1: error flaog
2009-12-16 18:17:34 +01:00
% logpost: log of posterior density
2015-04-04 19:36:57 +02:00
% options_: [structure] Matlab's structure describing the options (initialized by dynare, see @ref{options_}).
2017-05-16 15:10:20 +02:00
%
2009-12-16 18:17:34 +01:00
% SPECIAL REQUIREMENTS
2015-04-04 19:36:57 +02:00
%
% Requires CutSample to be run before in order to set up mh_history-file
2009-12-16 18:17:34 +01:00
2023-07-13 18:01:02 +02:00
% Copyright © 2003-2023 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
2021-06-09 17:33:48 +02:00
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
2009-12-16 18:17:34 +01:00
2013-11-20 18:03:12 +01:00
persistent mh_nblck NumberOfDraws BaseName FirstLine FirstMhFile MAX_nruns
2009-12-16 18:17:34 +01:00
2013-12-18 16:39:41 +01:00
xparams = 0 ;
logpost = 0 ;
2009-12-16 18:17:34 +01:00
if init
2015-04-04 19:36:57 +02:00
%get number of parameters
2009-12-16 18:17:34 +01:00
nvx = estim_params_ . nvx ;
nvn = estim_params_ . nvn ;
ncx = estim_params_ . ncx ;
ncn = estim_params_ . ncn ;
np = estim_params_ . np ;
npar = nvx + nvn + ncx + ncn + np ;
2015-04-04 19:36:57 +02:00
%get path of metropolis files
2013-11-20 18:03:12 +01:00
MetropolisFolder = CheckPath ( ' metropolis' , M_ . dname ) ;
FileName = M_ . fname ;
BaseName = [ MetropolisFolder filesep FileName ] ;
2015-04-04 19:36:57 +02:00
%load mh_history-file with info on what to load
2023-07-13 18:01:02 +02:00
record = load_last_mh_history_file ( MetropolisFolder , FileName ) ;
2009-12-16 18:17:34 +01:00
FirstMhFile = record . KeepedDraws . FirstMhFile ;
2017-05-16 15:10:20 +02:00
FirstLine = record . KeepedDraws . FirstLine ;
2009-12-16 18:17:34 +01:00
TotalNumberOfMhDraws = sum ( record . MhDraws ( : , 1 ) ) ;
NumberOfDraws = TotalNumberOfMhDraws - floor ( options_ . mh_drop * TotalNumberOfMhDraws ) ;
2015-04-04 19:36:57 +02:00
MAX_nruns = ceil ( options_ . MaxNumberOfBytes / ( npar + 2 ) / 8 ) ; %number of parameters plus posterior plus ?
2009-12-16 18:17:34 +01:00
mh_nblck = options_ . mh_nblck ;
2013-12-18 16:39:41 +01:00
% set sub_draws option if empty
if isempty ( options_ . sub_draws )
2017-03-03 17:03:59 +01:00
options_ . sub_draws = min ( options_ . posterior_max_subsample_draws , ceil ( . 25 * NumberOfDraws ) ) ;
2013-12-18 16:39:41 +01:00
else
2017-01-17 19:07:24 +01:00
if options_ . sub_draws > NumberOfDraws * mh_nblck
2013-12-18 16:39:41 +01:00
skipline ( )
2023-09-01 21:42:51 +02:00
disp ( [ ' The value of option sub_draws (' num2str ( options_ . sub_draws ) ' ) is greater than the number of available draws in the MCMC (' num2str ( NumberOfDraws * mh_nblck ) ' )!' ] )
disp ( ' You can either change the value of sub_draws, reduce the value of mh_drop, or run another mcmc (with the load_mh_file option).' )
2013-12-18 16:39:41 +01:00
skipline ( )
xparams = 1 ; % xparams is interpreted as an error flag
end
end
2009-12-16 18:17:34 +01:00
return
2015-04-04 19:36:57 +02:00
else %not initialization, return one draw
2017-05-16 15:10:20 +02:00
%get random draw from random chain
2015-04-04 19:36:57 +02:00
ChainNumber = ceil ( rand * mh_nblck ) ;
DrawNumber = ceil ( rand * NumberOfDraws ) ;
2009-12-16 18:17:34 +01:00
2015-04-04 19:36:57 +02:00
if DrawNumber < = MAX_nruns - FirstLine + 1 %draw in first file, needs to account for first line
MhFilNumber = FirstMhFile ;
MhLine = FirstLine + DrawNumber - 1 ;
2017-05-16 15:10:20 +02:00
else %draw in other file
2015-04-04 19:36:57 +02:00
DrawNumber = DrawNumber - ( MAX_nruns - FirstLine + 1 ) ;
2017-05-16 15:10:20 +02:00
MhFilNumber = FirstMhFile + ceil ( DrawNumber / MAX_nruns ) ;
2015-04-04 19:36:57 +02:00
MhLine = DrawNumber - ( MhFilNumber - FirstMhFile - 1 ) * MAX_nruns ;
end
%load parameters and posterior
load ( [ BaseName ' _mh' int2str ( MhFilNumber ) ' _blck' int2str ( ChainNumber ) ' .mat' ] , ' x2' , ' logpo2' ) ;
xparams = x2 ( MhLine , : ) ;
logpost = logpo2 ( MhLine ) ;
end