MATLAB code to sample arbitrary Lipschitz continuous densities on the interval.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


MATLAB code to generate random variates following arbitrary Lipschitz continuous densities on the interval.


Sample from the normal distribution (Lipschitz constant 0.25) restricted to [-5, 5].

sample = lipsample(@normpdf, 0.25, [-5 5], 10000000);

Plot the result.

pretty_hist(sample, [-5 5]);

Usage and functionalities

sample = lipsample(@f, L, [a b], m); Draws a sample of size m from the probability density function f which is Lipschitz continuous of order L on [a,b].


The function f does not have to be a normalized density, but it should be scaled to be near 1 as to improve efficiency. L is the Lipschitz constant of f, not of its renormalization.

Below you can see the envelope that was automatically constructed for the acceptance-rejection sampling of a triangular density (lipsample(@triangular, 4, [0 1], 1000000, 'N', 9);).