Why is a simulation of a probability experiment off by a factor of 10?

From a university homework assignment:

There are $$8$$ numbered cells and $$12$$ indistinct balls. All $$12$$ balls are randomly divided between all of the $$8$$ cells. What is the probability that there is not a single empty cell ($$i.e.$$ each cell has at least $$1$$ ball)?

The answer is $$\large\frac{\binom{11}{7}}{\binom{19}{7}}$$ which is about $$0.0065$$. I reached this result independently, and it was confirmed by the official homework solution of the university.

A friend of mine and I independently wrote Python simulations that run the experiment many times (tested up to $$1,000,000$$). We used both Pythons' random generator and several randomly generated lists from www.random.org. Results were similar and consistently hovering around $$0.09$$ which is a factor of $$10$$ or even a bit more off from the expected theoretical result.

Have we made some wrong assumptions? Any ideas for this discrepancy?

P.S.: Here is the Python code that I wrote, and maybe there is some faulty logic there.

def run_test():
global count, N

def run_experiment(n_balls, n_cells, offset):
cells = [0] * n_cells
# toss balls randomly to cells:
for j in range(n_balls):
cells[random.randrange(0, n_cells)] += 1
# cells[int(lines[offset + j])] += 1
cells = sorted(cells)
# print(cells)

# check if there is an empty cell. if so return 0, otherwise 1:
if cells[0] == 0:
return 0
return 1

count = 0
N = 1000000
offset = 0
N_CELLS = 8
N_BALLS = 12
# iterate experiment
for i in range(N):
result = run_experiment(N_BALLS, N_CELLS, offset=offset)
count += result
offset += N_CELLS

print("probability:", count, "/", N, "(~", count / N, ")")
• I suppose the issue must be in "randomly divided." What does it mean exactly? What is the distribution of probabilities between different outcomes? Was the same distribution simulated in the experiment and used in the formula? – Alexey Oct 29 '18 at 9:32
• If you suppose that all possible distributions of the numbers of balls (not of balls themselves) between cells have the same probability, I wonder how you could simulate this in an experiment... – Alexey Oct 29 '18 at 9:34
• Besides the distinguishability of the balls is how they are distributed between the cells. You can either assign a cell to each ball, uniformly at random, independently of all the other balls, or you can look at all the different distinguishable ball distributions and you pick one of those uniformly at random. That's the difference between your two approaches. – Arthur Oct 29 '18 at 9:57
• "Have we made some wrong assumptions?" Hard to tell, since you have shown neither the code that you have written nor specified the assumptions that you have made. Without mind-reading, this question is impossible to answer. If your question involves your code, you should show the relevant code in the question itself. – John Coleman Oct 29 '18 at 10:51
• It seems the book solution assumes that all the ways to distribute the balls into the cells are equally likely--for example a distribution of (12,0,0,0,0,0,0) is just as likely as (2,2,2,2,2,2,0,0). This is not consistent with a model that says each ball is dropped into a cell chosen independently and randomly, with each cell equally likely to be chosen, which I think is more realistic. – awkward Oct 29 '18 at 13:30

In reality, you will find it very difficult to put the balls in the cells without distinguishing between the balls, especially if you want equal probabilities so as to use counting methods for simulation. Suppose you wanted to consider the probability all the balls went into the first cell: with distinguishable balls this probability is $$\frac1{8^{12}}$$ and is easily simulated though a rare occurrence; with indistinguishable balls it is $$\frac1{19 \choose 7}$$ over a million times more likely but difficult to simulate

If the balls are distinguishable, the probability all eight boxes are full is $$\frac{8! \, S_2(12,8)}{8^{12}}$$ where $$S_2(n,k)$$ is a Stirling number of the second kind and $$S_2(12,8)=159027$$. That gives a probability that each cell has at least one ball of about $$0.0933$$. Is this similar to your simulation?

If you really want to simulate the indistinguishable balls case, despite it not being realistic physically outside Bose–Einstein condensate at temperatures close to absolute zero, you could use a stars and bars analogy. Choose $$7$$ distinct positions for the cells walls from possible positions $$\{0,1,2,3,\ldots,18\}$$ for the balls and cell walls; a success is when none of the cell walls are at positions $$0$$ or $$18$$ and no pair of them are consecutive

