/ concept-collection / walnuts-interactive
Sign in
concept-collection / walnuts-interactive
walnuts-interactive / helpers / grad_log_density.m
18 lines · 493 BCodeBlameHistory
727e2e4Interactive WALNUTS sampling of a 2D banana targetJeremy Magland 1function g = grad_log_density(theta)
2%GRAD_LOG_DENSITY Gradient of the banana log density (see log_density.m).
3a = 2;
4b = 0.2;
5x1 = theta(1);
6x2 = theta(2);
7y1 = x1 / a;
8y2 = x2 * a + a * b * (x1.^2 + a.^2);
9d1 = y1 - 0;
10d2 = y2 - 4;
11% Gradient w.r.t. y of the Gaussian log density is -inv(Sigma)*d.
12gy1 = -((4 / 3) * d1 - (2 / 3) * d2);
13gy2 = -(-(2 / 3) * d1 + (4 / 3) * d2);
14% Chain rule back through the twist y(x).
15gx1 = gy1 / a + gy2 * a * b * 2 * x1;
16gx2 = gy2 * a;
17g = [gx1; gx2];
18end
moveopenescclose