MATLAB: How to generate numbers from discrete bivariate distribution

bivariatediscretedistributionsamplingStatistics and Machine Learning Toolbox

I would like to know how I can generate sample numbers from discrete bivariate distribution assuming that I have vectors x and y and a discrete probability matrix P to describe the two variables.

Best Answer

If P is an unconditional probability matrix and sum of all entries equal to '1', where P(i, j) describe the probability choice (xi, yj), then the RANDSAMPLE function can be used to choose the pair (x,y) using probability matrix P. For example,
P = [0.083333 0.05 0.033333
0.1 0.16667 0.066667
0.1 0.1 0.3];
sum_P = sum(P(:))
x = [1 2 3]';
y = [1 2 3]';
xy = [x repmat(y(1), length(x),1); x repmat(y(2), length(x),1); x repmat(y(3), length(x),1)];
%%generate random row number of (x,y) pair
row = randsample(9,10,true, P(:) );
xy_sample = xy(rows,:);
sum_P =
1
xy_sample =
2 2
3 2
3 1
3 2
1 1
3 2
3 3
3 3
3 3
3 3