# Sample from independent uniform U(0,1) distributions and stop when the sum
# is greater than 1. Let N be that first sum. Then E[N] = e
<- function(t = 1) {
sim_sum <- 0
s_n <- 0
n while(s_n < 1) {
<- s_n + runif(1)
s_n <- n + 1
n
}c(n = n, s_n = s_n)
}
<- 1e4
reps <- data.frame(t(replicate(reps, sim_sum())))
N_sim <- table(N_sim$n)
N_sim_table <- N_sim_table / reps
N_sim_prob
<- as.numeric(names(N_sim_table))
K
<- mean(N_sim$n)
mean_N plot(K, N_sim_prob, type = 'b', ylim = c(0, 1), ylab = 'P(N = n)', xlab = 'N')
abline(v = mean_N, lty = 2)
text(x = mean_N + 1, y = 0.75, sprintf('Mean(n) = %.03f', mean_N))
Introduction
I saw this problem on r/probabilitytheory
If you pick a uniformly random real number on [0,1] and repeat this until the sum of numbers picked is greater than 1, you’ll on average pick
numbers
Posed in a way that we can actually begin to solve this.
Sample independent uniform random variables
and let . Let be the first integer such that . Find
Bonus question: What is the
Of course, the first step is to replicate the simple simulation.
Simulation
Proof
The idea of the proof is to directly solve:
So we need to calculate,
Using the proof from this answer on math.stackexchange,
when
PMF of
Since
To double check the answer we can simulate
# This is formula we computed analytically
# (K - 1) / (factorial(K))
<- as.numeric(names(N_sim_table))
K <- (K - 1) / (factorial(K))
expected_probs
plot(K, N_sim_prob, type = 'b', xlab = 'n', ylab = 'Prob(N = n)', ylim = c(0, 1), yaxp = c(0, 1, 4))
lines(K, expected_probs, type = 'b', col = 'red')