2

Here is a math problem I thought of:

  • Suppose you have $N$ = 10 coins: Each coin has a 0.5 probability of landing on heads and a 0.5 probability of landing on tails
  • Step 1: Each turn, I randomly select some of these 10 coins (i.e. generate a random integer between 0 and 10, and pick those many coins). Ex: Suppose my random number is 7, using Sampling Without Replacement, I select 7 out of the 10 coins (each coin has equal probability of being selected via Uniform Distribution, i.e. $p$ = 0.1)
  • Step 2: I then flip the coins I selected and record the results
  • I repeat Step 1 and Step 2 until at least one head has been observed in all 10 coins.
  • How many turns are required on average for this to happen?

Part 1: I first tried to solve this problem via simulation using the R programming language (1 = heads, 0 = tails, -1 = not selected):

multi_coins_flip_until_first_head <- function(n) {
    count_coins <- numeric(n)
    head_seen_coins <- logical(n)
    flips_coins <- vector("list", n)
    while(any(!head_seen_coins)) {
        selected_coins <- sample(seq_len(n), sample(seq_len(n), 1))
        for (i in seq_len(n)) {
            if (i %in% selected_coins) {
                flip <- sample(c(1, 0), 1)
                flips_coins[[i]] <- c(flips_coins[[i]], flip)
                if (flip == 1 && !head_seen_coins[i]) {
                    head_seen_coins[i] <- TRUE
                    count_coins[i] <- length(flips_coins[[i]])
                }
            } else {
                flips_coins[[i]] <- c(flips_coins[[i]], -1)
            }
        }
    }
    return(list("Number of Flips for First Head" = count_coins, "Sequence" = flips_coins))
}

The output of this function looks like this:

 $`Number of Flips for First Head`
 [1]  1 19  2  1  1  4  7  2  4  1

$Sequence $Sequence[[1]] [1] 1 0 -1 1 -1 0 0 1 -1 -1 -1 0 1 0 -1 0 0 -1 -1

$Sequence[[2]] [1] 0 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 0 0 0 -1 -1 0 1

$Sequence[[3]] [1] 0 1 -1 1 0 -1 0 -1 -1 1 -1 1 0 1 -1 -1 0 1 -1

$Sequence[[4]] [1] 1 0 -1 -1 -1 -1 0 -1 1 -1 1 0 1 1 1 -1 0 -1 -1

$Sequence[[5]] [1] 1 1 -1 1 -1 0 -1 -1 0 -1 -1 1 1 -1 -1 0 1 -1 -1

$Sequence[[6]] [1] 0 -1 0 1 -1 0 -1 -1 1 1 -1 0 1 -1 -1 -1 0 -1 -1

$Sequence[[7]] [1] -1 0 -1 0 -1 0 1 -1 -1 -1 -1 1 1 0 -1 -1 1 0 -1

$Sequence[[8]] [1] 0 1 0 0 -1 1 0 -1 -1 1 0 1 1 0 -1 -1 -1 0 0

$Sequence[[9]] [1] 0 0 -1 1 -1 1 -1 -1 -1 -1 0 0 -1 0 -1 -1 0 -1 -1

$Sequence[[10]] [1] 1 0 -1 -1 -1 1 -1 -1 1 0 -1 0 0 -1 -1 -1 0 -1 -1

Part 2: Then, I simulated this function 1000 times and plotted the results - the average of these simulations should represent the average number of times required to see at least one head in all coins:

multi_coins_flip_until_first_head <- function(n) {
    count_coins <- numeric(n)
    head_seen_coins <- logical(n)
    flips_coins <- vector("list", n)
    while(any(!head_seen_coins)) {
        selected_coins <- sample(seq_len(n), sample(seq_len(n), 1))
        for (i in seq_len(n)) {
            if (i %in% selected_coins) {
                flip <- sample(c(1, 0), 1)
                flips_coins[[i]] <- c(flips_coins[[i]], flip)
                if (flip == 1 && !head_seen_coins[i]) {
                    head_seen_coins[i] <- TRUE
                    count_coins[i] <- length(flips_coins[[i]])
                }
            } else {
                flips_coins[[i]] <- c(flips_coins[[i]], -1)
            }
        }
    }
    return(max(count_coins))
}

results <- replicate(1000, multi_coins_flip_until_first_head(10)) df <- data.frame(Iteration = 1:length(results), Output = results)

Part 3: Then, I plotted the results:

avg <- mean(results)
#[1] 6.904

ggplot(df, aes(x = Iteration, y = Output)) + geom_line() + geom_hline(yintercept = avg, color = "red", linetype = "dashed", size = 1) + labs(title = "Number Of Flips Required To See At Least 1 Head In All Coins", x = "Iteration", y = "Number of Flips") + theme_bw()

enter image description here

My Question: (Provided my simulation is correct) Is there a way to come up with a mathematical formula to arrive at this same conclusion?

From a first glance, this problems seems to require combining the Binomial Distribution (result of each coin flip), the Multinomial Distribution (number of coins being chosen in each turn) and the Geometric Distribution (number of cumulative coin flips required for a condition to be met). I suppose a "probability tree" can be constructed that outlines each possibility ... but the tree and the resulting likelihood function will obviously be completely "intractable" .

Thus, for such problems, is simulation the only way to come up with an answer?

Thanks!

stats_noob
  • 3,112
  • 4
  • 10
  • 36
  • Putting aside the question of why this would be done (a random number of coins flipped each time), it seems you could get to the same answer by flipping all of them, and set aside the ones that show heads, and continue flipping the rest, repeating this until all N are showing heads. (I think that is what you mean by "observed in all 10 coins".) This would involve the expected values of infinite probabilities. I'll see if I can work it out, and get back to you. Let me know if that is not what you mean by the question. – Bafs Nov 28 '23 at 05:37
  • Look at each coin separately. Each of those follows the same distribution. You want the expected maximum. – lulu Nov 28 '23 at 05:42
  • this question is a duplicate of the underlying computation. No sort of simple closed formula for it. – lulu Nov 28 '23 at 05:55
  • @ Bafs: thank you for your reply! this is an interesting point, let me think about this... – stats_noob Nov 28 '23 at 06:13
  • @ lulu: thank you for your reply and this question that you have linked! – stats_noob Nov 28 '23 at 06:13
  • Note: it's possible that my answer is for a different question than the one you intended, it depends on what you meant by choosing a subset of the coins. My answer assumes independence. That is, each coin is selected (or not) with probability $.5$ independently of the other coins. You might have meant "choose a number of coins uniformly between $0$ and $10$ and select that many coins." If you meant that, then the coins aren't independent. Knowing that one coin was chosen makes it less likely that another one was. – lulu Nov 28 '23 at 06:16
  • How are you selecting your number between $0$ and $10$ (inclusive, I assume)? Is it uniform? Binomial? – Brian Tung Nov 28 '23 at 07:00
  • @ Brian Tung: Thank you for your reply! Yes, using a Uniform Distribution – stats_noob Nov 28 '23 at 07:01
  • Note that there are eleven choices between $0$ and $10$ inclusive. But I'll assume that just means $p = 1/11$. ¶ I believe there won't be an analytic result. – Brian Tung Nov 28 '23 at 07:02
  • To follow up on my idea, that maybe you will be flipping all of the coins eventually, and may as well choose all of them together, the answer would be very easy to obtain. All of the coins have the same probability for heads. So, my guess is that you just need to find the log root 2 of N. For N = 10, it is ~3.322. If X is the root, then probability for each and every coin is 1/X. In this case, X is 2. – Bafs Nov 28 '23 at 13:04
  • However, 3.322 doesn't match your answer of 8.974, so maybe some adjustment could be made to my result. – Bafs Nov 28 '23 at 13:16
  • Can you clarify your "step 1" please? It is very confusing. Thanks. – Benjamin Wang Nov 28 '23 at 15:15
  • @ Benjamin Wang: Thank you for your reply! Can you please explain what part is confusing? I will try my best to explain – stats_noob Nov 28 '23 at 16:19

0 Answers0