## sampling the mean

A challenge found on the board of the coffee room at CEREMADE, Université Paris Dauphine:

When sampling with replacement three numbers in {0,1,…,N}, what is the probability that their average is (at least) one of the three?

With a (code-golfed!) brute force solution of

mean(!apply((a<-matrix(sample(0:n,3e6,rep=T),3)),2,mean)-apply(a,2,median))

producing a graph pretty close to 3N/2(N+1)² (which coincides with a back-of-the-envelope computation):

December 16, 2019 at 12:58 pm

Hi,

Thanks a lot for writing this post.

The solution to the problem reads:

3/2*n^2-2*n+3/2*alpha

where alpha = 1 if n is odd and alpha = 0 if n is even.

Another brute force solution, enumerating all possible 3 number trios, reads:

sum(

apply(

expand.grid(1:n, 1:n, 1:n), 1, function(x) {

m <- mean(x)

return(m == x[1] || m == x[2] || m == x[3])

#!(mean(x) – median(x))

}

)

)

Using your smart check "mean – median" seems to take longer to compute it than the direct condition check (m==x[1] …).

It would be nice to include a photo of the board of the coffee room with the challenge stated.

Have a wonderful day,

Pep

December 17, 2019 at 9:53 pm

Thanks, I was not aiming at speed but shortness of code, which is the essence of codegolf, and admittedly quite silly!!!

December 18, 2019 at 4:41 pm

Pep, you seem to be missing some parentheses in your analytic formula. Can you check & re-post?

thank you

December 19, 2019 at 10:08 am

Hi Carl,

I checked again my demonstration of the analytic formula and seems ok to me. Of course, I’m assuming a particular reading of the expression. Do you suggest I could write it this way:

(3/2) * n^2 – 2 * n + (3/2) * alpha;

where alpha = 0 if n is even, alpha = 1 if n is odd?

I could also include the factor 1/n^3 multiplying the whole expression above to give the expression for the probability, that is what the challenge ask for.

Thanks a lot for your comment.

Have a wonderful day,

Pep

December 19, 2019 at 4:05 pm

Pep, thanks – I had forgotten the 1/N^3 part which is why the 3/2*alpha seemed strange, being > 1 .

December 13, 2019 at 7:46 pm

I may be wrong, but isn’t the answer different if N is even vs. odd? Suppose you draw zero and N . Then you cannot “win” if N is odd because the only possible average is not an integer. But if N is even, you can draw N/2 . This seems to suggest different overall probability.

December 14, 2019 at 6:34 am

Thank you Carl, this is correct. I only ran the reasoning with N even.

December 13, 2019 at 8:11 am

[…] article was first published on R – Xi'an's Og, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here) […]

December 12, 2019 at 4:11 pm

[…] article was first published on R – Xi’an’s Og, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]

December 12, 2019 at 1:49 pm

There’s a syntax error in that code.

Another solution (even a bit smaller :-)

mean(replicate(1e3,mean(a<-sample(0:n,3,rep=T))==median(a)))

December 12, 2019 at 6:31 pm

Thanks, I had lost the matrix in the cut&paste! And replicate is indeed a better choice (60 vs. 75 characters). But there should be a ! in front.

December 12, 2019 at 11:13 am

[…] by data_admin [This article was first published on R – Xi’an’s Og, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]