Probability question (in need of code)
August 13, 2008 10:04 PM
Subscribe
It's late, I'm tired, and I have a probability and coding question that's fairly simple.
Say I have 5 buckets (A,B,C,D,E), with different colored balls in them. The probability of removing a red ball is different for each bucket:
A: 1/3
B: 1/4
C: 1/16
D: 1/6
E: 1/9
If I draw 1 ball from each bucket, what is the probability (P) of drawing at least N red balls?
Doing it for small numbers of buckets is easy enough, but I have thousands of buckets here. Given that I know the probabilities for each bucket, what's the easiest way to calculate P? I suspect that there's an R function that makes this a breeze, but I'm having trouble tracking it down.
I can do basic operations in R, and I'm also open to functions or pseudo code from your favorite language. Ruby and Perl preferred, but I can use others if they get the job done.
For good measure, here's a preemptive "This isn't homework-filter"
posted by chrisamiller to computers & internet (10 comments total)
1 user marked this as a favorite
In English: The probability of N red balls is equal to the probability of 1 red ball from the first bucket and N-1 from the rest, plus the probability of 0 red balls from the first bucket and N from the rest.
I think this function is O(2^N), and it's recursive, so you might need to optimize it somewhat if you have literally thousands of buckets.
posted by mbrubeck at 10:28 PM on August 13, 2008