Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Samples from a distribution

Subject: Samples from a distribution

From: Stas Berlyand

Date: 18 Feb, 2009 02:15:06

Message: 1 of 6

Say that I have some probability distribution function.
How can I have matlab pick random samples from that PDF.

Thanks!

Subject: Samples from a distribution

From: Roger Stafford

Date: 18 Feb, 2009 05:43:01

Message: 2 of 6

"Stas Berlyand" <ssb338@nyu.edu> wrote in message <gnfqva$6b0$1@fred.mathworks.com>...
> Say that I have some probability distribution function.
> How can I have matlab pick random samples from that PDF.
> .....

  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:

 r = rand(n,1);
 y = G(r);

This will give a vector y of n samples with the desired distribution.

  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

 P{y<=x} = P{F(y)<=F(x)} = P{r<=F(x)} = F(x)

which shows that y will have the desired cdf, namely F.

  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.

  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.

Roger Stafford

Subject: Samples from a distribution

From: Jos

Date: 18 Feb, 2009 09:22:01

Message: 3 of 6

"Stas Berlyand" <ssb338@nyu.edu> wrote in message <gnfqva$6b0$1@fred.mathworks.com>...
> Say that I have some probability distribution function.
> How can I have matlab pick random samples from that PDF.
>
> Thanks!

>> set(0,'advertisement_mode','on')

If you do not know the underlying function, but only the distribution of that function, you may find RANDP useful:
http://www.mathworks.com/matlabcentral/fileexchange/8891

You might also be interested in another of my snippets:
http://www.mathworks.com/matlabcentral/fileexchange/21700

>> set(0,'advertisement_mode','off')

hth
Jos

Subject: Samples from a distribution

From: Tom Lane

Date: 18 Feb, 2009 14:12:08

Message: 4 of 6

> Say that I have some probability distribution function.
> How can I have matlab pick random samples from that PDF.

Stas, you got some useful information from other posters. I'll just add
that if you have the Statistics Toolbox, and you only have the pdf of the
desired distribution, then you may want to look at the slicesample function.

-- Tom

Subject: Samples from a distribution

From: Pantelis Sopasakis

Date: 20 May, 2013 17:20:10

Message: 5 of 6

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 chi-squred, 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.

If you have some arbitrary distribution for which you know its histogram (approximate PDF), you can use this implementation: http://www.mathworks.com/matlabcentral/fileexchange/41689-pdfsampler

Subject: Samples from a distribution

From: Daniella

Date: 17 Oct, 2013 12:49:39

Message: 6 of 6

The Matlab function random does the job:
http://www.mathworks.com/help/stats/random.html
at least, for a whole bunch of distributions.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us