Products & Services Industries Academia Support User Community Company

Learn more about Statistics Toolbox   

slicesample - Slice sampler

Syntax

rnd = slicesample(initial,nsamples,'pdf',pdf)
rnd = slicesample(...,'width',w)
rnd = slicesample(...,'burnin',k)
rnd = slicesample(...,'thin',m)
[rnd,neval] = slicesample(...)

Description

rnd = slicesample(initial,nsamples,'pdf',pdf) generates nsamples random samples from a target distribution whose density function is defined by pdf using the slice sampling method. 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 accepts only one argument that must be the same type and size as initial. It defines a function that is proportional to the target density function. If the log density function is preferred, 'pdf' can be replaced with 'logpdf'. The log density function is not necessarily normalized.

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 kth point omitted in the generated sequence. Values beyond the kth point are kept. k is a nonnegative integer with default value of 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 with default value of 1.

[rnd,neval] = slicesample(...) also returns neval, the averaged number of function evaluations that occurred in the slice sampling. neval is a scalar.

Examples

Generate random samples from a distribution with a user-defined pdf.

First, define the function that is proportional to the pdf for a multi-modal distribution.

f = @(x) exp( -x.^2/2).*(1+(sin(3*x)).^2).* ...
    (1+(cos(5*x).^2));        

Next, use the slicesample function to generate the random samples for the function defined above.

x = slicesample(1,2000,'pdf',f,'thin',5,'burnin',1000);  

Now, plot a histogram of the random samples generated.

hist(x,50)
set(get(gca,'child'),'facecolor',[0.8 .8 1]);
hold on 
xd = get(gca,'XLim'); % Gets the xdata of the bins
binwidth = (xd(2)-xd(1)); % Finds the width of each bin
% Use linspace to normalize the histogram
y = 5.6398*binwidth*f(linspace(xd(1),xd(2),1000));
plot(linspace(xd(1),xd(2),1000),y,'r','LineWidth',2)

See Also

rand, mhsample, randsample

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS