Eliminate set_dynare_seed.m calls with globals

kalman-mex
Johannes Pfeifer 2023-09-25 13:25:39 +02:00
parent f2abdb6ec8
commit e65662151f
18 changed files with 148 additions and 106 deletions

View File

@ -85,7 +85,7 @@ fprintf(fidlog,'%% Session 1.\n');
fprintf(fidlog,' \n');
prior_draw(bayestopt_,options_.prior_trunc);
% Find initial values for the NumberOfParticles chains...
set_dynare_seed('default');
options_=set_dynare_seed_local_options(options_,'default');
fprintf(fidlog,[' Initial values of the parameters:\n']);
disp('Estimation::dsmh: Searching for initial values...');
ix2 = zeros(npar,NumberOfParticles);

View File

@ -155,7 +155,7 @@ if nargin<6 || isempty(innovations)
covariance_matrix_upper_cholesky = chol(covariance_matrix);
% Set seed to its default state.
if DynareOptions.bnlms.set_dynare_seed_to_default
set_dynare_seed('default');
DynareOptions=set_dynare_seed_local_options(DynareOptions,'default');
end
% Simulate structural innovations.
switch DynareOptions.bnlms.innovation_distribution

View File

@ -97,7 +97,7 @@ end
% Set seed.
if ep.set_dynare_seed_to_default
set_dynare_seed('default');
DynareOptions=set_dynare_seed_local_options(DynareOptions,'default');
end
% hybrid correction

View File

@ -142,7 +142,7 @@ priordens([],[],[],[],[],[],1);
dyn_first_order_solver();
% Set dynare random generator and seed.
set_dynare_seed('default');
options_=set_dynare_seed_local_options(options_,'default');
% Load user configuration file.
if isfield(options_, 'global_init_file')

View File

@ -83,7 +83,7 @@ state_variance_rank = size(StateVectorVarianceSquareRoot,2);
%end
% Set seed for randn().
set_dynare_seed('default');
DynareOptions=set_dynare_seed_local_options(DynareOptions,'default');
% Initialization of the likelihood.
const_lik = log(2*pi)*number_of_observed_variables;

View File

@ -77,7 +77,7 @@ state_variance_rank = size(StateVectorVarianceSquareRoot,2);
Q_lower_triangular_cholesky = chol(Q)';
% Set seed for randn().
set_dynare_seed('default');
DynareOptions=set_dynare_seed_local_options(DynareOptions,'default');
% Initialization of the likelihood.
const_lik = log(2*pi)*number_of_observed_variables+log(det(H));

View File

@ -78,7 +78,7 @@ state_variance_rank = size(StateVectorVarianceSquareRoot, 2);
Q_lower_triangular_cholesky = chol(Q)';
% Set seed for randn().
set_dynare_seed('default');
DynareOptions=set_dynare_seed_local_options(DynareOptions,'default');
% Initialization of the likelihood.
lik = NaN(T, 1);

View File

@ -74,7 +74,7 @@ else
end
if ParticleOptions.distribution_approximation.montecarlo
set_dynare_seed('default');
DynareOptions=set_dynare_seed_local_options(DynareOptions,'default');
end
% Get covariance matrices

View File

@ -79,7 +79,7 @@ else
end
if ParticleOptions.distribution_approximation.montecarlo
set_dynare_seed('default');
DynareOptions=set_dynare_seed_local_options(DynareOptions,'default');
end
% Get covariance matrices

View File

@ -105,7 +105,7 @@ else
end
if ParticleOptions.distribution_approximation.montecarlo
set_dynare_seed('default');
DynareOptions=set_dynare_seed_local_options(DynareOptions,'default');
end
% Get covariance matrices

View File

@ -39,7 +39,7 @@ function [pmean, pmode, pmedian, pstdev, p025, p975, covariance] = online_auxili
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
% Set seed for randn().
set_dynare_seed('default');
DynareOptions=set_dynare_seed_local_options(DynareOptions,'default');
pruning = DynareOptions.particle.pruning;
second_resample = DynareOptions.particle.resampling.status.systematic;
variance_update = true;

View File

@ -101,7 +101,7 @@ state_variance_rank = size(StateVectorVarianceSquareRoot,2);
Q_lower_triangular_cholesky = chol(Q)';
% Set seed for randn().
set_dynare_seed('default');
DynareOptions=set_dynare_seed_local_options(DynareOptions,'default');
% Initialization of the weights across particles.
weights = ones(1,number_of_particles)/number_of_particles ;

View File

@ -64,7 +64,7 @@ isHybridMatlabOctave = isHybridMatlabOctave && ~isoctave;
options_.parallel_info.isHybridMatlabOctave = isHybridMatlabOctave;
if isHybridMatlabOctave
% Reset dynare random generator and seed.
set_dynare_seed('default');
options_=set_dynare_seed_local_options(options_,'default');
end

View File

