I have two lists, where each time I have age-income vectors. Given the distribution of age in the first list, I would like to draw a sample with the similar distribution of age from the second list (and then compare ultimately distributions of incomes in list 1 and in the drawn sample).

While I can easily get empirical distribution of age from the list 1, how can I draw a similar distribution from the 2nd given list of age values?

Thanks a lot!

  • $\begingroup$ A couple of data samples and maybe initial attempts would improve the question. $\endgroup$
    – gwr
    Commented Jun 21, 2015 at 20:25

1 Answer 1


Let's generate two dummy samples of data of size 100 that match the form you have given:

sample1 = { 
   RandomInteger[ {20, 60}, 100], (* age *)
   RandomReal[ {1000, 5000}, 100] (* income *)
} // Transpose; (* make tuples *)

sample2 = {
   RandomInteger[ {30, 50}, 100], (* age *)
   RandomReal[ {3000, 7000}, 100] (* income *)
} // Transpose; (* make tuples *)

Now we extract the ages from group1 and generate a smooth Kernel distribution for it:

ageDistribution1 = KernelMixtureDistribution @ sample1[[All, 1]]

This can be used to caculate sampling weights. The weights simple are the probability density for any age of the second group using the Smooth Kernel Distribution according to the first groups data:

weights = 10.^3 PDF[ ageDistribution1, #] & /@ sample2[[All, 1]];
(* the weights are scaled which helps numerics I believe *)

Now one can simply sample from the 2nd group using the age distribution of the first group:

sample = With[ 
   { sampleSize = 10 }, (* or another value less than group size *)
   RandomSample[ weights -> sample2, sampleSize ]
  • $\begingroup$ FANTASTIC!! Thanks a lot!! I did not know how to make steps "weights" and "sample" (I am quite new to Mathematica)... If you only saw pages of codes through which I tried to implement this. Thank you very much!! $\endgroup$
    – Kass
    Commented Jun 21, 2015 at 19:53
  • $\begingroup$ Great if that helped. Btw you may also upvote...hint,hint. ;-) $\endgroup$
    – gwr
    Commented Jun 21, 2015 at 19:54
  • 1
    $\begingroup$ Rhh I would really love to do this, upvote, but it tells me that I need 15 reputations and apparently I don't have them. Guys around -- please upvote this answer!! $\endgroup$
    – Kass
    Commented Jun 21, 2015 at 19:56
  • $\begingroup$ @gwr I like you answer! $\endgroup$
    – Bob Brooks
    Commented Dec 2, 2016 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.