**A** nice question was posted on X validated as to figure out a way to simulate a Bernoulli B(q) variate when using only a Bernoulli B(p) generator. With the additional question of handling the special case q=a/b, a rational probability. This is not exactly a Bernoulli factory problem in that q does not write as f(p), but still a neat challenge. My solution would have been similar to the one posted by William Huber, namely to simulate a sequence of B(p) or B(1-p) towards zooming on q until the simulation of the underlying uniforms U allows us to conclude at the position of U wrt q. For instance, if p>q and X~B(p) is equal to zero, the underlying uniform is more than p, hence more than q, leading to returning zero for the B(q) generation. Else, a second B(p) or B(1-p) generation means breaking the interval (0,p) into two parts, one of which allows for stopping the generation, and so on. The solution posted by William Huber contains an R code that could be easily improved by choosing for each interval between p and (1-p) towards the maximal probability of stopping. I still wonder at the ultimate optimal solution that would minimise the (average or median) number of calls to the Bernoulli(p) generator.

## Archive for Stack Echange

## a Bernoulli factory of sorts?

Posted in Books, Kids, Statistics with tags Bernoulli distribution, Bernoulli factory, cross validated, Monte Carlo, simulation, Stack Echange on May 10, 2016 by xi'an## Le Monde puzzle [#959]

Posted in Kids, R with tags Le Monde, mathematical puzzle, R, Stack Echange on April 20, 2016 by xi'an**A**nother of those arithmetic Le Monde mathematical puzzle:

Find an integer A such that A is the sum of the squares of its four smallest dividers (including1) and an integer B such that Bis the sum of the third poser of its four smallest factors. Are there such integers for higher powers?

**T**his begs for a brute force resolution checking the integers until a solution appears. The only exciting part is providing the four smallest factors but a search on Stack overflow led to an existing R function:

FUN <- function(x) { x <- as.integer(x) div <- seq_len(abs(x)) return(div[x %% div == 0L]) }

(which uses the 0L representation I was unaware of) and hence my R code:

quest1<-function(n=2){ I=4 stop=TRUE while ((stop)&(I<1e6)){ I=I+1 dive=FUN(I) if (length(dive)>3) stop=(I!=sum(sort(dive)[1:4]^n)) } return(I) }

But this code only seems to work for n=2 as produces A=130: it does not return any solution for the next value of n… As shown by the picture below, which solely exhibits a solution for n=2,5, A=17864 (in the second case), there is no solution less than 10⁶ for n=3,4,6,..9. So, unless I missed a point in the question, the solutions for n>2 are larger if they at all exist.

A resolution got published yesterday night in Le Monde and (i) there is indeed *no* solution for n=3 (!), (ii) there *are* solutions for n=4 (1,419,874) and n=5 (1,015,690), which are larger than the 10⁶ bound I used in the R code, (iii) there is supposedly *no* solution for n=5!, when the R code found that 17,864=1⁵+2⁵+4⁵+7⁵… It is far from the first time the solution is wrong or incomplete!

## STEM forums

Posted in Books, R, Statistics, University life with tags Boston, cross validated, JSM 2014, Springer-Verlag, Stack Echange, STEM forums on August 15, 2014 by xi'an

“I can calculate the movement of stars, but not the madness of men.”Isaac Newton

**W**hen visiting the exhibition hall at JSM 2014, I spoke with people from STEM forums on the Springer booth. The concept of STEM (why STEM? Nothing to do with STAN! Nor directly with Biology. It stands as the accronym for Science, Technology, Engineering, and Mathematics.) is to create a sort of peer-reviewed Cross Validated where questions would be filtered (in order to avoid the most basic questions like “How can I learn about Bayesian statistics without opening a book?” or “What is the Binomial distribution?” that often clutter the Stack Exchange boards). That’s an interesting approach which I will monitor in the future, as on the one hand, it would be nice to have a Statistics forum without “lazy undergraduate” questions as one of my interlocutors put, and on the other hand, to see how STEM forums can compete with the well-established Cross Validated and its core of dedicated moderators and editors. I left the booth with a neat tee-shirt exhibiting the above quote as well as alpha-tester on the back: STEM forums is indeed calling for entries into the Statistics section, with rewards of ebooks for the first 250 entries and a sweepstakes offering a free trip to Seattle next year!