6

I have a joint density function for two independent variables X and Y. 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.

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?!

EDIT: I have data values and I use [pdf bins] = hist3([N Y])

I then normalize the pdf and do

cumulativeDistribution = cumtrapz(pdfNormalize)

And yes (to the comment below) X,Y are suppose to be independent.

Groot
  • 13,943
  • 6
  • 61
  • 72

2 Answers2

6

If you know how to sample a distribution in 1D then you can extend it to 2D. Create the marginal distribution for X. Take a sample from that, say X1. Then in your 2D distribution fix one variate X=X1 and sample for Y, i.e., sample Y from 1D distribution fXY(X1, Y).

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Mohsen Nosratinia
  • 9,844
  • 1
  • 27
  • 52
  • 1
    This works! :) The same is almost done in this very nice .m-file from the MATLAB file exchange: http://www.mathworks.com/matlabcentral/fileexchange/35797-generate-random-numbers-from-a-2d-discrete-distribution – Groot Apr 19 '12 at 08:05
  • This is correct but would you have a solution when only relying on the cdf? – Xi'an ні війні Jun 17 '22 at 14:24
3

Given a joint distribution in say, two random variable X and Y, you can compute a CDF for X alone by summing over all possible values of Y, i.e. P(X<=x)=Sum[P[X=x_i and Y=y_j],{x_i<=x and all values of y_j}]. Once you have P(X<=x) in hand, there are well-known methods for sampling a value of X, let's call it a. Now that you have a, compute P(Y<=y given X=a)=Sum[P[X=a and Y=y_j],{y_j<=y}]/Sum[P[X=a and Y=y_j],{all values of y_j}]. Sample Y using the same approach that gave you X, yielding Y=b. Done.

The same approach generalizes to more that two random jointly distributed random variable.

  • This answer was very helpful to me! This put me on the right path, and I was able to implement this into a useful sampling method for the Kent distribution. – amicitas Mar 12 '22 at 03:48