1function lp = log_density(theta)
2%LOG_DENSITY Unnormalized log density of the currently selected 2D target.
3% The target is chosen by the global WTARGET (1=banana, 2=gaussian,
4% 3=correlated, 4=donut), set by walnuts_sampler. Defaults to the banana.
5% Normalizing constants are dropped (irrelevant for sampling and the heatmap).
6global WTARGET
7t = WTARGET;
8if isempty(t)
9 t = 1;
10end
11x = theta(1);
12y = theta(2);
13switch t
14 case 2 % standard Gaussian N(0, I)
15 lp = -0.5 * (x.^2 + y.^2);
16 case 3 % correlated Gaussian, covariance [1 0.8; 0.8 1]
17 lp = -0.5 * (x.^2 - 2 * 0.8 * x .* y + y.^2) / (1 - 0.8^2);
18 case 4 % donut / ring: radius 2.5, variance 0.15 in the radial direction
19 r = sqrt(x.^2 + y.^2);
20 lp = -(r - 2.5).^2 / (2 * 0.15);
21 otherwise % 1: banana (correlated Gaussian under a quadratic twist)
22 a = 2;
23 b = 0.2;
24 y1 = x / a;
25 y2 = y * a + a * b * (x.^2 + a.^2);
26 d1 = y1;
27 d2 = y2 - 4;
28 lp = -0.5 * ((4 / 3) * (d1.^2 + d2.^2) - (4 / 3) * d1 .* d2);
29end
30end