%RANGRID(grid,density) is a function which takes a matched pair of VECTORS with a grid of values and corresponding densities and returns a draw from the density approximated by linear interpolation between the grid points. This is a last resort for doing a draw from a non-standard (univariate) distribution when another method such as the rejection method isn't available.



VECTOR of grid values. These need to be increasing, but do not have to be equally spaced.


VECTOR of corresponding density values to the elements in grid. These do not have to integrate to one, even approximately, as the density is (in effect) divided through by the integral. (If the distribution is non-standard, the integrating constant is generally not a known function).


This does 10000 draws from an approximated density for

\(p(x) \propto x^2 (1 - x)^3 \cos (x)\)

on the unit interval.

compute grid=%seqrange(0.0,1.0,1001)

dec vect density(%size(grid))

ewise density(i)=grid(i)^2*(1-grid(i))^3*cos(grid(i))

compute ndraws=10000

set xdraw 1 ndraws = %rangrid(grid,density)