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):temp

12 Responses to “sampling the mean”

  1. 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

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

    • carl witthoft Says:

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

      • 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

    • carl witthoft Says:

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

  2. carl witthoft Says:

    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.

  3. […] 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) […]

  4. […] 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 […]

  5. João Neto Says:

    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)))

    • 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.

  6. […] 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 […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.