shelled and riddled

Posted in Books, Kids, pictures, R, Statistics with tags , , , , , , , on August 10, 2022 by xi'an

Consider a shell game with three shells and a ball with The Riddler constraint that the location of the shell with the ball is always exchanged with the location of an empty shell, randomly chosen. If one starts with the ball as rightmost, what is the distribution of the location of the ball after N steps?

Running an exploratory R code like

o=rep(0,3)
for(n in 1:1e6){
b=c(0,0,1)
for(t in 1:N){
i=sample((1:3)[!b],1);b=0*b;b[i]=1}
o=o+b}


shows that the difference in probability is between the rightmost position and both others, starting at zero, and evolving as p⁺=(1-p⁻)/2, with the successive values 0,1/2,1/4,3/8,5/15,11/32,… Very quickly converging to 1/3.

Goats do room

Posted in Books, Kids, R, Statistics, Wines with tags , , , , , , , , , , , , on July 16, 2022 by xi'an

The riddle of the week is about 10 goats sequentially moving to their room, which they have chosen at random and independently (among ten rooms), unless another goat already occupies the room, in which case they move to the first free room with a higher number or fail. What is the probability that all goats end up in a room?

Coding the experiment is straightforward:

g=sample(1:N,N,rep=TRUE)
o=0*g
for(i in 1:N){
if(min(o[g[i]:N])){f=f+1;break()
}else{
o[min(which(!o[g[i]:N]))+g[i]-1]=1
}}}


returning an estimated probability of approximately 0.764.

As I had some free time during the early mornings at ISBA 2022, I tried to reformulate the question as a continuous event on uniform order statistics, turning to be at most one uniform larger than (N-1)/N, at most two larger than (N-2)/N, and so on… Asking the question on math.stackexchange quickly produced an answer that reversed engineered my formulation back to the goats (or parking lot), with a generic probability of

$\dfrac{(N+1)^{N-1}}{N^N}$

which of course coincides with the Monte Carlo approximation!

As an aside, I once drank South-African wines named Goats-do-Roam and Goat-Roti at my friends Jim and Maria’s place,  and they were quite enjoyable!

Bayes in Riddler mode

Posted in Books, Kids, R, Statistics with tags , , , , , on July 7, 2022 by xi'an

A very classical (textbook) question on the Riddler on inferring the contents of an urn from an Hypergeometric experiment:

You have an urn with N  red and white balls, but you have no information about what N might be. You draw n=19 balls at random, without replacement, and you get 8 red balls and 11 white balls. What is your best guess for the original number of balls (red and white) in the urn?

With therefore a likelihood given by

$\frac{R!}{(R-8)!}\frac{W!}{(W-11)!}\frac{(R+W-19)!}{(R+W)!}$

leading to a simple posterior derivation when choosing a 1/RW improper prior. That can be computed for a range of integer values of R and W:

L=function(R,W)lfactorial(R)+lfactorial(W)+
lfactorial(R+W-19)-lfactorial(R-8)-
lfactorial(W-11)-lfactorial(R+W)


and produces a posterior mean of 99.1 for R and of 131.2 for W, or a posterior median of 52 for R and 73 for W. And to the above surface for the log-likelihood. Which is unsurprisingly maximal at (8,11). The dependence on the prior is of course significant!

However silly me missed one word in the riddle, namely that R and W were equal… With a proper prior in 1/R², the posterior mean is 42.2 (unstable) and the posterior median 20. While an improper prior in 1/R leads to a posterior mean of 133.7 and a posterior median of 72. However, since the posterior mean increases with the number of values of R for which the posterior is computed, it may be that this mean does not exist!

self-avoiding random angles

Posted in Books, Kids, pictures, Statistics, Travel with tags , , , , , on June 17, 2022 by xi'an

An apparently easy riddle from The Riddler this week: given N random half-lines starting from a N-S segment, what is the probability that none ever intersect? If m ½-lines are on the same side of the segment, they will not intersect when their angle with the segment is decreasing with the longitude of the endpoint of this ½-line on the segment. Assuming that drawing a ½-line at random is akin to uniformely drawing an angle on (0,π), this no X’ing event happens when the m angles are properly ordered, a 1/m! event. Independently, the probability for the segments on the other side is 1/(N-m)! The joint probability is thus

$\displaystyle\sum_{m=0}^N {N\choose m}\frac{1}{2^N} \frac{1}{m!(N-m)!}=\frac{(2N)!}{2^N(N!)^3}$

Carmichael number, more or less

Posted in Books, Kids, Statistics with tags , , , , , , , on May 6, 2022 by xi'an

A quick-and-dirty R resolution of a riddle from The Riddler, namely to find a Carmichael number of the form abcabc:

library(numbers)
for(i in 1:9)
for(j in 0:9)
for(k in 0:9){
x=i*100100+j*1010+k*101
if(!isPrime(x)){
p=primeFactors(x)
if((prod(apply(outer(p,p,F="=="),1,sum)%%2))&
(!max((x-1)%%(p-1))))break()}}


resulting into the number 101 101, since its prime factors are

> primeFactors(101101)
[1]   7  11  13 101


and 6, 10, 12, and 100 are divisors of 101100:

> primeFactors(101100)
[1] 2 2 3 5 5 337