What is the expected value of the highest roll of a \(6\)-sided die if we roll it until we get a \(4\)?
Analytical Solution
Define the stochastic process\(\{X_t\}_{t\geq 1}\) with independent discrete uniform rolls \(X_t\sim\mathcal{U}(\{1,\ldots,6\})\). Further define the running maximum
\[
S_t = \max\{X_1,\ldots,X_t\},
\]
and the stopping time\(\tau = \min\{t\geq 1: X_t = 4\}\). We include the final roll of \(4\), so \(S_\tau\in\{4,5,6\}\). We wish to compute \(\mathbb{E}[S_\tau]\).
For \(s\in\{0,1,2,3\}\), each probability is \(1\) because the final roll is \(4\). For \(s\in\{4,5\}\), we use [[order-statistics]]. Conditional on \(\tau=t\), the first \(t-1\) rolls are independent and uniform on \(\{1,2,3,5,6\}\), while \(X_t=4\). Conditioning only on \(X_t=4\) would not exclude earlier rolls of \(4\). Hence
A shorter check uses the same tail-sum formula directly at the stopping time. The maximum exceeds \(4\) precisely when a \(5\) or \(6\) appears before the first \(4\). Among \(\{4,5,6\}\), each is equally likely to appear first, so this probability is \(2/3\). Similarly, the maximum exceeds \(5\) when a \(6\) appears before the first \(4\), with probability \(1/2\). Therefore
# Packagesimport numpy as np# Set random seedrng = np.random.default_rng(seed=342)# Single experiment iterationdef roll_until_4(): test =False x = [] s = []while test isFalse: roll = rng.integers(low=1, high=7) x.append(roll) s.append(np.max(np.array(x)))if roll ==4: test =Truereturn np.array(x), np.array(s)# Empirical means_vals = []for _ inrange(10000): x, s = roll_until_4() s_vals.append(s[-1])# Print the averageprint("The expected value is: ", np.mean(s_vals, axis=0))