help slicesample
slicesample Slice sampling method.
RND = slicesample(INITIAL,NSAMPLES,'pdf',PDF) draws NSAMPLES random
samples from a target distribution with the density function PDF using
the slice sampling. INITIAL is a row vector or scalar containing the
initial value of the random sample sequences. INITIAL must be within
the domain of the target distribution. NSAMPLES is the number of
samples to be generated. PDF is a function handle created using @. PDF
takes only one argument as an input and this argument has the same type
and size as INITIAL. It defines a function that is proportional to the
target density function. If log density function is preferred, 'pdf'
can be replaced with 'logpdf'. The log density function is not
necessarily normalized, either.
RND = slicesample(...,'width',W) performs slice sampling for the target
distribution with a typical width W. W is a scalar or vector. If it is
a scalar, all dimensions are assumed to have the same typical widths.
If it is a vector, each element of the vector is the typical width of
the marginal target distribution in that dimension. The default value
of W is 10.
RND = slicesample(...,'burnin',K) generates random samples with values
between the starting point and the K-th point omitted in the generated
sequence, but keep points after that. K is a non-negative integer. The
default value of K is 0.
RND = slicesample(...,'thin',M) generates random samples with M-1 out of
M values omitted in the generated sequence. M is a positive integer.
The default value of M is 1.
[RND, NEVAL] = slicesample(...) also returns NEVAL as the averaged
number of function evaluations occurred in the slice sampling. NEVAL is
a scalar.
Example:
Generate random samples from a distribution with a user-defined pdf:
f = @(x) exp( -x.^2/2).* ... % This is a function proportional
(1+(sin(3*x)).^2).* ... % to the pdf for a multi-modal
(1+(cos(5*x).^2)); % distribution
x = slicesample(1,2000,'pdf',f,'thin',5,'burnin',1000);
hist(x,50)
set(get(gca,'child'),'facecolor',[0.6 .6 .6]);
hold on
xd = get(gca,'XLim'); % Gets the x-data of the bins.
binwidth = (xd(2)-xd(1)); % Finds the width of each bin.
y = 5.6398*binwidth* ... % Normalization necessary to
f(linspace(xd(1),xd(2),1000)); % overplot the histogram.
plot(linspace(xd(1),xd(2),1000),y,'r')
See also: mhsample, rand, hist, plot.
Reference page in Help browser
doc slicesample