Solved – How to draw random sample from an empirical joint distribution

joint distributionsampling

I want to draw a random sample from a known joint distribution. what I have now is the number of people in each age and income bracket (as shown in the example below):0.12 0.42 0.06
16 0.08 0.28 0.04

                   income $1-10,000    income $10,001-20,000
15-20 years old      0.12                      0.08
21-25 years old      0.42                      0.28
26-30 years old      0.06                      0.04

Using this I will get the joint distribution of income and age. But I am not sure where to go from here in terms of drawing samples.

Any thoughts are appreciated. My knowledge in statistics is quite limited…

Best Answer

If you want to sample (Age,Income) pairs, use any old sampling method directly on your joint distribution. Using numpy:

dist = [(("15-20 yr", "$1-10000"), 0.12), (("15-20 yr", "$10000-20000"), 0.08), ...]
num_of_samples = 10 # choose any size you want here
samples_i = numpy.random.choice(len(dist), size=num_of_samples, p=[x[1] for x in dist])
samples = [dist[x][0] for x in samples_i]
Related Question