forked from mudigonda/HMC_reducedflip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_samplers.m
135 lines (121 loc) · 3.47 KB
/
test_samplers.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
HOME=getenv('HOME');
whos
clc;
close all;
opts_init = [];
opts_init.E = @E_gauss;
opts_init.dEdX = @dEdX_gauss;
% make this 1 for more output
opts_init.Debug = 0;
opts_init.LeapSize = 1;
opts_init.epsilon = 1.3;
%Model Name
FEVAL_MAX = 5000000
modelname='2D'
Nsamp = 10000;
opts_init.BatchSize = 1000;
opts_init.DataSize = 2;
opts_init.funcevals = 0;
theta = diag(exp(linspace(log(1e-4), log(1), opts_init.DataSize)));
%opts_init.Xinit = sqrtm(inv(theta))*randn( opts_init.DataSize, opts_init.BatchSize );
opts_init.Xinit = randn( opts_init.DataSize, opts_init.BatchSize );
%Initalize Options
ii = 1;
names{ii} = 'standard';
opts{ii} = opts_init;
opts{ii}.FlipOnReject = 0;
opts{ii}.alpha = 1;
%Initialize States
states{ii} = [];
% arrays to keep track of the samples
X{ii} = zeros(opts{ii}.DataSize,Nsamp);
fevals{ii} = [];
ii = ii + 1;
names{ii} = 'persist';
opts{ii} = opts_init;
opts{ii}.FlipOnReject = 0;
%Initialize States
states{ii} = [];
% arrays to keep track of the samples
X{ii} = zeros(opts{ii}.DataSize,Nsamp);
fevals{ii} = [];
ii = ii + 1;
names{ii} = 'reduced flip';
opts{ii} = opts_init;
opts{ii}.FlipOnReject = 1;
%Initialize States
states{ii} = [];
% arrays to keep track of the samples
X{ii} = zeros(opts{ii}.DataSize,Nsamp);
fevals{ii} = [];
ii = ii + 1;
names{ii} = 'forever forward';
opts{ii} = opts_init;
opts{ii}.FlipOnReject = 3;
%Initialize States
states{ii} = [];
% arrays to keep track of the samples
X{ii} = zeros(opts{ii}.DataSize,Nsamp);
fevals{ii} = [];
ii = ii + 1;
names{ii} = 'default + ff';
opts{ii} = opts_init;
opts{ii}.FlipOnReject = 3;
opts{ii}.alpha = 1;
%Initialize States
states{ii} = [];
%arrays to keep track of samples
X{ii} = zeros(opts{ii}.DataSize,Nsamp);
fevals{ii} = [];
if 0
ii = ii + 1;
names{ii} = 'two momentum';
opts{ii} = opts_init;
opts{ii}.FlipOnReject = 2;
%Initialize States
states{ii} = [];
% arrays to keep track of the samples
X{ii} = zeros(opts{ii}.DataSize,Nsamp);
fevals{ii} = [];
end
RUN_FLAG=1;
ttt = tic();
ii=1;
% call the sampling algorithm Nsamp times
while (ii <=Nsamp && RUN_FLAG == 1)
for jj = 1:length(names)
if ii == 1 || states{jj}.funcevals < FEVAL_MAX
[Xloc, statesloc] = rf2vHMC( opts{jj}, states{jj},theta);
states{jj} = statesloc;
if ii > 1
X{jj} = cat(3,X{jj}, Xloc);
else
X{jj} = Xloc;
end
fevals{jj}(ii,1) = states{jj}.funcevals;
assert(opts_init.BatchSize == size(Xloc,2));
else
RUN_FLAG = 0;
break;
end
end
%Display + Saving
if (mod( ii, 10 ) == 0) || (ii == Nsamp) || RUN_FLAG == 0
fprintf('%d / %d in %f sec (%f sec remaining)\n', ii, Nsamp, toc(ttt), toc(ttt)*Nsamp/ii - toc(ttt) );
for jj = 1:length(names)
disp(names{jj})
disp(states{jj})
disp(states{jj}.steps)
disp(states{jj}.steps.leap')
disp('most recent covariance')
xx = X{jj}(:,:,end);
disp( xx * xx' / size(xx,2))
disp('total covariance')
xx = X{jj}(:,:,:);
xx = reshape(xx, size(xx,1), size(xx,2)*size(xx,3));
disp( xx * xx' / size(xx,2))
end
end
ii = ii + 1;
end
ttt = toc(ttt);