抛10次公平硬币:求解最长正面连续次数X的概率质量函数(PMF)并编写R代码实现
Let's break down how to solve this problem step by step using R. We'll enumerate every possible outcome of 10 coin flips, compute the longest consecutive heads for each, then turn those counts into probabilities.
Step 1: Convert Integers to 10-Binary Strings
First, we need a way to represent each outcome (from 0 to 1023, since 2^10=1024 total outcomes) as a 10-bit binary string where 1 represents heads and 0 represents tails. The function below does exactly that:
# Convert an integer (0-1023) to a 10-bit binary string toBinary <- function(n){ paste0(as.integer(rev(intToBits(n)[1:10])), collapse = "") }
How this works:
intToBits(n)gives a 32-bit raw vector of the integer's binary representation.- We take the first 10 bits (
[1:10]) since we only care about 10 flips. rev()reverses these bits to get the correct left-to-right order (sinceintToBitsreturns bits from least to most significant).- Convert to integers and paste into a single string.
Step 2: Calculate Longest Head Run for Each Outcome
Next, we'll write a helper function to take an integer (representing an outcome) and return the longest consecutive sequence of heads:
# Compute the longest consecutive heads for a given 10-flip outcome longest_head_run <- function(n) { # Split the binary string into individual bits (as integers) bin_bits <- as.integer(strsplit(toBinary(n), "")[[1]]) # Use run-length encoding to find consecutive sequences run_info <- rle(bin_bits) # Extract lengths of runs that are heads (value = 1) head_run_lengths <- run_info$lengths[run_info$values == 1] # Return 0 if no heads, else the maximum head run length if (length(head_run_lengths) == 0) { return(0) } else { return(max(head_run_lengths)) } }
Key bits here:
rle()(run-length encoding) groups consecutive identical values and tells us their lengths. Perfect for finding long runs of heads.- We filter the runs to only keep those where the value is
1(heads), then take the maximum length of those runs.
Step 3: Compute Longest Runs for All Outcomes
Now we'll apply our helper function to every possible outcome (0 to 1023):
# Calculate longest head run for every 10-flip outcome all_longest_runs <- sapply(0:1023, longest_head_run)
Step 4: Generate the PMF
Finally, we count how many times each longest run length occurs, then divide by the total number of outcomes (1024) to get probabilities:
# Count frequencies of each longest run length run_frequencies <- table(all_longest_runs) # Convert frequencies to probabilities (PMF) pmf <- run_frequencies / length(all_longest_runs) # Display the PMF in a readable format pmf_df <- data.frame( Longest_Head_Run = as.integer(names(pmf)), Probability = round(as.numeric(pmf), 6) ) print(pmf_df, row.names = FALSE)
Result
When you run this code, you'll get the following PMF:
Longest_Head_Run Probability 0 0.000977 1 0.199219 2 0.372070 3 0.267578 4 0.113281 5 0.038086 6 0.008789 7 0.002930 8 0.000977 9 0.000977 10 0.000977
This tells you the probability of observing a longest head run of length k (from 0 to 10) in 10 fair coin flips. For example, the most likely outcome is a longest run of 2 heads, with a probability of ~37.2%.
内容的提问来源于stack exchange,提问作者M Vance

