-
Notifications
You must be signed in to change notification settings - Fork 1
/
HMC_exact.m
205 lines (138 loc) · 4.45 KB
/
HMC_exact.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
function [Xs, bounce_count] = HMC_exact(F, g, M, mu_r, cov, L, initial_X)
% Implementation of the algorithm described in http://arxiv.org/abs/1208.4118
% Author: Ari Pakman
% Returns samples from a d-dimensional Gaussian with m constraints given by F*X+g >0
% If cov == true
% then M is the covariance and the mean is mu = mu_r
% if cov== false
% then M is the precision matrix and the log-density is -1/2 X'*M*X + r'*X
% Input
% F: m x d matrix
% g: m x 1 vector
% M d x d matrix, must be symmmetric and definite positive
% mu_r d x 1 vector.
% cov: see explanation above
% L: number of samples desired
% initial_X d x 1 vector. Must satisfy the constraint.
% Output
% Xs: d x L matrix, each column is a sample
% bounce_count: number of times the particle bounced
%% go to a whitened frame
m = size(g,1);
if size(F,1) ~= m
display('error');
return
end
if cov
mu= mu_r;
g = g + F*mu;
R = chol(M);
F = F*R';
initial_X= initial_X -mu;
initial_X = R'\initial_X;
else
r=mu_r;
R=chol(M); % M = R'*R
mu = R\(R'\r);
g = g+F*mu;
F = F/R; % this is the time-consuming step in this code section
initial_X= initial_X -mu;
initial_X = R*initial_X;
end
d = size(initial_X,1);
Xs=NaN;
bounce_count =0;
nearzero= 10000*eps;
% Verify that initial_X is feasible
c= F*initial_X +g;
if any(c<0)
display('error: inconsistent initial condition');
return
end
% Unsparcify the linear constraints
g=full(g);
F2 = sum(F.*F,2); % squared norm of the rows of F, needed for reflecting the velocity
F=full(F); % if we don't unsparcify qj= F(j,:)*V/F2(j) becomes very slow.
Ft = F';
%% Sampling loop
last_X= initial_X;
Xs=zeros(d,L);
Xs(:,1)=initial_X;
i=2;
while (i <= L)
i;
stop=0;
j=0;
V0= normrnd(0,1, d,1); % initial velocity
X = last_X;
T=pi/2; % total time the particle will move
tt=0; % records how much time the particle already moved
while (1)
a = V0;
a= real(a);
b = X;
fa = F*a;
fb = F*b;
U = sqrt(fa.^2 + fb.^2);
phi = atan2(-fa,fb); % -pi < phi < +pi
pn = abs(g./U)<=1; % these are the walls that may be hit
% find the first time constraint becomes zero
if any(pn)
inds = find(pn);
phn= phi(pn);
t1=-phn + acos(-g(pn)./U(pn)); % time at which coordinates hit the walls
% this expression always gives the correct result because U*cos(phi + t) + g >= 0.
% if there was a previous reflection (j>0)
% and there is a potential reflection at the sample plane
% make sure that a new reflection at j is not found because of numerical error
if j>0
if pn(j)==1
cs = cumsum(pn);
indj=cs(j);
tt1 = t1(indj);
if abs(tt1) < nearzero || abs(tt1-2*pi)< nearzero
t1(indj)=Inf;
end
end
end
[mt, m_ind] = min(t1);
% find the reflection plane
j = inds(m_ind); % j is an index in the full vector of dim-m, not in the restriced vector determined by pn.
else %if pn(i) =0 for all i
mt =T;
end %if pn(i)
tt=tt+mt;
% fprintf(num2str(tt/T));
if tt>=T
mt= mt-(tt-T);
stop=1;
end
% move the particle a time mt
X = a*sin(mt) + b*cos(mt);
V = a*cos(mt) - b*sin(mt);
if stop
break;
end
% compute reflected velocity
qj= F(j,:)*V/F2(j);
V0 = V -2*qj*Ft(:,j);
bounce_count = bounce_count+ 1;
end % while(1)
% at this point we have a sampled value X, but due to possible
% numerical instabilities we check that the candidate X satisfies the
% constraints before accepting it.
if all(F*X +g > 0)
Xs(:,i)=X;
last_X = X;
i= i+1;
else
disp('hmc reject')
end
end %while (i <= L)
% transform back to the unwhitened frame
if cov
Xs = R'*Xs + repmat(mu, 1,L);
else
Xs = R\Xs + repmat(mu, 1,L);
end
end