Histogram a 2D PDF for Sampling

**HISTOGRAM_PDF_2D_SAMPLE**
is a MATLAB program which
demonstrates how uniform sampling of a 2D region with respect to some
known Probability Density Function (PDF) can be approximated by
decomposing the region into rectangles, approximating the PDF by
a piecewise constant function, constructing a histogram for the CDF,
and then sampling.

The 2D region is assumed to be a rectangle, because this makes it easy to decompose the region. However, a similar approach could be applied to any 2D region that could be, for instance, triangulated.

The PDF's considered in the examples here are separable, that is

PDF(X,Y) = F(X) * G(Y)but that is simply a common feature of many scientific models, and is by no means necessary.

The purpose of this program is to enable the region to be sampled uniformly with respect to the PDF. In the ideal case, this would be done analytically, that is, by determining a function iCDF(u) which, given a random value u between 0 and 1, would specify a point (X,Y) in the region with a probability corresponding to the PDF. While this is often not difficult for a 1D problem, the task of constructing an inverse CDF in 2D is often impractical.

The approach taken here is simply to use discretization. The region is discretized, into rectangles. The PDF is discretized, replaced by a function which is constant over each rectangle, amounting to a sort of 2D histogram. The CDF can be determined by ordering these rectangles and then forming the running sum of the piecewise constant PDF integrated successively over each rectangle (usually a normalization is necessary so that the CDF runs exactly between 0 and 1). Once the CDF is constructed, it is then easy to do sampling.

The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.

**HISTOGRAM_PDF_2D_SAMPLE** is available in
a MATLAB version.

FEM1D_SAMPLE, a MATLAB program which samples a scalar or vector finite element function of one variable, defined by FEM files, returning interpolated values at the sample points.

FEM2D_SAMPLE, a MATLAB program which evaluates a finite element function defined on an order 3 or order 6 triangulation.

HISTOGRAM_DATA_2D_SAMPLE, a MATLAB program which demonstrates how to construct a Probability Density Function (PDF) from a frequency table over a 2D domain, and then to use that PDF to create new samples.

HISTOGRAM_PDF_SAMPLE, a MATLAB library which demonstrates how sampling can be done by starting with the formula for a PDF, creating a histogram, constructing a histogram for the CDF, and then sampling.

PROB, a MATLAB library which evaluates and inverts a number of probabilistic distributions.

RANDOM_DATA, a MATLAB library which generates sample points for various probability distributions, spatial dimensions, and geometries;

REJECTION_SAMPLE, a MATLAB library which demonstrates acceptance/rejection sampling.

WALKER_SAMPLE, a MATLAB library which efficiently samples a discrete probability vector using Walker sampling.

- histogram_pdf_2d_sample.m, the source code.
- histogram_pdf_2d_sample_output.txt, the output file.
- chebyshev.txt, 1000 samples from [-1,+1]x[-1,+1] with a product Chebyshev PDF, decomposed into a 20x20 nonuniform grid of rectangles whose widths are also governed by the Chebyshev PDF.
- chebyshev.png
- square_sqrt.txt, 1000 samples from [-1,+1]x[0,+1] with the PDF(x,y)=(1-x^2)*sqrt(y), decomposed into a 20x20 uniform grid of rectangles.
- square_sqrt.png

You can go up one level to the MATLAB source codes.