Come for the quick hacks, stay for the epiphanies. | |
PerlMonks |
Re: Sample Probabilitiesby blokhead (Monsignor) |
on Dec 07, 2006 at 18:15 UTC ( [id://588420]=note: print w/replies, xml ) | Need Help?? |
Just to be sure I understand you want ... You have 35 red widgets, 41 green widgets, 16 blue widgets, and you want to know the probability of getting 2 red, 1 green, 0 blue, if you were chosing 3 items randomly from all widgets? Also, the order in which they were drawn is irrelevant (drawing green,red,red is the same as red,green,red), and you are drawing without replacement.
Continuing with that example, the probability of getting green,red,red, in that order, is: All we have to do now is multiply this quantity by the number of ways to rearrange (green,red,red). In this case, there are 3 ways to rearrange them. In general, the probability of getting x1 red, x2 green, x3 blue, etc, in some particular order, is the following: (I separated out the numerator and denominator to make it easier) And the number of ways of reordering a list of x1 reds, x2 greens, x3 blues, is Those exclamation points are for factorials. As perl code, and using the example in your original post (I changed the indexing to be 0-based): Disclaimer: This code not tested in any great detail. Also keep in mind that all the probabilities will look small, because there are so many different events in this distribution. Update: updated computation to account for drawing without replacement. Old code is still in comment tags. The probabilities hardly change at all since the domain is so large. Update: fixed copy & paste error, thanks to BrowserUk++. blokhead
In Section
Seekers of Perl Wisdom
|
|