Please before continue reading, make sure to read the disclaimer at the bottom of this article.

Every time I watch on tv some game of Texas Hold’em I am always curious about the small percentages which appear in the bottom corners of the screen and tell us the chances of win for each player. There must be some kind of algorithm which implements and refreshes those numbers at each draw.

Today I am going to write about one of the first simulations I put down as code and wrapped my head around. Now that I am a little smarter at coding than I was when I coded this, I believe this whole simulation could be done much better and with A LOT less code… Anyway I guess that is a good sign since the fact I found “mistakes” in my code should mean I improved at least on the very things I made mistakes on.

The code I am about to show estimates the probabilities of drawing a pair, a three of a kind (tris) and a poker.

Let’s proceed, first of all: we need to define the deck and the drawing function.

Let’s try our drawing function hand() and check the result:

Not that good draw we were looking for huh? Anyway that’s a complete game of Texas Hold’em (provided neither of the two player folded).

Why don’t we create 100 of these games? Here they are:

At this stage, if we run the code, R will generate three tables (or matrix) with the results of each one of the 100 simulate games. Something like this:

Now we need only to look for pairs, tris and pokers. We need to define 3 functions as follows:

The result should look like this:

In 100 games, we have got 47 pairs, 2 three of a kind and no pokers! Interesting data! However, this might be a mere case! We need to run this a LOT of times to be sure the odds we obtained are at least near the real ones. Let’s build a function that can do this. For instance, the function below runs n times 100 games and then collect the results. Note that It outputs the probabilities as the mean of the probabilities occurred. Much of the code here is replicated from the functions above. I guess this could have been done a lot better! If you have any idea please let me know or leave a comment!

Let’s try for instance to run this 10.000 times, with n = 100. Here are the results:

For debugging purposes our function outputs each poker it finds. Since usually pokers are not that frequent it should be fine. 10.000 times seems not to be enough…

Let’s do 100.000 times!!!

This looks better! By simulating in less than 2 minutes 100.000 games of Texas Hold’em with 2 players we concluded that drawing a poker two times in a row is a very unlikely event, drawing a pair is a relative common event while three of a kind is rare, but not as much as poker!

I should mention that, it looks like the probability of a poker is overestimated according to the formal calculation, I believe that is either because it is an “outlier” or because I did not run the simulation a big enough number of times. Anyway the other two probabilities seem fine (you can check for more information on http://en.wikipedia.org/wiki/Poker_probability)

Hope you enjoyed!

**Disclaimer: **This article is for educational purpose ONLY. Odds generated by this code are calculated by a random simulation. As such the odds will represent an approximation of the true odds. They might even be completely wrong or misleading. This code must NOT be used for anything other than educational purpose. The provider of this code does not guarantee the accuracy of the results and accepts no liability for any loss or damage that may occur as a result of the use of this code. Understanding and agreeing to the terms of this disclaimer is a condition of use of this code. By reading the article you confirm you have understood and will comply with this disclaimer.

By "tris" and "pokers" do you mean "trips" (3 of a kind) and "quads" (4 of a kind)? The name "poker" doesn't refer to any configuration of cards (http://www.todayifoundout.com/index.php/2012/08/how-the-game-poker-got-its-name/).

ReplyDeleteThank you for your post though, I feel encouraged to start writing a poker-odds package in R now.

Also, your odds of getting a pair or trips should exclude the odds of getting a better hand that you're counting;

Deletep1_pairs_test = (p1_has_pair) & (!p1_has_trips) & (!p1_has_quads)

Hi JonoCarroll, thanks for your comment.

ReplyDeleteYes by 'tris' and 'poker' I mean, respectively, three of a kind and four of a kind (dealer's cards plus your cards). My friends use those names :), thanks for pointing out the correct ones.

As for the odds, having a 'trips' or a 'quads' implies that you have (at least) a pair. A better label for my calculation would be 'a pair or better' (assuming no flushes or other higher scores are considered aside from these 3 scores I considered), a bit of semplification is always welcomed :) . While what you suggested I guess could be labeled as '(strictly) a pair'.

I'm glad this post encouraged you to write your own R package. Leave a comment with the GitHub link if you put your package on GitHub.