Generating random number from a pdf

6 views (last 30 days)
Symeon Mattes
Symeon Mattes on 8 May 2014
Edited: Symeon Mattes on 12 May 2014
Hi all,
I have bivariate probability density function f(x,y), which it doesn't follow any pdf from the known pdfs, and I would like to generate x, y random numbers that follow this pdf. Is there any way to do that in matlab?
Thanks in advance

Answers (1)

SK
SK on 8 May 2014
Edited: SK on 8 May 2014
For simplicity, assume that the distribution has bounded support, i.e:
There is some Mx and My such that f(x,y) is zero for abs(x) > Mx and abs(y) > My.
One could probably do without this condition but it would a little more involved and in practice (unless you have a heavy-tailed distribution) it wont make much difference.
With this assumption, a general way to do it for any f(x,y) is as follows:
Let Mz = max(f(x,y)) over all x and y.
Now use rand to choose 3 numbers uniformly in the Box (-Mx < x < Mx) x (-My < y < My) x (z < Mz):
x = 2*Mx*(rand-0.5);
y = 2*My*(rand-0.5);
z = Mz*rand;
if z > f(x,y), reject x and y
otherwise add (x,y) to your set of generated points.
Best Regards,
Sandeep
  1 Comment
Symeon Mattes
Symeon Mattes on 11 May 2014
Edited: Symeon Mattes on 12 May 2014
Thanks SK for your reply. This seem to work but because I have a huge amount of data and the grid is very dense is there any analytical method such as in 1D case, e.g. Using a sample PDF to generate random numbers ?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!