Sampling from a PDF histogram

HISTOGRAM_PDF_SAMPLE is 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.

We would prefer to compute the CDF exactly by integration of the PDF, then invert the formula for the CDF to get a formula for random samples. But often the CDF is not invertible, and often it's not possible to get an exact formula for the CDF in any case. For such problems, the histogram approach may provide a relatively straightforward solution.


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


HISTOGRAM_PDF_SAMPLE is available in a Matlab version.

Related Data and Programs:

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 of a 2D region.

FEM3D_SAMPLE, a MATLAB program which evaluates a finite element function defined on 3D tetrahedral mesh.

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.

PDFLIB, a MATLAB library which evaluates Probability Density Functions (PDF's) and produces random samples from them, including beta, binomial, chi, exponential, gamma, inverse chi, inverse gamma, multinomial, normal, scaled inverse chi, and uniform.

PROB, a MATLAB library which evaluates, samples, inverts, and characterizes a number of Probability Density Functions (PDF's) and Cumulative Density Functions (CDF's), including anglit, arcsin, benford, birthday, bernoulli, beta_binomial, beta, binomial, bradford, burr, cardiod, cauchy, chi, chi squared, circular, cosine, deranged, dipole, dirichlet mixture, discrete, empirical, english sentence and word length, error, exponential, extreme values, f, fisk, folded normal, frechet, gamma, generalized logistic, geometric, gompertz, gumbel, half normal, hypergeometric, inverse gaussian, laplace, levy, logistic, log normal, log series, log uniform, lorentz, maxwell, multinomial, nakagami, negative binomial, normal, pareto, planck, poisson, power, quasigeometric, rayleigh, reciprocal, runs, sech, semicircular, student t, triangle, uniform, von mises, weibull, zipf.

RANLIB, a MATLAB library which produces random samples from Probability Density Functions (PDF's), including Beta, Chi-square Exponential, F, Gamma, Multivariate normal, Noncentral chi-square, Noncentral F, Univariate normal, random permutations, Real uniform, Binomial, Negative Binomial, Multinomial, Poisson and Integer uniform, by Barry Brown and James Lovato.

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.

Source Code:

CHEBYSHEV1 is a test case for the PDF rho(x)=1/sqrt(1-x*x)/pi for x between -1 and +1, which is infinite at the endpoints.

CHEBYSHEV13 is a test case for the PDF rho(x)=(1-x*x)^(-1/6) for x between -1 and +1, which is infinite at the endpoints. (In order to integrate to 1 over [-1,+1], multiply by the factor gamma(4/3)/sqrt(pi)/gamma(5.6).)

CHEBYSHEV2 is a test case for the PDF rho(x)=2*sqrt(1-x*x)/pi for x between -1 and +1.

Examples and Tests:

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

Last revised on 11 August 2016.