No BSD License  

4.71429

4.7 | 7 ratings Rate this file 9 Downloads (last 30 days) File Size: 902 Bytes File ID: #6506

Obs. from Arbitrary PDF

by

 

08 Dec 2004 (Updated )

Generate a sample from an arbitrary PDF.

| Watch this File

File Information
Description

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.

Acknowledgements

This file inspired Many Observations From Arbitrary Pdf.

MATLAB release MATLAB 5.2 (R10)
Other requirements I alluded to some tools in other toolboxes in the comments above, but they are NOT required to use this function.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (9)
27 Oct 2013 mutah

GOOD CODE BUT WHEN THE N IS LARGE THE EXCUTE VERY SLOW ?? HOW CAN FAST THIS EXECUTION

21 Apr 2013 Jack Peterson

Very useful! I modified this a bit to generate many random numbers more efficiently -- in case this would be helpful to anyone else, the modified file is at: https://github.com/tensorjack/randarbmulti/blob/master/randarbmulti.m

16 Oct 2009 Seth  
18 Nov 2008 Ahmed Fasih

Valuable code to have.

Code can be made faster by vectorizing the while loop, and also to return multiple draws from the distribution. Just make sure to validate your modifications with a non-symmetric distribution like a beta.

This technique of random number generation is discussed here: http://en.wikipedia.org/wiki/Inverse_transform_sampling

17 Nov 2008 Alex Lu

Thank you very much .for your excellent work, good luck

07 Nov 2008 Ameya Deoras

This method is called CDF inversion - Generate a standard uniform random number and apply the inverse CDF of the distribution D to that number to obtain a new random number drawn from distribution D. This is available in MATLAB for a number of densities: eg: expinv, binoinv etc..
http://www.mathworks.com/access/helpdesk/help/toolbox/stats/bqttfc1.html#bqucf5l-2

09 Feb 2007 ibrahim karauz  
30 Dec 2004 Hong Zhang

Extrmely simply yet efficient.

14 Dec 2004 Craig Chippindale

Exactly what I was looking for.

Contact us