Instead of wanting to approximate p(x) p ( x ) , we want to find the global maximum. For example, if p(x) p ( x ) is the likelihood or posterior distribution, we often want to compute the ML and maximum a posteriori (MAP) estimates. As mentioned earlier, we could run a Markov chain of invariant distribution p(x) p ( x ) and estimate the global mode by
Running the Simulated annealing algorithm with a Gaussian proposal distribution q(x∗|x(i))=N(x(i),10) q ( x ∗ | x ( i ) ) = N ( x ( i ) , 10 ) and a bimodal target distribution p(x)∝0.3 exp(−0.2x2)+0.7 exp(−0.2(x−10)2) p ( x ) ∝ 0.3 e x p ( − 0.2 x 2 ) + 0.7 e x p ( − 0.2 ( x − 10 ) 2 ) for 5000 iterations with Ti=(C ln(i+T0))−1 T i = ( C l n ( i + T 0 ) ) − 1 , where C C and T0 T 0 are problem-dependent. In our test, we set C=1, T0=1 C = 1 , T 0 = 1 .
% Metropolis(-Hastings) algorithm
% true (target) pdf is p(x) where we know it but can't sample data.
% proposal (sample) pdf is q(x*|x)=N(x,10) where we can sample.
%%
clc
clear;
X(1)=0;
N=5e3;
p = @(x) 0.3*exp(-0.2*x.^2) + 0.7*exp(-0.2*(x-10).^2);
C = 1; T_0 = 2;
T = @(x) 1.0/(C*log(x+T_0));
dx=0.5; xx=-10:dx:20; fp=p(xx); plot(xx,fp) % plot the true p(x)
%% MH algorithm
sig=(10);
for i=1:N-1
u=rand;
x=X(i);
xs=normrnd(x,sig); % new sample xs based on existing x from proposal pdf.
pxs=p(xs);
px=p(x);
qxs=normpdf(xs,x,sig);
qx=normpdf(x,xs,sig); % get p,q.
if u1,pxs^(1/T(i))*qx/(px^(1/T(i))*qxs)) % case 1: pesudo code
% if u
% if u
X(i+1)=xs;
else
X(i+1)=x;
end
end
% compare pdf of the simulation result with true pdf.
N0=1; close all;figure; %N/5;
nb=histc(X(N0+1:N),xx);
bar(xx+dx/2,nb/(N-N0)/dx); % plot samples.
A=sum(fp)*dx;
hold on; plot(xx,fp/A,'r') % compare.
% figure(2); plot(N0+1:N,X(N0+1:N)) % plot the traces of x.
% compare cdf with true cdf.
F1(1)=0;
F2(1)=0;
for i=2:length(xx)
F1(i)=F1(i-1)+nb(i)/(N-N0);
F2(i)=F2(i-1)+fp(i)*dx/A;
end
figure
plot(xx,[F1' F2'])
max(F1-F2) % this is the true possible measure of accuracy.
[1]. An Introduction to MCMC for Machine Learning.
[2]. https://blog.csdn.net/Eric2016_Lv/article/details/79691392