**R – Curtis Miller's Personal Website**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

## Problem

Dave’s Donuts offers 14 flavors of donuts (consider the supply of each flavor as being unlimited). The “grab bag” box consists of flavors randomly selected to be in the box, each flavor equally likely for each one of the dozen donuts. What is the probability that at most three flavors are in the grab bag box of a dozen?

## Solution

For this we will need the multinomial distribution, which is a discrete probability distribution. In a string of characters there are characters possible to fill one position of the string, which is characters long. Therandom variable counts the number of occurrences of character 1 in the string, the number of occurrences of character 2, and so on until . Let be the individual probability each of the characters could appear in a position of the string; each position is filled independently of the characters in other positions. Let such that . Then

Here, , , and . So we can say

We will say

Compute each of those probabilities separately.

If and , there is exactly one flavor in the box.(1) shows the probability this happens is . Since we could pick an and there were 14 ways to make this decision, we can say

Let’s now compute . We start by fixing . We get

Unfortunately (2) includes cases where there’s actually only one flavor present in the box, so compute

(3) | ||

(4) |

Of course we could have picked different variables to fix at zero, andthere were ways to pick the variables to fix at zero (or equivalently, pick the variables to not fix at zero), finally yielding

Now to compute . Again we start by fixing and compute

We could try and use tricks to compute (6) or we can acknowledge that we’re busy people and ask SymPy to do it. Check that the following Python code is correct:

from sympy import init_session, binomial init_session() def multinomial(params): if len(params) == 1: return 1 return binomial(sum(params), params[-1]) * \ multinomial(params[:-1]) l1 = list() for i in range(1, 10 + 1): v = sum([multinomial([i, j, (12 - i - j)]) for j in range(1, 11 - i + 1)]) l1.append(v) sum(l1)/14**12 # Solution

The resulting probability is . We could have picked different flavors to fix, and there were ways to pick the flavors to fix, so we get

We can write (1) and (5) as and , respectively. Summing these probabilities yields

## Related Question

This is the proper way to obtain the probability that there are at most three flavors in the “grab bag” box, but how many boxes exist in which there are at most three flavors when we discount the number of ways there are to arrange the donuts in a box?

If there’s exactly one flavor, then we pick it and fill the box with that flavor; there’s 14 ways to pick one flavor. If there’s exactly two flavors in the box, we’ll call them Flavor 1 and Flavor 2. There is at least one donut of Flavor 1 and one of Flavor 2. Now pick the rest of the donuts’ flavors, order doesn’t matter, there is replacement; there are ways to do that. Then pick the two flavors: there’s ways to do that, and thus boxes with exactly twoflavors. Similarly, for exactly three flavors, there are ways for there to be exactly three flavors. Sum these numbers. (See `https://math.stackexchange.com/q/3230011`.) There are 21,035 such boxes.

Special thanks to Math Stack Exchange user wavex for his help with this problem! He provided the following R script for simulating it:

total = 0 for (y in 1:10000000){ x = rmultinom(1,12,c(1/14,1/14,1/14,1/14,1/14, 1/14,1/14,1/14,1/14,1/14,1/14,1/14,1/14,1/14)) x <- c(x) count = 14 for(i in x){ if(i==0){ count = count -1 } } if(count <= 3){total = total + 1} } sprintf("%.20f", total / 10000000)

In his run of the code this event occured only 27 out of 10,000,000 times. A rare event indeed!

## About this document …

This document was generated using the LaTeX2HTML translator Version 2019 (Released January 1, 2019)

The command line arguments were:

`latex2html -split 0 -nonavigation -lcase_tags -image_type gif Example6Solution.tex`

The translation was initiated on 2019-05-20

Packt Publishing published a book for me entitled *Hands-On Data Analysis with NumPy and Pandas*, a book based on my video course *Unpacking NumPy and Pandas*. This book covers the basics of setting up a Python environment for data analysis with Anaconda, using Jupyter notebooks, and using NumPy and pandas. If you are starting out using Python for data analysis or know someone who is, please consider buying my book or at least spreading the word about it. You can buy the book directly or purchase a subscription to Mapt and read it there.

If you like my blog and would like to support it, spread the word (if not get a copy yourself)!

**leave a comment**for the author, please follow the link and comment on their blog:

**R – Curtis Miller's Personal Website**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.