@ -126,15 +126,15 @@ for curr_block = fblck:nblck
%
% Set the random number generator type (the seed is useless but needed by the function)
if ~isoctave
set_dynare_seed(options_.DynareRandomStreams.algo, options_.DynareRandomStreams.seed);
options_=set_dynare_seed_local_options(options_,options_.DynareRandomStreams.algo, options_.DynareRandomStreams.seed);
else
set_dynare_seed(options_.DynareRandomStreams.seed+curr_block);
options_=set_dynare_seed_local_options(options_,options_.DynareRandomStreams.seed+curr_block);
end
% Set the state of the RNG
set_dynare_random_generator_state(record.InitialSeeds(curr_block).Unifor, record.InitialSeeds(curr_block).Normal);
catch
% If the state set by master is incompatible with the slave, we only reseed
set_dynare_seed(options_.DynareRandomStreams.seed+curr_block);
options_=set_dynare_seed_local_options(options_,options_.DynareRandomStreams.seed+curr_block);
end
mh_recover_flag=0;
if (options_.load_mh_file~=0) && (fline(curr_block)>1) && OpenOldFile(curr_block) %load previous draws and likelihood

View File

@ -176,7 +176,7 @@ if ~options_.load_mh_file && ~options_.mh_recover
end
% Find initial values for the NumberOfBlocks chains...
if NumberOfBlocks > 1 || options_.mh_initialize_from_previous_mcmc.status% Case 1: multiple chains
set_dynare_seed('default');
options_=set_dynare_seed_local_options(options_,'default');
fprintf(fidlog,[' Initial values of the parameters:\n']);
fprintf('%s: Searching for initial values...\n', dispString);
if ~options_.mh_initialize_from_previous_mcmc.status
@ -295,7 +295,7 @@ if ~options_.load_mh_file && ~options_.mh_recover
else
for j=1:NumberOfBlocks
% we set a different seed for the random generator for each block then we record the corresponding random generator state (vector)
set_dynare_seed(options_.DynareRandomStreams.seed+j);
options_=set_dynare_seed_local_options(options_,options_.DynareRandomStreams.seed+j);
% record.Seeds keeps a vector of the random generator state and not the scalar seed despite its name
[record.InitialSeeds(j).Unifor,record.InitialSeeds(j).Normal] = get_dynare_random_generator_state();
end

View File

@ -1,8 +1,10 @@
function set_dynare_seed(a,b)
% Set seeds depending on matlab (octave) version. This routine is called in dynare_config and can be called by the
function set_dynare_seed(varargin)
% Set seeds depending on matlab (octave) version. This routine is called is
% a wrapper for set_dynare_seed_local_options
% dynare_config and can be called by the
% user in the mod file.
%
% Copyright © 2010-2020 Dynare Team
% Copyright © 2010-2023 Dynare Team
%
% This file is part of Dynare.
%
@ -18,92 +20,11 @@ function set_dynare_seed(a,b)
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
global options_
if ~nargin
if nargin<1
error('set_dynare_seed:: I need at least one input argument!')
end
matlab_random_streams = ~(isoctave || options_.parallel_info.isHybridMatlabOctave);
if matlab_random_streams% Use new matlab interface.
if nargin==1
if ischar(a) && strcmpi(a,'default')
options_.DynareRandomStreams.algo = 'mt19937ar';
options_.DynareRandomStreams.seed = 0;
s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
reset(RandStream.setGlobalStream(s));
return
end
if ischar(a) && strcmpi(a,'reset')
s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
reset(RandStream.setGlobalStream(s));
return
end
if ~ischar(a) || (ischar(a) && strcmpi(a, 'clock'))
options_.DynareRandomStreams.algo = 'mt19937ar';
if ischar(a)
options_.DynareRandomStreams.seed = rem(floor(now*24*60*60), 2^32);
else
options_.DynareRandomStreams.seed = a;
end
s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
reset(RandStream.setGlobalStream(s));
return
end
error('set_dynare_seed:: something is wrong in the calling sequence!')
elseif nargin==2
if ~ischar(a) || ~( strcmpi(a,'mcg16807') || ...
strcmpi(a,'mlfg6331_64') || ...
strcmpi(a,'mrg32k3a') || ...
strcmpi(a,'mt19937ar') || ...
strcmpi(a,'shr3cong') || ...
strcmpi(a,'swb2712') )
disp('set_dynare_seed:: First argument must be string designing the uniform random number algorithm!')
RandStream.list
skipline()
disp('set_dynare_seed:: Change the first input accordingly...')
skipline()
error(' ')
end
if ~isint(b)
error('set_dynare_seed:: The second input argument must be an integer!')
end
options_.DynareRandomStreams.algo = a;
options_.DynareRandomStreams.seed = b;
s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
reset(RandStream.setGlobalStream(s));
end
else% Use old matlab interface.
if nargin==1
if ischar(a) && strcmpi(a,'default')
if isoctave
options_.DynareRandomStreams.algo = 'state';
else
options_.DynareRandomStreams.algo = 'twister';
end
options_.DynareRandomStreams.seed = 0;
rand(options_.DynareRandomStreams.algo,options_.DynareRandomStreams.seed);
randn('state',options_.DynareRandomStreams.seed);
return
end
if ischar(a) && strcmpi(a,'reset')
rand(options_.DynareRandomStreams.algo,options_.DynareRandomStreams.seed);
randn('state',options_.DynareRandomStreams.seed);
return
end
if (~ischar(a) && isint(a)) || (ischar(a) && strcmpi(a,'clock'))
if ischar(a)
options_.DynareRandomStreams.seed = floor(now*24*60*60);
else
options_.DynareRandomStreams.seed = a;
end
rand(options_.DynareRandomStreams.algo,options_.DynareRandomStreams.seed);
randn('state',options_.DynareRandomStreams.seed);
return
end
error('set_dynare_seed:: Something is wrong in the calling sequence!')
else
error('set_dynare_seed:: Cannot use more than one input argument with your version of Matlab/Octave!')
end
end
options_=set_dynare_seed_local_options(options_,varargin{:});

