concept-collection / walnuts-interactive
walnuts-interactive / helpers / micro.m
35 lines · 949 BBlameHistoryRaw
1function ell = micro(logmu, gradlogmu, theta_0, rho_0, h_0, delta)
2 ell = 0;
4 max_ell = 8; % default cap
6 while ell <= max_ell
7 % reset initial state for this candidate ell
8 theta = theta_0;
9 rho = rho_0;
11 R = 2^(ell); % number of micro-steps
12 h = h_0 / R; % micro step size
14 % initial energy
15 H = -logmu(theta) + 0.5 * norm(rho)^2;
16 H_max = H;
17 H_min = H;
19 % integrate using *your* leapfrog, one step at a time
20 for j = 1:R
21 [theta, rho] = leapfrog(logmu, gradlogmu, theta, rho, h, 1);
22 H = -logmu(theta) + 0.5 * norm(rho)^2;
23 H_max = max(H_max, H);
24 H_min = min(H_min, H);
25 end
27 % accept this ell if energy variation is small enough
28 if H_max - H_min <= delta
29 return;
30 end
32 % otherwise, refine (halve h, double R)
33 ell = ell + 1;
34 end
35end