Debian Codegraph @

tests/parallel/_estimation_commands_with_different_seeds.inc

raw
1 /*
2 * Contains the core estimation logic used by the serial and PCT test files.
3 * It runs estimations with different seeding strategies (start/default, specific seed 123,
4 * no seed, reset to default) and performs checks to verify that seeds behave as expected
5 * (e.g., specific seeds produce identical results, different seeds produce different results).
6 */
7
8 /*
9 * Copyright © 2026 Dynare Team
10 *
11 * This file is part of Dynare.
12 *
13 * Dynare is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * Dynare is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with Dynare. If not, see <https://www.gnu.org/licenses/>.
25 */
26
27 @#define MH_REPLIC = 3000
28
29 % first run of a mod file uses set_dynare_seed('default');
30 fprintf('Seed at start:\n');
31 disp(options_.DynareRandomStreams);
32 estimation(datafile=data_ca1,first_obs=8,nobs=79,prefilter=1,
33 mode_compute=0,nograph,nodisplay,
34 mode_file='ls2003/Output/ls2003_mode',
35 mh_nblocks=@{MH_NBLOCKS},mh_jscale=0.5,mh_replic=@{MH_REPLIC},
36 dirname='@{DIRNAME}_start');
37 @#for i in 1:MH_NBLOCKS
38 seed_start_@{i} = load(['@{DIRNAME}_start/metropolis/' M_.fname '_mh1_blck@{i}.mat']);
39 @#endfor
40
41 set_dynare_seed(123); % should give different results to default
42 fprintf('Seed after set_dynare_seed(''123''):\n');
43 disp(options_.DynareRandomStreams);
44 estimation(datafile=data_ca1,first_obs=8,nobs=79,prefilter=1,
45 mode_compute=0,nograph,nodisplay,
46 mode_file='ls2003/Output/ls2003_mode',
47 mh_nblocks=@{MH_NBLOCKS},mh_jscale=0.5,mh_replic=@{MH_REPLIC},
48 dirname='@{DIRNAME}_123');
49 @#for i in 1:MH_NBLOCKS
50 seed_123_@{i} = load(['@{DIRNAME}_123/metropolis/' M_.fname '_mh1_blck@{i}.mat']);
51 @#endfor
52
53 fprintf('Seed without any set_dynare_seed command:\n');
54 disp(options_.DynareRandomStreams);
55 estimation(datafile=data_ca1,first_obs=8,nobs=79,prefilter=1,
56 mode_compute=0,nograph,nodisplay,
57 mode_file='ls2003/Output/ls2003_mode',
58 mh_nblocks=@{MH_NBLOCKS},mh_jscale=0.5,mh_replic=@{MH_REPLIC},
59 dirname='@{DIRNAME}_noseed');
60 @#for i in 1:MH_NBLOCKS
61 noseed_@{i} = load(['@{DIRNAME}_noseed/metropolis/' M_.fname '_mh1_blck@{i}.mat']);
62 @#endfor
63
64 set_dynare_seed('default'); % should be equal to serial_start
65 fprintf('Seed after set_dynare_seed(''default''):\n');
66 disp(options_.DynareRandomStreams);
67 estimation(datafile=data_ca1,first_obs=8,nobs=79,prefilter=1,
68 mode_compute=0,nograph,nodisplay,
69 mode_file='ls2003/Output/ls2003_mode',
70 mh_nblocks=@{MH_NBLOCKS},mh_jscale=0.5,mh_replic=@{MH_REPLIC},
71 dirname='@{DIRNAME}_default');
72 @#for i in 1:MH_NBLOCKS
73 seed_default_@{i} = load(['@{DIRNAME}_default/metropolis/' M_.fname '_mh1_blck@{i}.mat']);
74 @#endfor
75
76 @#for i in 1:MH_NBLOCKS
77
78 fprintf('\nRESULTS FOR BLOCK @{i}:\n');
79
80 msg = sprintf('seed at start and set_dynare_seed(''default'') should be equal!');
81 if isequaln(seed_start_@{i}.x2, seed_default_@{i}.x2)==1 && isequaln(seed_start_@{i}.logpo2, seed_default_@{i}.logpo2)==1
82 fprintf('✅ %s\n',msg);
83 else
84 error('❌ %s',msg);
85 end
86
87 msg = sprintf('seed at start and set_dynare_seed(123) should not be equal!');
88 if isequaln(seed_start_@{i}.x2, seed_123_@{i}.x2)==1 && isequaln(seed_start_@{i}.logpo2, seed_123_@{i}.logpo2)==1
89 error('❌ %s',msg);
90 else
91 fprintf('✅ %s\n',msg);
92 end
93
94 msg = sprintf('seed at start and noseed should not be equal!');
95 if isequaln(seed_start_@{i}.x2, noseed_@{i}.x2)==1 && isequaln(seed_start_@{i}.logpo2, noseed_@{i}.logpo2)==1
96 error('❌ %s',msg);
97 else
98 fprintf('✅ %s\n',msg);
99 end
100
101 msg = sprintf('set_dynare_seed(''default'') and set_dynare_seed(123) should not be equal!');
102 if isequaln(seed_default_@{i}.x2, seed_123_@{i}.x2)==1 && isequaln(seed_default_@{i}.logpo2, seed_123_@{i}.logpo2)==1
103 error('❌ %s',msg);
104 else
105 fprintf('✅ %s\n',msg);
106 end
107
108 msg = sprintf('set_dynare_seed(''default'') and noseed (which came directly after set_dynare_seed(''123'') should not be equal!');
109 if isequaln(seed_default_@{i}.x2, noseed_@{i}.x2)==1 && isequaln(seed_default_@{i}.logpo2, noseed_@{i}.logpo2)==1
110 error('❌ %s',msg);
111 else
112 fprintf('✅ %s\n',msg);
113 end
114
115 msg = sprintf('set_dynare_seed(123) and noseed (which came directly after set_dynare_seed(''123'')) should be equal!');
116 if isequaln(seed_123_@{i}.x2, noseed_@{i}.x2)==1 && isequaln(seed_123_@{i}.logpo2, noseed_@{i}.logpo2)==1
117 fprintf('✅ %s\n',msg);
118 else
119 error('❌ %s',msg);
120 end
121
122 @#endfor
123 fprintf('\n');