Contents
clear
clc
Initialize
N = 70;
mu = [2,2];
sig = [2,0.5
0.5,2];
p = @(x) 1/(2*pi)/sqrt(det(sig))*exp(-.5*(x-mu)/(sig)*(x-mu)');
b = 1.2;
x_out = zeros(N,2);
rej_flag = zeros(N,1);
x_rej = zeros(size(x_out));
main loop
x_out(1,:) = randn(1,2)*b + mu;
for i = 2:N
y = randn(1,2)*b + x_out(i-1,:);
alpha = min(p(y)/p(x_out(i-1,:)),1);
u = rand;
if(u<=alpha)
x_out(i,:) = y;
rej_flag(i) = 1;
else
x_out(i,:) = x_out(i-1,:);
x_rej(i,:) = y;
end
end
plot the data
hold on;
[V,D] = eig(sig);
if((sin(acos(V(1,2)))-V(2,2))<0.0001)
theta = rad2deg(acos(V(1,2)));
elseif((sin(acos(V(1,2)))+V(2,2))<0.0001)
theta = rad2deg(-acos(V(1,2)));
end
ecc = sqrt(axes2ecc(D(2,2),D(1,1)));
[elat,elon] = ellipse1(mu(1),mu(2),[D(2,2) ecc], theta );
plot(elat,elon,'-.');
for i = 2:N
s0 = [x_out(i-1,1),x_out(i,1)];
t0 = [x_out(i-1,2),x_out(i,2)];
plot(s0,t0,'-.or','LineWidth',1);
if(rej_flag(i) == 0)
s = [x_out(i-1,1),x_rej(i,1)];
t = [x_out(i-1,2),x_rej(i,2)];
h = plot(s0,t0,'-.or',x_rej(i,1),x_rej(i,2),'+');
plot(s,t,'g','LineWidth',1);
end
end
legend(h,'accepted move','rejected move');
axis([-2,6,-2,6])
axis square