# HISTOGRAM_DATA_2D_SAMPLE Create and Sample a PDF Based on Sample 2D Data

HISTOGRAM_DATA_2D_SAMPLE is a FORTRAN90 program which demonstrates how to construct a Probability Density Function (PDF) from a table of sample data over a 2D region, and then to use that PDF to create new samples.

The program presented here is hard-wired to handle a specific problem. However, the ideas used in the program are easily extended to other regions and other dimensions.

For the problem given here, we assume we have sample values of a function F(X,Y) for each subregion of a region. These values might actually represent population counts, a density, the integral of some function over the subregion, or simply an abstract function. We implicitly assumed that all the values are positive.

The particular region studied here is the unit square, which has been broken down into a 20x20 array of equal subsquares.

If we normalize by the sum of the data values, the result is a PDF associated with each subregion. By assigning an arbitrary order to the subregions, we can add the PDF values up to the given subregion to get a CDF (cumulative density function) for that subregion. Now given an arbitrary random value U, we can locate the subregion whose CDF value just exceeds U. Choosing a random point within this subregion gives us the sample point. If we choose many such sample points, the statistics for this sample will tend to the discrete PDF that we defined from the data we were given.

### Usage:

histogram_data_2d_sample n
where
• n is the number of sample points desired;

### Languages:

HISTOGRAM_DATA_2D_SAMPLE is available in a C version and a C++ version and a FORTRAN90 version and a MATLAB version.

### Related Data and Programs:

FEM1D_SAMPLE, a FORTRAN90 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 FORTRAN90 program which evaluates a finite element function defined on an order 3 or order 6 triangulation.

IEEE_UNIFORM_SAMPLE, a FORTRAN90 library which tries to uniformly sample the discrete set of values that represent the legal IEEE real numbers;

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

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

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

### Examples and Tests:

TEST01 computes 1000 samples, based on a 20x20 PDF table that is heavily biased toward the northwest corner.

TEST02 computes 1000 samples, based on a 12x8 PDF table that is loosely based on the population of counties in Iowa.

### List of Routines:

• MAIN is the main program for HISTOGRAM_DATA_2D_SAMPLE.
• TEST01 looks at a 20x20 region.
• TEST02 looks at a 12x8 region.
• DISCRETE_CDF_TO_XY finds XY points corresponding to discrete CDF values.
• GET_DISCRETE_PDF_DATA1 returns the discrete PDF data array.
• GET_DISCRETE_PDF_DATA2 returns the discrete PDF data array.
• GET_DISCRETE_PDF_SIZE1 returns the dimension of the discrete PDF data
• GET_DISCRETE_PDF_SIZE2 returns the dimension of the discrete PDF data
• GET_UNIT returns a free FORTRAN unit number.
• R8MAT_WRITE writes an R8MAT file.
• R8VEC_UNIFORM_01 returns a unit pseudorandom R8VEC.
• SET_DISCRETE_CDF sets a CDF from a discrete PDF.
• S_TO_I4 reads an integer value from a string.
• TIMESTAMP prints the current YMDHMS date as a time stamp.

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