How to sample from custom 2D distribution?

51 views (last 30 days)
I have a joint density function for to independent variables X and Y. (See: And I now want to sample new x,y from this distribution.
What I believe I have to do is to find the joint cumulative distribution and then somehow sample from it. I kinda know how to do this in 1D, but I find it really hard to understand how to do it in 2D.
I also used the matlab-function cumtrapz to find the cumulative distribution function for the above pdf. (See:
Just to be clear, what i want to do is to sample random values x,y from this empirical distribution.
Can someone please point me in the right direction here?!

Accepted Answer

Razvan on 19 Apr 2012
Hussein Ammar
Hussein Ammar on 2 Jul 2019
Thank you for mentioning this function. It helped me a lot.

Sign in to comment.

More Answers (2)

Richard Brown
Richard Brown on 19 Apr 2012
A really basic, quick to code (but darned inefficient way) is to generate uniform samples in the 3D volume defined by the x and y coordinates and the maximum z coordinate. Accept only samples that fall beneath the surface. The x,y coordinates of these samples will have the distribution you want.

Sign in to comment.

Tom Lane
Tom Lane on 19 Apr 2012
There is a Statistics Toolbox function "slicesample" that could be useful. It does not generate independent samples from the distribution, but instead generates a Markov Chain such that a long sequence of values will have a distribution close to the target distribution. To use this, you would need to be able to write down an expression for the density.

Community Treasure Hunt

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

Start Hunting!