http://www.mathworks.com/matlabcentral/newsreader/view_thread/244756
MATLAB Central Newsreader  Samples from a distribution
Feed for thread: Samples from a distribution
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Wed, 18 Feb 2009 02:15:06 +0000
Samples from a distribution
http://www.mathworks.com/matlabcentral/newsreader/view_thread/244756#629026
Stas Berlyand
Say that I have some probability distribution function. <br>
How can I have matlab pick random samples from that PDF.<br>
<br>
Thanks!

Wed, 18 Feb 2009 05:43:01 +0000
Re: Samples from a distribution
http://www.mathworks.com/matlabcentral/newsreader/view_thread/244756#629058
Roger Stafford
"Stas Berlyand" <ssb338@nyu.edu> wrote in message <gnfqva$6b0$1@fred.mathworks.com>...<br>
> Say that I have some probability distribution function. <br>
> How can I have matlab pick random samples from that PDF.<br>
> .....<br>
<br>
There are many ways to do this. I'll just mention one which utilizes matlab's 'rand' function and the inverse of the desired cumulative distribution function (cdf). With F the desired cdf, let G be its inverse function and use it in the following way:<br>
<br>
r = rand(n,1);<br>
y = G(r);<br>
<br>
This will give a vector y of n samples with the desired distribution.<br>
<br>
The reason for that is this. Since r = rand is uniformly distributed on the interval [0,1], it is true that P{r<=t} = t for any 0<=t<=1. Also we have that y = G(r) implies that r = F(y). Therefore, substituting F(x) for t, we have<br>
<br>
P{y<=x} = P{F(y)<=F(x)} = P{r<=F(x)} = F(x)<br>
<br>
which shows that y will have the desired cdf, namely F.<br>
<br>
You can experiment with it to generate a standard normal distribution by using matlab's 'erfinv' function which can be converted to the inverse of the cdf for a standard normal distribution. You can compare the result with the performance of 'randn' which accomplishes the same thing.<br>
<br>
This all depends on being able to solve for the inverse of a desired cdf. This is not always easily done however, so there are many other methods used in generating random variables. Also note that MathWorks' Statistics Toolbox is able to generate random variables with a great many kinds of distribution.<br>
<br>
Roger Stafford

Wed, 18 Feb 2009 09:22:01 +0000
Re: Samples from a distribution
http://www.mathworks.com/matlabcentral/newsreader/view_thread/244756#629092
Jos
"Stas Berlyand" <ssb338@nyu.edu> wrote in message <gnfqva$6b0$1@fred.mathworks.com>...<br>
> Say that I have some probability distribution function. <br>
> How can I have matlab pick random samples from that PDF.<br>
> <br>
> Thanks!<br>
<br>
>> set(0,'advertisement_mode','on')<br>
<br>
If you do not know the underlying function, but only the distribution of that function, you may find RANDP useful:<br>
<a href="http://www.mathworks.com/matlabcentral/fileexchange/8891">http://www.mathworks.com/matlabcentral/fileexchange/8891</a><br>
<br>
You might also be interested in another of my snippets:<br>
<a href="http://www.mathworks.com/matlabcentral/fileexchange/21700">http://www.mathworks.com/matlabcentral/fileexchange/21700</a><br>
<br>
>> set(0,'advertisement_mode','off')<br>
<br>
hth<br>
Jos

Wed, 18 Feb 2009 14:12:08 +0000
Re: Samples from a distribution
http://www.mathworks.com/matlabcentral/newsreader/view_thread/244756#629146
Tom Lane
> Say that I have some probability distribution function.<br>
> How can I have matlab pick random samples from that PDF.<br>
<br>
Stas, you got some useful information from other posters. I'll just add <br>
that if you have the Statistics Toolbox, and you only have the pdf of the <br>
desired distribution, then you may want to look at the slicesample function.<br>
<br>
 Tom

Mon, 20 May 2013 17:20:10 +0000
Re: Samples from a distribution
http://www.mathworks.com/matlabcentral/newsreader/view_thread/244756#904681
Pantelis Sopasakis
It depends how this PDF is represented. For instance, if it is the normal distribution, then you can use MATLAB's function randn. If it's the uniform, you may use rand. If it is some other distribution, like the chisqured, it is sometimes possible to map from the normal, i.e., if f(x) if the pdf of N(0,1) and g(x) is the pdf of your distribution G, you may find a T so that g(x)=T(f(x)), then if X~N(0,1), T(X)~G.<br>
<br>
If you have some arbitrary distribution for which you know its histogram (approximate PDF), you can use this implementation: <a href="http://www.mathworks.com/matlabcentral/fileexchange/41689pdfsampler">http://www.mathworks.com/matlabcentral/fileexchange/41689pdfsampler</a>

Thu, 17 Oct 2013 12:49:39 +0000
Re: Samples from a distribution
http://www.mathworks.com/matlabcentral/newsreader/view_thread/244756#912735
Daniella
The Matlab function random does the job:<br>
<a href="http://www.mathworks.com/help/stats/random.html">http://www.mathworks.com/help/stats/random.html</a><br>
at least, for a whole bunch of distributions.

Mon, 05 Jan 2015 14:26:11 +0000
Re: Samples from a distribution
http://www.mathworks.com/matlabcentral/newsreader/view_thread/244756#930202
Kevin
This function offers a nice solution:<br>
<a href="http://www.mathworks.com/matlabcentral/fileexchange/21912samplingfromadiscretedistribution">http://www.mathworks.com/matlabcentral/fileexchange/21912samplingfromadiscretedistribution</a>