25 Şubat 2013 Pazartesi

Random Walks and Gambler's Ruin

To contact us Click HERE
Suppose you have $100 and you decide to go to a casino to try to double your money. Are you better off putting the whole $100 on a single bet, or should you make a lot of smaller bets? Maybe you should adjust the size of your bet as the evening progresses? Should you stop as soon as you reach $200 (if you ever do), or keep going?

Lots of people have opinions about questions like these. Today, I will show you how to calculate the correct answers yourself with just a few lines of code in R. Even better, you will understand the approach, which means you can do your own analysis of whatever strategy you want to test. And best of all, it's free: no need to spend real money at an actual casino to find out.

First, you need a copy of R. This is the free, high quality,open source statistical programming language that has become astandard for statisticians in industry and academia because it is botheasy and powerful. Download the latest version for Windows, Mac, orLinux from The R Project forStatistical Computing. Click on "Download R" and select a mirror(meaning, pick a site located close to you, to speed up the downloadprocess - there are mirrors all over the world), then click "DownloadR for Windows" (or Mac or Linux). Then just double click the installerand accept the default selections. You should now have a desktop iconor Start menu entry for starting R. You can copy and paste the samplecode from this blog post right into the R console window, and it willprint answers and draw graphs right on your computer screen.

We are going to answer the questions by running simulations. Not thegiant computer-game kind of simulations, with photo-realistic images ofblackjack tables, just a simple mathematical simulation of theessential elements of the process.

What do we need to know to set up the simulation? Not very much. Wedon't even need to know the details of any particular casino game,just your probability of winning and your payoff if you dowin. These vary depending on the game you choose to play.

So, let's assume the following situation:
  • You start with some initial amount of money.
  • You choose a size for your next bet.
  • With probability w, you win back your bet plus more.
  • With probability 1-w, you lose your bet.
  • You decide whether to play again or to stop.
  • You have to stop if you cannot make a minimum bet.
Let 'm' represent how much money you have to play with. Let 'b'represent the amount you choose to bet, which must be between 0 and'm'. Let 'w' be the probability of winning, and let 's' be themultiple of your bet that you get if you win. In symbols:
  • With probability 'w', you now have 'm+b*s', because you get back your bet, bringing your total back to 'm', but then on top of that you get 's*b' as a prize.
  • With probability '1-w', you now have 'm-b', because you lose the amount 'b' that you bet.

We can code this up using R without difficulty. We have only tospecify the strategy you want to test. We can encapsulate yourstrategy into a function that returns the size of your nextbet, as long as we interpret a zero or negative bet size as meaningyou choose to end the game and walk away without further betting.

However, the questions at the beginning of this article asked whetheryou would be "better off" under certain strategies. This is trickierto decide, since it depends on your personal values (both moral andfinancial). In other words, the answer depends on you.

In order to have something to discuss here, I will rank orderthe strategies according to the probability that you do not losemoney. However, you can choose to rank them by other criteria, ifyou want, such as the average amount of money you walk away with. Happily,the results of our simulation will provide a complete picture of thepossible outcomes, so you can decide for yourself which strategy youprefer.

Here's the code. You can copy and paste this into R now.

w <- 0.48s <- 1f <- 0.5nextBet <- function(m) {  if(m >= 200) 0  else m*f}oneNight <- function() {  m <- 100  b <- nextBet(m)  while(b >= 1 & m >= b) {    if(runif(1) < w) m <- m + b*s    else m <- m - b    b <- nextBet(m)  }  m}score <- function() {  n <- 1e4  x <- 0  for(i in 1:n)    if(oneNight() >= 100) x <- x+1  x/n}print(score())

This should only take a second or two to run, after which it shouldprint a number around 0.37, which means that in about 37% of the testcases, the strategy did allow you to leave with at least as much moneyas you started with. But what exactly is the strategy we are testinghere?

Let's examine the code. The first line sets the probability of winningto be 48%. That's because in R, the two characters '<' and '-'together act like an arrow pointing left, and they mean "assign".

The next line sets s=1, which means you are playingdouble-or-nothing.

Finally, the strategy: 'f' represents the fraction of your currentbalance that you will bet each turn. In this example, 'f' is 1/2,which means that on your first turn, you bet $50, which is half yourbalance. If you lose, you will only have $50 left, so your second betwill be half of that, or $25. If you win, you will have $150, so yoursecond bet will be $75. And so on.

How does 'f' come to mean 'fraction to bet'? The answer is in the'nextBet' function. This function receives as an input your currentmoney balance 'm'. If you have reached $200, it returns zero, meaningtime to go home. Otherwise, it returns 'm*f', which is fraction 'f' ofyour current balance.

You can modify the code to test other strategies by changing the'nextBet' function. We will look at an example toward the end. Firstthough, let's see how 'nextBet' gets used. The 'oneNight' functionstarts you off with m=100 dollars. Then it calculates your initialbet. As long as that bet is positive, it draws a random number betweenzero and one using 'runif(1)', and if that is less than 'w', youwin. Winning raises your balance to 'm+b*s', while losing lowers it to'm-b'. Finally, you get to decide the size of your next bet; choosingzero means you exit the loop and are done. I have imposed a minimum bet of$1 here, so actually, if your balance drops below $2, half of it willbe below $1, so you will stop. I have also insisted that you haveenough money to cover the bet (that's the 'm >= b' condition in thewhile loop).

Calling the 'oneNight' function simulates a single night at thecasino. However, any one night could be lucky or unlucky, purely bychance, irrespective of the strategy you want to test. So the 'score'function calls 'oneNight' ten thousand times, to give a very thoroughevaluation of the possible results.

You can modify the 'score' function to reflect whatever metric youwant to use for ranking strategies. I have made it count up the numberof nights in which you walk out with at least the $100 you startedwith, but you could instead ask it to compute the average dollaramount that you end up with each night, by writing something like

score <- function() {  n <- 1e4  x <- 0  for(i in 1:n)    x <- x + oneNight()  x/n}

If you copy and paste that in and run 'print(score())' again, R willprint a number around 89, meaning that on average you take home $89each night. In fact, in this specific example, you actually takehome either at least $200 (in 37% of the cases) or something close tozero (in 63% of the cases), which simply happen to average to $89:in no case do you ever take home an intermediate value like $89.

Notice that $89 is less than your initial $100 balance. Thisis bad: it means that on average, you lose $11 each night. The morenights you play this game, the more you lose. Yes, on any given night,you might win, and temporarily reverse the trend, but if you play manynights, you will find your money draining away, slowly and not quitesteadily, but inescapably.

If you like, you can even see a histogram or density plot showing thevariety of outcomes:

score <- function() {  n <- 1e4  x <- c()  for(i in 1:n)    x <- c(x,oneNight())  plot(density(x))  mean(x)}print(score())

Here's the result:

