views:

210

answers:

3

I have x items and y <= x special items. Out of these I pick z <= x items. Given s where 1 <= s <= y, what is the probability that the z items I picked contain s special items?

The particular case I want to apply it to: I have 70 items, 14 are special. I pick 5 of them. What's the chance that 1 is special, 2 are special, etc... up to 5.

Update: If you're feeling up to it, see a more general problem here.

+3  A: 

Let's take your example:

   Special    14
   Ordinary   56

Now, you can choose (assuming that once an object has been chosen, it cannot be reused for at a later time):

   1 special item in 14 different ways -- there are 14 to choose from,
   and the remaining (5 - 1) = 4 items from the 56 ordinary ones
        the first ordinary item has = 56 choices
        the second ...              = 55 choices
        ...
      This gives a total of 56 * 55 * 54 * 53 choices.

   Since these 4 ordinary elements can be arranged differently, we have some 
   duplicates in the above, exactly 4! number of duplicates.

   So uniques choices are (56 * 55 * 54 * 53) / 4!

   Total number of possible choices is simply 14 * (56 * 55 * 54 * 53) / 4!

Read up on combinations. For brownie points, think what happens if you could reuse an object chosen earlier (combination with replacement).

dirkgently
nice explanation!
Claudiu
+5  A: 

That's an Hypergeometric distribution. You can find its probability distribution here: http://en.wikipedia.org/wiki/Hypergeometric_distribution

  • N is total: 70
  • m is the number of special items: 14
  • n is the number of picked items: 5
  • k is the number of special items picked, so you are looking for:

f(k=1), f(k=2), etc.

Gerardo
+2  A: 

Run a simulation ;)

class Program
{
    static Random rnd = new Random();
    static double SimulateProbability(int totalItems, int specialItems, int items, int matchedSpecialItems)
    {
        const int testCount = 1000000;
        return Enumerable.Range(0, testCount).Count(dummy2 => Enumerable.Range(0, items).Count(dummy => rnd.Next(totalItems) < specialItems) == matchedSpecialItems) / (double)testCount;
    }

    static void Main(string[] args)
    {
        for (int i = 1; i <= 5; i++)
            Console.WriteLine("{0} special items: {1:F} percent", i, SimulateProbability(70, 14, 5, i) * 100);
    }
}

Output:

1 special items: 40.97 percent
2 special items: 20.47 percent
3 special items: 5.12 percent
4 special items: 0.64 percent
5 special items: 0.03 percent
Mehrdad Afshari
I love it. Yours would be the easiest way to answer my updated question, too.
Claudiu