# Probability of drawing two differently colored balls in a partition of equally sized partitions

If I randomly distribute, say, 100 red and 10 blue balls onto a field of 10000 square meters (100*100 m), how would one calculate the chance of having at least one red and at least on blue in the same square meter ?

Update:

1. The balls have infinite small volumen, i.e. they dont take up any area from the field, and hence you can have infinite number of balls in each square meter, in principle.
2. Similar the square grids also do not take up any area (lines in a mathematical sense you could say).
3. Each ball is placed independent of any other ball already placed.
• "in the same square meter" Do you divide the 100m x 100m area into 10 000 squares, or do you mean any possible square of 1m x 1m? Oct 26, 2020 at 11:14
• Good point - It think I mean in a grid, i.e. 10 000 squares. One would think that it - except for limit cases (very few blues e.g.) would come to the same since balls are placed randomly ? Oct 26, 2020 at 17:56
• not quite because if you don't consider squares then balls of same color can land very close ($<1m$) and then it shrinks "success area" for other color, but probably by very low amount, also success area alone will be different because we would count circles with some radius Oct 26, 2020 at 20:14
• Please edit the post to make the meaning clear! Oct 26, 2020 at 20:18
• "It think I mean in a grid" @user3375672 You say 'you think', so you are not sure yourself what the question should be? Could you maybe explain the problem more accurately based on the underlying question/problem/situation/thought that caused you to ask this question? Oct 27, 2020 at 10:19

### Interpretation 1: No red and blue balls within any possible square of 1m x 1m #### plotting situation

### drawing boxes around the blue points
box <- function(x, y , col = "blue") {
polygon(c(x,x,x,x)+c(-1,-1,1,1), c(y,y,y,y)+c(-1,1,1,-1), border = col)
}
box <- Vectorize(box)

set.seed(1)

### plot the 100 x 100 square
plot(-1000,-1000, xlim = c(0,100), ylim = c(0,100), xlab = "", ylab = "",
xaxs = "i",
yaxs = "i")

### blue points with boxes around them
xb <- runif(10,0,100)
yb <- runif(10,0,100)
box(xb,yb, col = "blue")

### red points
points(xb,yb, pch = 21, cex = 0.3, col = "blue", bg = "blue")
points(runif(100,0,100),runif(100,0,100), cex = 0.3, pch = 21, col = "red", bg = "red")


### Approximation

The 10 blue balls will approximately cover 40 square meters of the area within which they will be within the same 1x1 square meter with another ball (this ignores possible overlap with each other and with the edges). Then each red ball will have a 0.004 probability to be within a blue ball.

The probability that at least one of the $$n$$ red balls will be within 1 meter of a blue ball, when the probability for an individual red ball is $$p$$, is

$$1-(1-p)^n \approx 0.3302174$$

