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 MIT license


histogram_pdf_2d_sample is available in a MATLAB version.

Related Data and Programs:

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.

Source Code:

Last modified on 30 January 2019.