Archive for R

blind monty hall

Posted in R, Statistics with tags , , , , , , , , on January 10, 2022 by xi'an

As I was waiting for my boat on a French Guiana beach last week, I thought back about a recent riddle from The Riddler where an item does a random walk over a sequence of N integers. Behind doors. The player opens a door at the same rate as the item, door that closes immediately after. What is the fastest strategy to catch the item? With a small value of N, it seemed that repeating the same door twice and moving from 1 to N and backward was eventually uncovering the item.

Here is the cRude code I later wrote to check whether or not this was working:

  p=1+t%%N #starting item position
  h=v=s=m=1 #h=door, v=attempt number, s=direction, m=repeat number
  while(h-p){
    p=ifelse(p==1,2, #no choice
             ifelse(p==N,N-1, #no choice
                    ifelse(p==h-1,p-1, #avoid door
                           ifelse(p==h+1,p+1, #avoid door
                                  p+sample(c(-1,1),1))))) #random
    m=m+1
    if(m>2){
      h=h+s;m=1
      if(h>N){
        h=N-1;s=-1}
      if(!h){
        s=1;h=2}
      }
    v=v+1

and the outcome for N=100 was a maximum equal to 198. The optimal strategy leads to 196 as repeating the extreme doors is not useful.

triple ruin

Posted in Books, Kids, pictures, R, Statistics, Wines with tags , , , , , , , , , , on December 28, 2021 by xi'an

An almost straightforward riddle from The Riddler involving a triple gambler’s ruin: Dawn competes against three players Alessandra, Berenike, and Chinue, with probabilities of winning one round ¾, ½, and ¼, respectively, until the cumulated score reaches ±15, ±30, and ±45, for the first, second, and third games. What is Dawn’s optimal sequence of adversaries?

First, a brute force R simulation shows that the optimal ordering is to play the three adversaries first weakest, third strongest and middle fair:

ord=function(p){
  z=2*(runif(1)<p[1])-1
  while(abs(z)<15)z=z+2*(runif(1)<p[1])-1
  y=2*(runif(1)<p[2])-1
  while(abs(z+y)<30)y=y+2*(runif(1)<p[2])-1
  x=2*(runif(1)<p[3])-1
  while(abs(z+y+x)<45)x=x+2*(runif(1)<p[3])-1 
  return(x+y+z>0)}
mcord=function(p,T=1e2){
  for(t in 1:T)F=F+ord(p)
  return(F/T)}
comp=function(T=1e2){
  return(c(mcord(c(.5,.55,.45),t),
    #mcord(c(.5,.45,.55),t),#1-above
    mcord(c(.55,.5,.45),t),
    #mcord(c(.45,.5,.55),t),#1-above
    mcord(c(.55,.45,.5),t)
    #mcord(c(.45,.55,.5),t)))#1-above
    ))}

where I used probabilities closer to ½ to avoid estimated probabilities equal to one.

> comp(1e3)
[1] 0.051 0.038 0.183

(and I eliminated the three other probabilities by sheer symmetry). Second, checking in Feller’s bible (Vol. 1, XIV.3) for the gambler’s ruin probability, a simple comparison of the six orderings confirms this simulation.

dice and sticks

Posted in Books, Kids, R with tags , , , , , , on November 19, 2021 by xi'an

A quick weekend riddle from the Riddler about the probability of getting a sequence of increasing numbers from dice with an increasing number of faces, eg 4-, 6-, and 8-face dice. Which happens to be 1/4. By sheer calculation (à la Gauss) or simple enumération (à la R):

> for(i in 1:4)for(j in (i+1):6)F=F+(8-j)
> F/4/6/8
[1] 0.25

The less-express riddle is an optimisation problem related with stick breaking: given a stick of length one, propose a fraction a and win (1-a) if a Uniform x is less than one. Since the gain is a(1-a) the maximal average gain is associated with a=½. Now, if the remaining stick (1-a) can be divided when x>a, what is the sequence of fractions one should use when the gain is the length of the remaining stick? With two attempts only, the optimal gain is still ¼. And a simulation experiment with three attempts again returns ¼.

 

worst colour scale?

Posted in Books, Kids, pictures with tags , , on November 11, 2021 by xi'an

Xing glass bridges [or not]

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

A riddle from the Riddler surfing on Squid Games. Evaluating the number of survivors (out of 16 players) able to X the glass bridge, when said bridge is made of 18 consecutive steps, each involving a choice between a tempered and a non-tempered glass square. Stepping on a non-tempered square means death, while all following players are aware of the paths of the earlier ones. Each player thus moves at least one step further than the previous and unlucky player. The total number of steps used by the players is therefore a Negative Binomial Neg(16,½) variate truncated at 19 (if counting attempts rather than failures), with the probability of reaching 19 being .999. When counting the number of survivors, a direct simulation gives an estimate very close to 7:

   mean(apply(apply(matrix(rgeom(16*1e6,.5)+1,nc=16),1,cumsum)>18,2,sum))

but the expectation is not exactly 7! Indeed, this value is a sum of probabilities that the cumulated sums of Geometric variates are larger than 18, which has no closed form as far as I can see

   sum(1-pnbinom(size=1:16,q=17:2,prob=.5)

but whose value is 7.000076. In the Korean TV series, there are only three survivors, which would have had a .048 probability of occurring. (Not accounting for the fact that one player was temporarily able to figure out which square was right and that two players fell through at the same time.)

Looking later at on-line discussions, I found that the question was quite popular, with a whole spectrum of answers… Including a wrong Binomial B(18, ½) modelling that does not account for the fact that all 16 (incredibly unlucky) players could have died before the last steps.

And reading the solution on The Riddler a week later, I was sorry to see this representation of the distribution of survivors, as if it was a continuous distribution!

%d bloggers like this: