Archive for the R Category

a new Monty Hall riddle

Posted in Books, Kids, Mountains, pictures, R, Statistics, Travel with tags , , , , , , , , , on May 22, 2020 by xi'an

The Riddler was sort of feeling the rising boredom of being under lockdown when proposing the following variant to the Monty Hall puzzle:

There are zero to three goats, with a probability ¼ each, and they are allocated to different doors uniformly among the three doors of the show. After the player chooses a door, Monty opens another door hidding a goat or signals this is impossible. Given that he did open a door, what is the probability that the player’s door does not hide a goat?

Indeed, a straightforward conditional probability computation considering all eight possible cases with the four cases corresponding to Monty opening a door leads to a probability of 3/8 for the player’s door. As confirmed by the following R code:

s=sample
m=c(0,0)
for(t in 1:1e6)m=m+(range(s(1:3,s(1:3,1)))>1)

not a Bernoulli factory

Posted in Books, Kids, pictures, R with tags , , , , , , , on May 20, 2020 by xi'an

A Riddler riddle I possibly misunderstood:

Four isolated persons are given four fair coins, which can be either flipped once or returned without being flipped. If all flipped coins come up heads, the team wins! Else, if any comes up tails, or if no flip at all is done, it looses. Each person is further given an independent U(0,1) realisation. What is the best strategy?

Since the players are separated, I would presume the same procedure is used by all. Meaning that a coin is tossed with probability p, ie if the uniform is less than p, and untouched otherwise. The probability of winning is then

4(1-p)³p½+6(1-p)³p½²+4(1-p)p³½³+p⁴½⁴

which is maximum for p=0.3420391, with a winning probability of 0.2848424.

And an extra puzzle for free:

solve xxxx=2020

Where the integral part is the integer immediately below x. Puzzle that I first fail solving by brute force, because I did not look at negative x’s… Since the fourth root of 2020 is between 6 and 7, the solution is either x=6+ε or x=-7+ε, with ε in (0,1). The puzzle then becomes either

(6+ε)⌊(6+ε)⌊(6+ε)⌊6+ε⌋⌋ = (6+ε)⌊(6+ε)⌊36+6ε⌋⌋ = (6+ε)⌊(6+ε)(36+⌊6ε⌋)⌋ = 2020

where there are 6 possible integer values for ⌊6ε⌋, with only ⌊6ε⌋=5 being possible, turning the equation into

(6+ε)⌊41(6+ε) = (6+ε)(246+⌊41ε) = 2020

where again only ⌊42ε=40 being possible, ending up with

1716+286ε = 2020

which has no solution in (0,1). In the second case

(-7+ε)(-7+ε)(-7+ε)-7+ε⌋⌋ = (-7+ε)(-7+ε)(49+-7ε⌋)= 2020

shows that only -7ε=-3 is possible, leading to

(-7+ε)⌊46(-7+ε)) = (-7+ε) (-322+46ε⌋)=2020

with only 46ε=17 possible, hence

2135-305ε=2020

and

ε=115/305.

A brute force simulated annealing resolution returns x=-6.622706 after 10⁸ iterations. A more interesting question is to figure out the discontinuity points of the function

ℵ(x) = xxxx

as they seem to be numerous:

For instance, only 854 of the first 2020 integers enjoy a solution to ℵ(x)=n.

another Bernoulli factory

Posted in Books, Kids, pictures, R, Statistics with tags , , , , , , on May 18, 2020 by xi'an

A question that came out on X validated is asking for help in figuring out the UMVUE (uniformly minimal variance unbiased estimator) of (1-θ)½ when observing iid Bernoulli B(θ). As it happens, there is no unbiased estimator of this quantity and hence not UMVUE. But there exists a Bernoulli factory producing a coin with probability (1-θ)½ from draws of a coin with probability θ, hence a mean to produce unbiased estimators of this quantity. Although of course UMVUE does not make sense in this sequential framework. While Nacu & Peres (2005) were uncertain there was a Bernoulli factory for θ½, witness their Question #1, Mendo (2018) and Thomas and Blanchet (2018) showed that there does exist a Bernoulli factory solution for θa, 0≤a≤1, with constructive arguments that only require the series expansion of θ½. In my answer to that question, using a straightforward R code, I tested the proposed algorithm, which indeed produces an unbiased estimate of θ½… (Most surprisingly, the question got closed as a “self-study” question, which sounds absurd since it could not occur as an exercise or an exam question, unless the instructor is particularly clueless.)

