mhsample
statistics: [smpl, accept] = mhsample (start, nsamples, property, value, …)
Draws nsamples samples from a target stationary distribution pdf using Metropolis-Hastings algorithm.
Inputs:
Some property-value pairs can or must be specified, they are:
(Required) One of:
or
In case optional argument symmetric is set to false (the default), one of:
or
The following input property/pair values may be needed depending on the desired output:
Outputs:
Example : Sampling from a normal distribution
start = 1; nsamples = 1e3; pdf = @(x) exp (-.5 * x .^ 2) / (pi ^ .5 * 2 ^ .5); proppdf = @(x,y) 1 / 6; proprnd = @(x) 6 * (rand (size (x)) - .5) + x; [smpl, accept] = mhsample (start, nsamples, "pdf", pdf, "proppdf", ... proppdf, "proprnd", proprnd, "thin", 4); histfit (smpl); |
See also: rand, slicesample
Source Code: mhsample
Define function to sample
d = 2;
mu = [-1; 2];
rand ('seed', 5) # for reproducibility
Sigma = rand (d);
Sigma = (Sigma + Sigma');
Sigma += eye (d) * abs (eigs (Sigma, 1, 'sa')) * 1.1;
pdf = @(x)(2*pi)^(-d/2)*det (Sigma)^-.5*exp (-.5*sum ((x.'-mu).*(Sigma\(x.'-mu)),1));
Inputs
start = ones (1, 2);
nsamples = 500;
sym = true;
K = 500;
m = 10;
rand ('seed', 8) # for reproducibility
proprnd = @(x) (rand (size (x)) - .5) * 3 + x;
[smpl, accept] = mhsample (start, nsamples, 'pdf', pdf, 'proprnd', proprnd, ...
'symmetric', sym, 'burnin', K, 'thin', m);
figure;
hold on;
plot (smpl(:, 1), smpl(:, 2), 'x');
[x, y] = meshgrid (linspace (-6, 4), linspace (-3, 7));
z = reshape (pdf ([x(:), y(:)]), size (x));
mesh (x, y, z, 'facecolor', 'None');
Using sample points to find the volume of half a sphere with radius of .5
f = @(x) ((.25-(x(:,1)+1).^2-(x(:,2)-2).^2).^.5.*(((x(:,1)+1).^2+(x(:,2)-2).^2)<.25)).';
int = mean (f(smpl) ./ pdf (smpl));
errest = std (f(smpl) ./ pdf (smpl)) / nsamples ^ .5;
trueerr = abs (2 / 3 * pi * .25 ^ (3 / 2) - int);
printf ("Monte Carlo integral estimate int f(x) dx = %f\n", int);
Monte Carlo integral estimate int f(x) dx = 0.242643
printf ("Monte Carlo integral error estimate %f\n", errest);
Monte Carlo integral error estimate 0.028039
printf ("The actual error %f\n", trueerr);
The actual error 0.019156
mesh (x, y, reshape (f([x(:), y(:)]), size (x)), 'facecolor', 'None');
Integrate truncated normal distribution to find normalization constant
pdf = @(x) exp (-.5*x.^2)/(pi^.5*2^.5);
nsamples = 1e3;
rand ('seed', 5) # for reproducibility
proprnd = @(x) (rand (size (x)) - .5) * 3 + x;
[smpl, accept] = mhsample (1, nsamples, 'pdf', pdf, 'proprnd', proprnd, ...
'symmetric', true, 'thin', 4);
f = @(x) exp (-.5 * x .^ 2) .* (x >= -2 & x <= 2);
x = linspace (-3, 3, 1000);
area (x, f(x));
xlabel ('x');
ylabel ('f(x)');
int = mean (f(smpl) ./ pdf (smpl));
errest = std (f(smpl) ./ pdf (smpl)) / nsamples^ .5;
trueerr = abs (erf (2 ^ .5) * 2 ^ .5 * pi ^ .5 - int);
printf ("Monte Carlo integral estimate int f(x) dx = %f\n", int);
Monte Carlo integral estimate int f(x) dx = 2.346204
printf ("Monte Carlo integral error estimate %f\n", errest);
Monte Carlo integral error estimate 0.019410
printf ("The actual error %f\n", trueerr);
The actual error 0.046372