View File

@ -0,0 +1,121 @@
function options_=set_dynare_seed_local_options(options_,a,b)
% options_=set_dynare_seed_local_options(options_,a,b)
% Set seeds depending on Matlab (octave) version
% Inputs:
% o options_ options structure
% o a first input argument,
% for single argument input, either
% [number] seed
% [string] 'default'
% 'reset'
% 'clock'
% for two inputs:
% [string] indicating feasible RNG algorithm (default: 'mt19937ar')
% o b second input argument
% [number] seed for algorithm
% specified with first input
% Copyright © 2010-2023 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 <https://www.gnu.org/licenses/>.
if nargin<2
error('set_dynare_seed:: I need at least two input arguments!')
end
matlab_random_streams = ~(isoctave || options_.parallel_info.isHybridMatlabOctave);
if matlab_random_streams% Use new matlab interface.
if nargin==2
if ischar(a) && strcmpi(a,'default')
options_.DynareRandomStreams.algo = 'mt19937ar';
options_.DynareRandomStreams.seed = 0;
s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
reset(RandStream.setGlobalStream(s));
return
end
if ischar(a) && strcmpi(a,'reset')
s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
reset(RandStream.setGlobalStream(s));
return
end
if ~ischar(a) || (ischar(a) && strcmpi(a, 'clock'))
options_.DynareRandomStreams.algo = 'mt19937ar';
if ischar(a)
options_.DynareRandomStreams.seed = rem(floor(now*24*60*60), 2^32);
else
options_.DynareRandomStreams.seed = a;
end
s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
reset(RandStream.setGlobalStream(s));
return
end
error('set_dynare_seed:: something is wrong in the calling sequence!')
elseif nargin==3
if ~ischar(a) || ~( strcmpi(a,'mcg16807') || ...
strcmpi(a,'mlfg6331_64') || ...
strcmpi(a,'mrg32k3a') || ...
strcmpi(a,'mt19937ar') || ...
strcmpi(a,'shr3cong') || ...
strcmpi(a,'swb2712') )
disp('set_dynare_seed:: First argument must be string designing the uniform random number algorithm!')
RandStream.list
skipline()
disp('set_dynare_seed:: Change the first input accordingly...')
skipline()
error(' ')
end
if ~isint(b)
error('set_dynare_seed:: The second input argument must be an integer!')
end
options_.DynareRandomStreams.algo = a;
options_.DynareRandomStreams.seed = b;
s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
reset(RandStream.setGlobalStream(s));
end
else% Use old matlab interface.
if nargin==2
if ischar(a) && strcmpi(a,'default')
if isoctave
options_.DynareRandomStreams.algo = 'state';
else
options_.DynareRandomStreams.algo = 'twister';
end
options_.DynareRandomStreams.seed = 0;
rand(options_.DynareRandomStreams.algo,options_.DynareRandomStreams.seed);
randn('state',options_.DynareRandomStreams.seed);
return
end
if ischar(a) && strcmpi(a,'reset')
rand(options_.DynareRandomStreams.algo,options_.DynareRandomStreams.seed);
randn('state',options_.DynareRandomStreams.seed);
return
end
if (~ischar(a) && isint(a)) || (ischar(a) && strcmpi(a,'clock'))
if ischar(a)
options_.DynareRandomStreams.seed = floor(now*24*60*60);
else
options_.DynareRandomStreams.seed = a;
end
rand(options_.DynareRandomStreams.algo,options_.DynareRandomStreams.seed);
randn('state',options_.DynareRandomStreams.seed);
return
end
error('set_dynare_seed:: Something is wrong in the calling sequence!')
else
error('set_dynare_seed:: Cannot use more than one input argument with your version of Matlab/Octave!')
end
end

View File

@ -53,7 +53,7 @@ if nargin<2 || isempty(innovations)
covariance_matrix_upper_cholesky = chol(covariance_matrix);
% Set seed to its default state.
if options_.bnlms.set_dynare_seed_to_default
set_dynare_seed('default');
options_=set_dynare_seed_local_options(options_,'default');
end
% Simulate structural innovations.
switch options_.bnlms.innovation_distribution