## where is .5?

**A** Riddler’s riddle on breaking the unit interval into 4 random bits (by which I understand picking 3 Uniform realisations and ordering them) and finding the length of the bit containing ½ (sparing you the chore of converting inches and feet into decimals). The result can be found by direct integration since the ordered Uniform variates are Beta’s, and so are their consecutive differences, leading to an average length of 15/32. Or by raw R simulation:

simz=t(apply(matrix(runif(3*1e5),ncol=3),1,sort)) mean((simz[,1]>.5)*simz[,1]+ (simz[,1]<.5)*(simz[,2]>.5)*(simz[,2]-simz[,1])+ (simz[,2]<.5)*(simz[,3]>.5)*(simz[,3]-simz[,2])+ (simz[,3]<.5)*(1-simz[,3]))

Which can be reproduced for other values than ½, showing that ½ is the value leading to the largest expected length. I wonder if there is a faster way to reach this nice 15/32.

September 10, 2020 at 4:23 am

Numpy seems to be about 15x faster using my naive implementation

“`

import numpy as np

import time

start = time.time()

mat = np.random.uniform(size=3 * int(1e5)).reshape((int(1e5), 3))

simz = np.apply_along_axis(np.sort, 1, mat)

a = np.mean(

(simz[:, 0] > 0.5) * simz[:, 0]

+ (simz[:, 0] 0.5) * (simz[:, 1] – simz[:, 0])

+ (simz[:, 1] 0.5) * (simz[:, 2] – simz[:, 1])

+ (simz[:, 2] < 0.5) * (1 – simz[:, 2]))

end = time.time()

print(end – start)

“`

Clocks in at around 0.3 secs on my laptop vs 5 secs for your r code.

September 10, 2020 at 11:13 am

Thanks for the alternative! (I was not attempting at being fast, esp. when using R.)

September 10, 2020 at 6:31 pm

The R code is also much faster in pqR-2020-07-23 (see pqR-project.org) than in R-4.0.2 (1.2 seconds vs. 8.0 seconds on the computer I’m using at the moment). Further investigation reveals that most of the time is spent in ‘sort’, which has a faster implementation in pqR.

September 11, 2020 at 6:49 am

Thank you, Radford. I should definitely use pqR!