Sum of uniform random variables until sum is greater than one

Author

R package build

Published

November 10, 2022

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 e2.718 numbers

Posed in a way that we can actually begin to solve this.

Sample independent uniform random variables U1,U2, and let Sn=i=1nUi. Let N be the first integer n such that Sn>1. Find E[N]

Bonus question: What is the E[Sn]? Can we find its distribution?

Of course, the first step is to replicate the simple simulation.

Simulation

# 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

sim_sum <- function(t = 1) {
  s_n <- 0
  n <- 0
  while(s_n < 1) {
    s_n <- s_n + runif(1)
    n <- n + 1
  }
  c(n = n, s_n = s_n)
}


reps <- 1e4
N_sim <- data.frame(t(replicate(reps, sim_sum())))
N_sim_table <- table(N_sim$n)
N_sim_prob <- N_sim_table / reps

K <- as.numeric(names(N_sim_table))

mean_N <- mean(N_sim$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))

Proof

The idea of the proof is to directly solve:

E[n]=n=1nP(N=n)

So we need to calculate, P(N=n). This is the same as, P(Sn11 and Sn>1). If we calculate P(Snx)=FSn(x) we can complete the proof.

P(Snx)

Using the proof from this answer on math.stackexchange,

when x[0,1], claim is that P(Snt)=xnn!. When n=1, then P(S1t)=P(U1t)=t. Assume true for n. Then,

P(Sn+1x)=P(Sn+Un+1t)=01P(Sn+ut)f(u)1 duSn and Un+1 independent=01P(Sntu) du=0tP(Sntu)Induction Hypothesis duSince P(Sntu)=0 when u[t,1]=0t(tu)nn! du=1n!(1n+1(tu)n+1|u=0u=t)=tn+1(n+1)!

PMF of Sn

Since FSn(x)=xnn! we have fSn(x)=dFSndx=nxn1n!

P(N=n)

P(N=n)=P(Sn11 and Sn>1)=P(Sn>1|Sn11)P(Sn11)=01P(Un+x>1|Sn1=x1)P(s1|Sn1=s)1fSn1(x) dx=01P(Un+x>1) fSn1(x) dxUn and Sn1 independent=01(1Fn(1x)) fSn1(x) dx=01x(n1)xn2(n1)! dx=n1(n1)!01xn1 dx=n1n(n1)!=n1n!

To double check the answer we can simulate

# This is formula we computed analytically
# (K - 1) / (factorial(K))
K <- as.numeric(names(N_sim_table))
expected_probs <- (K - 1) / (factorial(K))

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')

E[N] = e

E[N]=n=2nP(N=n)=n=2n(n1)n!=n=21(n2)!=n=01n!since n=0xnn!=ex=e