This approximation is an overestimation because the probability $$p$$ can be lower than 0.004 due to overlap (but in this particular case it won't be that much).

### Simulation

In the simulation below we simulate the overlap. We do this by sampling the positions of the blue balls from a discrete distribution. The reason that we do this, is because this simplifies the computation of the area. We do this very simply by drawing the area $$\pm 1 \text{ meter}$$ around the blue balls and compute how many pixels we got painted (and some of the pixels might overlap or be outside the field).

In this case, the probability equals $$\approx 0.3269781$$

tst <- function(m = 10) {

### pars
d <- 10 ### the number of cells for a square of 1x1 meter
size <- 100 ### size of the square

### sample blue balls in discrete values
xb <- sample(1:(size*d),m,replace = TRUE)+d
yb <- sample(1:(size*d),m,replace = TRUE)+d

### make a grid
l <- (size+2)*d+1
grid <- matrix(rep(0,l^2),l)

### make cells equal to 1 around the squares
for (i in 1:m) {
grid[xb[i]+c(-(d-2):(d+1)),yb[i]+c(-(d-2):(d+1))] = 1
}

### compute the number of cells that are equal to 1 ignoring the edges
range <- c(1:(size*d))+d
sum(grid[range,range])
}
tst(10)
arr <- replicate(10^4,tst())

hist(arr/1000^2, breaks = seq(0,0.004,10^-4), xlim = c(0.0035,0.004))
xs <- c(0,0.004, 10^-5)

### compute based on p = 0.0004
p <- 40/10000
1-(1-p)^100

### compute based on variable p
p <- arr/1000000
prob <- 1-(1-p)^100
mean(prob)


### Interpretation 2: No red and blue balls within the same 1m x 1m square of a grid In this case, the problem is similar to an urn model: you have $$100^2$$ urns in which you place red and blue balls, and you question yourself whether any red and blue are in the same urn.

### Computation

• We can compute how likely it is that the 10 blue balls occupy $$k$$ urns, which is the classical occupancy problem. See Ben's answer for: Sampling inclusion probability for multiple items

$$\mathbb{P}(K=k) = \frac{(N)_k \cdot S(t,k)}{N^t} \quad \quad \quad \text{for all } 1 \leqslant k \leqslant \min(t,N),$$

where $$(N)_k = N(N-1)(N-2) \cdots (N-k+1)$$ are the falling factorials and $$S(t,k)$$ are the Stirling numbers of the second kind.

• Then for a given number $$k$$ the probability that at least one of the red balls occupies an urn with a blue ball is binomial distributed with the probability equal to $$k/n_{urns}$$

$$\mathbb{P}(n_\text{red and blue together} \geq 1|k) = 1 - \left( 1- \frac{k}{n_{urns}}\right)^{n_{red}}$$

• The final answer is the compound distribution of the two previous points.

$$\mathbb{P}(n_\text{red and blue together} \geq 1) = \sum_{k=1}^{n_{blue}} \mathbb{P}(n_\text{red and blue together} \geq 1|k) \cdot \mathbb{P}(K=k)$$

The code below gives $$p_{computed} = 0.0951669$$ which is nearly the same as the simple estimate $$p_{estimate} = 1-\left(1-\frac{n_{blue}}{n_{urns}}\right)^{n_{red}} = 1-\left(1-\frac{10}{10000}\right)^{100} = 0.09520785$$

n_urns <- 100^2
n_blue <- 10

### function to compute stirling number
Stirling2 <- function(n,k) {
i = 0:k
terms <- (-1)^i * choose(k,i) * (k-i)^n
sum(terms)/factorial(k)
}

### function to compute occupancy problem
pk <- function(k, n_urns, n_blue) {
prod(n_urns-c(0:(k-1)))*Stirling2(n_blue,k)/n_urns^n_blue
}
pk <- Vectorize(pk)

### number of cells occupied with blues
k <- 1:n_blue

### probility of k
p_k <- pk(k, n_urns = n_urns, n_blue = n_blue)

### probability of 1 or more red in one of the k blue urns
p_occupy <- 1 - dbinom(0, size = 100, prob = k/n_urns)

### compound
sum(p_occupy*p_k)


let's presume that you have 10000 boxes, and you place blue balls in these boxes at random, and then you are placing red balls at random, let's count probability of success:

$$p_b(i) = p(\text{10 blue balls in i boxes}) = \frac{\binom{10000}{i}\binom{9}{i-1}}{10000^{10}}$$ $$p_r(i) = p(\text{100 red balls avoid i boxes with blue balls}) = (\frac{10000-i}{10000})^{100}$$ result is: $$\frac{\sum_{i=1}^{10}p_b(i)*(1-p_r(i))}{\sum_{i=1}^{10}p_b(i)}$$ this is the prob that you have at least one box with at least one blue and at least one red ball

• The combination ${{10000}\choose i}$ is the number of ways to pick $i$ boxes out of the 10000 boxes. But you can also vary in the way that you put the blue balls in those boxes (e.g. if you have 2 boxes and 10 balls, you can fill them with 1:9, 2:8, ..., etc.). So you need to multiply with the number of ways that you can partition the 10 blue balls into $i$ groups. Oct 26, 2020 at 21:08
• not exactly, that sum is the number of ways to do the partition (including urns with zero items), it should be like this: stats.stackexchange.com/questions/420104/… This number you need:en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kind Oct 26, 2020 at 21:50
• Here is another explanation probabilityandstats.wordpress.com/2010/03/27/… Oct 26, 2020 at 21:56
• @SextusEmpiricus sure but after I will pick $i$ I want to avoid $0$ in my tuples Oct 26, 2020 at 21:59
• for simplicity from $p_b$ denominator could be dropped Oct 26, 2020 at 22:22