You see a large peak near zero (you never really go negative, that'sjust an artifact of the smoothing process inherent in drawing thecurve), and a smaller peak at and above $200. If you win the first twobets, you walk away with $225, but other combinations of wins andlosses can lead to a variety of other winning outcomes between $200and $300.

I've been saying you will get an answer "close" to $89, because eachtime you run the program, you will get different random numbers, andso get a slightly different final answer. That's why we simulate10,000 different nights: it helps average out the noise, so that youwind up with a pretty consistent analysis, regardless of the specificroll of the dice. If you want to always get the same answer each time,put 'set.seed(123)' at the start of the code instead.

Mathematicians call this sort of situation a "random walk", becauseyour balance staggers randomly up and down over time, and it has"absorbing barriers" at $2 and $200, because once you reach (or pass)those values, you stop. Here is a picture of one particular night,showing your balance over time:

In this example, you won the first bet, but then lost the nexttwo. Then you won again, but then you lost 5 times in a row, whichforced you to stop.

So far, so good (or bad). Whether you like the odds reflected in these pictures or not, they are the results of betting half your cash each time, in a double-or-nothing game with a 48% chance of winning, given that you stop if you double your initial cash. But the real question is, "compared to what?" We need to try some alternative strategies to see if they are better or worse.

We assume you cannot change 'w' and 's', because those arefixed characteristics of the game you are playing. In reality, youcould go look for a different, more favorable game, but 48%double-or-nothing is about as favorable as typical casino games get,actually.

So what can you change? You can change 'f', or you can modify the'nextBet' function to do something else, such as bet a fixed dollaramount each time, rather than a fixed percentage. This is easy enough:type in

f <- 25nextBet <- function(m) {  if(m >= 200) 0  else f}

and now 'f' is the fixed dollar amount of each bet, in this case $25each time.

So, try some experiments. Change 'f' to reflect different fractions ordifferent fixed size bets, and see what happens. Draw the densitycurves to see the whole story, or just pick the strategy with thehighest score. Let me know in the comment section if you find astrategy you think is really good - but be warned, ultimately, acasino exists to take your money, so stick with computer simulationsand stay out of actual casinos. One can in fact prove, mathematically,that in this sort of game there is NO "winning" strategy, meaning onethat returns on average more than your original $100. You can keepyour original $100 by not going to the casino at all, but the moreoften you bet, the more likely you are to lose.

To demonstrate that last remark, here are the results if you bet yourfull balance in one big bet: you get a 48% chance of walking away with$200, making this strategy "better" (by my scoring definition) thanthe first one we looked at, since that only gave you a 37% chance ofwinning. This raises your average payout to $96, still less than the$100 you started with (as I said, this is unavoidable), but betterthan the $89. Of course, you don't have as much "fun", since theevening is over after just one bet, either way. The distribution ofoutcomes is very sharply peaked, at zero and at $200, since these arethe only two possible outcomes.

Conversely, if you decide to make the evening last by making smallerbets, you wind up hurting your chances of winning: the more often youbet, the more likely it is that the casino takes your money, becausethe odds are in its favor. If we set 'f <- 0.1' in our originalcode, so that you bet only 10% of your balance each time, you win onlyabout 26% of nights, and your average balance is $61. The distribution ofoutcomes is also more skewed: more probability of losing everything,less of reaching, let alone exceeding, $200, as shown in the image atthe very beginning of this post.

Similarly, if we make a fixed size small bet, say $10 each time, weget a 30% chance of winning, and a $62 payout. Again, the results areworse financially than just betting your whole $100 in one shot,although they might provide more "entertainment value" since you getto keep playing longer.

Now it's your turn. Think up some new strategies you would like tocompare, code them up and see what you can discover! What happens,for instance, if you limit yourself to 20 bets rather than continuingto play indefinitely until you reach zero or $200? (Hint: modify the'oneNight' function to count the number of bets 'n', then add '& n<= 20' in the condition of the 'while' loop.) (Warning: nostrategy, no matter how clever, will prevent you from losing money atthis game, so don't try it with real money!)

If you liked this article, you may also like Supply, Demand and Market Microstructure for a more elaborate, "agent based" simulation of economic activity, or check out the Contents page for a complete list of past topics.

Please post questions, comments and other suggestions using the box below, or email me directly at the address given by the clues at the end of the Welcome post. Remember that you can sign up for email alerts about new posts by entering your address in the widget on the sidebar. If you prefer, you can follow @ingThruMath on Twitter, where I will tweet about each new post to this blog. The Contents page has a complete list of previous articles in historical order. Now that there are starting to be a lot of articles, you may also want to use the 'Topic', 'Search' or 'Archive' widgets in the side-bar to find other articles of related interest.

I hope you enjoyed this discussion. You can click the "M"button below to email this post to a friend, or the "t" button toTweet it, or the "f" button to share it on Facebook, and so on. Seeyou next time!

Hiç yorum yok:

Yorum Gönder