**histogram_pdf_2d_sample**,
a MATLAB code 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 code 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 code which evaluates a finite element function defined on an order 3 or order 6 triangulation.

histogram_data_2d_sample, a MATLAB code 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 code 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 code which evaluates and inverts a number of probabilistic distributions.

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

rejection_sample, a MATLAB code which demonstrates acceptance/rejection sampling.

walker_sample, a MATLAB code 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.
- set_discrete_cdf.m, sets a CDF from a discrete PDF.