more games of life

Posted in Books, Kids, R with tags , , , , on May 5, 2020 by xi'an

Another puzzle in memoriam of John Conway in The Guardian:

Find the ten digit number, abcdefghij. Each of the digits is different, and

  • a is divisible by 1
  • ab is divisible by 2
  • abc is divisible by 3
  • abcd is divisible by 4
  • abcde is divisible by 5
  • abcdef is divisible by 6
  • abcdefg is divisible by 7
  • abcdefgh is divisible by 8
  • abcdefghi is divisible by 9
  • abcdefghij is divisible by 10

Which brute force R coding by checking over random permutations of (1,2,…,9) [since j=0] solves within seconds:

while(0<1)
  if (prod(!(x<-sum(10^{0:8}*sample(1:9)))%/%10^{7:0}%%2:9))break()

into x=3816547290. And slightly less brute force R coding even faster:

while(0<1){
e=sample(c(2,6,8))#even
o=sample(c(1,3,7,9))#odd
if((!(o[1]+e[1]+o[2])%%3)&
(!(10*o[2]+e[2])%%4)&
(!(o[1]+e[1]+o[2]+e[2]+5+4)%%3)&
(!sum(10^{6:0}*c(o[1],e[1],o[2],e[2],5,4,o[3]))%%7)&
(!(10*o[3]+e[3])%%8)&
(!(sum(o)+sum(e))%%9)){
print(sum(10^{9:0}*c(o[1],e[1],o[2],e[2],4,5,o[3],e[3],o[4],0)));break()}}

Le Monde puzzle [#1141]

Posted in Kids, pictures, R, University life with tags , , , , , , , , on May 4, 2020 by xi'an

The weekly puzzle from Le Monde is in honour of John Conway, who just passed away, ending up his own game of life:

On an 8×8 checker-board, Alice picks n squares as “infected”. She then propagates the disease by having each square with least two infected neighbours to become infected as well. What is the minimal value of n for the entire board to become infected? What if three infected neighbours are required?

A plain brute force R random search for proper starting points led to n=8 (with a un-code-golfed fairly ugly rendering of the neighbourhood relation, I am afraid!) with the following initial position

With three neighbours, an similar simulation failed to return anything below n=35 as for instance:

oops, n=34 when running a little longer:

which makes sense since an upper bound is found by filling one square out of two (32) and adding both empty corners (2). But this upper bound is only considering one step ahead, so is presumably way too large. (And indeed the minimal value is 28, showing that brute force does not always work! Or it may be that forcing the number of live cells to grow at each step is a coding mistake…)

another surmortality graph

Posted in Books, R, Statistics with tags , , , , , , , , on April 27, 2020 by xi'an

Another graph showing the recent peak in daily deaths throughout France as recorded by INSEE and plotted by Baptiste Coulmont from Paris 8 Sociology Department. And further discussed by Arthur Charpentier on Freakonometrics. With a few days off due to reporting, this brings an objective perspective on the impact of the epidemics (and of the quarantine) compared with the other years since 2001, without requiring tests or even surveys. (The huge peak in August 2003 was an heat wave that decimated elderly citizens throughout France.)

Le Monde puzzle [#1139]

Posted in Books, Kids, R, Statistics with tags , , , , , on April 24, 2020 by xi'an

A weekly Monde current mathematical puzzle that reminded me of an earlier one (but was too lazy to check):

The integer n=36 enjoys the property that all the differences between its ordered divisors are also divisors of 36. Find the only 18≤m≤100 that enjoys this property such that all its prime dividers areof multiplicity one. Are there other such m’s?

The run of a brute force R search return 42 as the solution (codegolf welcomed!)

y=z=1:1e5
for(x in y)z[y==x]=!sum(x%%diff((1:x)[!x%%(1:x)]))
y=y[z==1]
for(k in generate_primes(2,max(y)))y=y[!!y%%k^2]

where generate_primes is a primes R function. Increasing the range of y’s to 10⁵ exhibits one further solution, 1806.