• Nit picking from a physicist: any collection of Bosons will show this effect. It's just that B-E condensates are one of the simplest for us to demonstrate. Messing with , say, photons, is more difficult – Carl Witthoft Oct 29 '18 at 15:28
• I'm clearly missing something here. Why would the probability of them all ending up in the same cell have anything to do with whether you can distinguish between them? Why would being able to tell which ball is which change a ball's chance of ending up in the first cell? – JimmyJames Oct 29 '18 at 20:27
• @JimmyJames: that is way it is physically difficult to get the indistinguishable case. Try a simpler thought experiment: take two indistinguishable balls and two distinguishable cells; what are the possible patterns and their probabilities? – Henry Oct 29 '18 at 20:46
• It's your assertion that this is relevant which I'm not understanding. There's nothing in the question that states or implies that the being able to tell one ball from another changes the probability of landing in the first cell which would seem to be 1/8 assuming each event is independent. Your thought experiment has the same distribution as flipping a fair coin twice. 1/2 one head one tail, 1/4 two heads, 1/4 two tails. If I flip a fair penny and then a fair dime, it has no effect on the distribution of head and tails. – JimmyJames Oct 29 '18 at 20:57
• @JimmyJames You could argue that the question is vague. It says the balls "are randomly divided" but doesn't describe how. Depending on the procedure, one gets different distributions. And the procedures you've described are plausible. Despite all that, I believe it's the convention for textbook problems like this one that all possible patterns are equally probable unless stated otherwise. And the textbook problem specifies "indistinct balls" for the precise purpose of constraining what those possible patterns are. We're meant to follow that, even if we think it's unmotivated and silly. – Chris Culter Oct 29 '18 at 21:52

Consider the set $$D$$ of ways to distribute $$12$$ balls labelled [abcdefghijkl] among $$8$$ cells numbered [01234567]. This set has $$8^{12}\approx7\times10^{10}$$ elements.

Now consider the set $$I$$ of distinguishable ways to populate those same $$8$$ cells [01234567] with $$12$$ indistinct balls. This set has $${19\choose7}\approx 5\times10^4$$ elements.

The assignment asks you to compute a probability of an event over the uniform distribution on $$I$$, if not in so many words. In principle, you could approximate this probability by sampling from the uniform distribution on $$I$$. But your strategy is to sample from the uniform distribution on $$D$$, and then map each sample to $$I$$! That's not the same.

Instead of taking the average of all the results, you need to take a weighted average, such that the weight compensates for the number of elements in $$D$$ that map to the same element of $$I$$. Hint, it's something like this:

weight = 1
for cell_population in cells:
weight *= math.factorial(cell_population)

At least, that gets the right answer. Rigorously justifying that formula as a consequence of the mapping between $$D$$ and $$I$$ is left as an exercise to the reader.

The original problem is posed, so far as I can tell, to show the difference between combinations and permutations. In nature, there is no such thing as indistinguishable balls. Semi-infinite tests (e.g. Las Vegas) have shown this to be true.

Now, if the problem really wants you to use "indistinguishable" balls for the purposes of solving the problem, then yes, you need to use combinations and not permutations when calculating all the ways the indistinguishable balls are placed into the containers. And of course, you need to use permutations for the numbered balls, as they are distinguishable from each other and from the collection of indistinguishable balls.

Now, I believe that Chris Culter's calculations reflect this difference. Whether your Python code does this correctly we can't say until we see the code.

• pastebin.com/5mKBxfbM – Shmuel Levinson Oct 29 '18 at 18:24
• If we want to show the difference between combinations and permutations, we can ask for the number of ways to distribute $12$ indistinct balls among $8$ numbered cells; then ask for the number of ways to distribute the same balls in the same cells so that no cell is empty. This has the solver do all the calculations in the "solved" problem except for the final division, without requiring any assumptions about how the word "indistinct" affects probabilities. – David K Nov 5 '18 at 0:02