By following twitter’s #rstats hashtag (rss feed), I recently came across a very interesting R-related blog: datanalytics.com.

The first post I read from it was about setting up an on-line reading group to go through the excellent “The Elements of Statistical Learning“. It is going on and you can find related posts here. Something you should know, however: it’s in Spanish.

Thanks to that blog, I’ve learned at least one thing: the meaning of ‘L’ as in `x<-10L`

, which I've seen in some code before but never investigated further. It turns out that it's used to cast variables as integers instead of doubles. You can see that in their code, which also has other unexpected examples:

1 2 3 4 5 6 7 8 9 10 11 12 | a <- 1:10 typeof( a ) object.size( a ) a[1] <- 10 typeof( a ) object.size( a ) a <- 1:10 a[1] <- 10L typeof( a ) object.size( a ) |

The blog also presented a puzzle last week. The gist of it is that you play three different gambling games:

- Game 1: at each step, you win 1 point with probability 0.49 and lose 1 point with probability 0.51.
- Game 2: at each step, you look at how many points you have accumulated so far. If it's a multiple of 3, you win 1 point with probability 0.095 and lose 1 point with probability 0.905. Otherwise, you win 1 point with probability 0.745 and lose 1 point with probability 0.255.
- Game 3: at each step, start with tossing a fair coin: head you play game 1, tail you play game 2.

If you simulate this (or write down the maths), you'll see that the expectation for the score after 1000 steps is negative for both game 1 and game 2; you will lose with both games. But mixing the two games randomly as in Game 3 produces a winning game!

Like datanalytics, I invite you to think about why that is.

Here is what the score distribution looks like for each game after 5000 iterations:

**EDIT:** Two probabilities were inverted (see comment by **jkomi** below). Text has been corrected now.

>you win 1 point with probability 0.095 and lose 1 point with probability 0.905

0.095 and 0.905 are reverse

Oops, you’re right; I inverted two probabilities but in the second case, not the first one.

If current score=3k, you lose with probability 0.905.

If current score=3k+1 or 3k+2, you lose with probability 0.255.

It’s probably easier to read like this.

I’m correcting the post. Thanks!

oh, you right. inversion was in second and thanks to modification

and I see what this trick is about..

Nice counter-intuitive result Thanks, I enjoyed that a lot.