Winning from losing
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.