Obs. from Arbitrary PDF

Version 1.0.0.0 (599 Bytes) by Dave Dykes
Generate a sample from an arbitrary PDF.
2.4K Downloads
Updated 13 Dec 2004

No License

This program assumes you have created an arbitrary PDF and can represent it via an arbitrarily high number of x,y points.

As an aside, if you only have observations from the PDF, I find the combination of ksdensity and a smoothing spline are a great way to create cfit function that you can then run feval on and then generate the arbitrarily high x,y pairs mentioned above.

Here's the surprisingly simple part: imagine this, take your y (heights) and lay them end-to-end? what you then have is a line composed of ?segments? each of length y(x). Now generate a uniform random number of the range of 0 to the length of the segmented line (e.g. sum(y)*rand()). Now, wherever that uniform random number falls, look down at which y ?segment? you landed on and generate the corresponding x as the output!

I wish I could remember where I read this trick because I just codified it in MATLAB. This seems to be a common problem with an illusively simple and elegant solution.

NB: In general, this technique seems NOT to be perfect as the samples rarely represent the extremes of the original PDF. If there is a sophisticated way to induce some kurtosis to properly account for this, I'd be very interested to learn of it.

Cite As

Dave Dykes (2024). Obs. from Arbitrary PDF (https://www.mathworks.com/matlabcentral/fileexchange/6506-obs-from-arbitrary-pdf), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R10
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Polynomials in Help Center and MATLAB Answers
Acknowledgements

Inspired: Many observations from arbitrary PDF

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0.0