Histogram a 2D PDF for Sampling

**HISTOGRAM_PDF_2D_SAMPLE**,
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.

- discrete_cdf_to_xy.m, the source code.
- r8mat_write.m, writes an R8MAT to a file.
- r8vec_linspace.m, creates a vector of linearly spaced values.
- r8vec_linspace2.m, creates a vector of linearly spaced values.
- r8vec_linspace3.m, creates a vector of linearly spaced values.
- r8vec_uniform_01.m, returns a unit pseudorandom R8VEC.
- set_discrete_cdf.m, sets a CDF from a discrete PDF.
- timestamp.m, prints the current YMDHMS date as a timestamp.