# “Hold Only That Pair of 2s?” Studying a Video Poker Hand with R

Whenever I tell people in my family that I study Statistics, one of the first questions I get from laypeople is “do you count cards?” A blank look comes over their face when I say “no.”

Look, if I am at a casino, I am well aware that the odds are against me, so why even try to think that I can use statistics to make money in this way? Although I love numbers and math, the stuff flows through my brain all day long (and night long), every day. If the goal is to enjoy and have fun, I do not want to sit there crunching probability formulas in my head (yes that’s fun, but it is also work). So that leaves me at the video Poker machines enjoying the free drinks. Another positive about video Poker is that \$20 can sometimes last a few hours. So it should be no surprise that I do not agree with using Poker to teach probability.  Poker is an extremely superficial way to introduce such a powerful tool and gives the impression that probability is a way to make a quick buck, rather than as an important tool in science and society. The only time that I have used Poker in teaching (besides when required), is to cover the hypergeometric distribution and sampling without replacement.

Since I took Intro Probability Theory, I have always wondered what to do in the following situation. Say a pair of cruddy low cards appear on the first draw. The game only awards money for pairs of jacks or better. If all I have in the hand is a pair of low cards and no face cards, my decision is easy: hold the pair of low cards. But what if there is at least one face card showing (no other pairs)? Pictorially this looks like

The conundrum:

1. Hold the two low cards and deal, hoping for a three of a kind, or
2. Hold the two low cards AND one of the face cards, hoping for a three of a kind, OR a pair of Jacks of Better.

Under each of these decisions, which yields the highest probability of winning something and which one yields the highest payout? This problem can be solved exactly by using combinatorics, conditional probability and expectation, but since a video poker game is basically a simulator (though likely biased), I wrote my own simulation. For the answer, scroll to the end!

Data Structure

In most card games, we would want to store the state of the game: the outstanding cards in the deck(s), and the hand(s) of each player. In standard video poker, there is one deck, and one player, so only the player hand needs to be recorded because every card in the deck is either in the hand, or it is not. One obvious way to represent the hand is as an array of denomination/suit tuples in an array. Unfortunately, this data structure requires other data structures to store the possible suits, and possible denominations. It is also more tedious to detect certain kinds of wins. For this simulation, I use a 13 x 4 matrix where each row is a different denomination, and each column is each of the four suits. This matrix allows us to easily see which cards are possible to be dealt. Additionally, this matrix, as well as vector-based languages such as R, make it easy to detect wins. Such a matrix looks like the following for the hand 2 5♣ 8♥ 8♣ A♦

where Cij denotes a card, i is the denomination $i \in \{ 2, \ldots, 10\} \cup \{J, Q, K, A\}$ and j is the suit $j \in \{\heartsuit, \diamondsuit, \spadesuit, \clubsuit \}$ and H is the player’s hand in question.

Detecting Wins

Poker wins are not disjoint. A three of a kind involving Jacks is also a pair of Jacks or better, etc. When checking wins, I start with the lowest paying win, and move up to Royal Flush, only keeping track of the highest win. Thus, this algorithm detects a four-of-a-kind involving Queens as Jacks or Better, two pairs of Queens, and a three-of-a-kind of Queens, but only counts it as the highest win, the four-of-a-kind.

1. Pair of Jacks or Better: a pair of Jacks, Queens, Kings or Aces. In A, this is simply the condition that at least one row in rows 10 through 13 has a row sum greater than 1.
2. Two pair: two pairs of anything. In A, this is the condition that at least two rows have a sum greater than 1.
3. Three of a kind: three of any card. In A, this is the condition that at least one row has a sum of at least 3.
4. Straight: all 5 cards can be permuted such that they form an ascending sequence: A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A. This case is interesting and will be discussed in a bit.
5. Flush: all 5 cards are of the same suit. In A, this is the condition that at least one column has a sum of at least 5.
6. Full House: one three-of-a-kind, and a pair of anything. In A, this is the condition that a row has sum 3, and another row has sum 2.
7. Four of a Kind: 4 of any card. In A, this is the condition that a row has sum 4.
8. Straight Flush: the 5 cards can be permuted to form an ascending sequence and are all of the same suit. In A, this is simply the condition that we have a straight and a flush in the same hand.
9. Royal Flush: a straight flush with the Ace as the high card. In A, this is simply the condition that we have a straight flush AND the sum of row 13 is 1.
Of course, this “short circuit logic” only works for a game containing 5 cards. Also, note that under my scenario (a pair of low cards is dealt first), it is never possible to have a straight, flush, royal flush, or straight flush as the highest wins. Also, it is not possible to have Jacks or Better as the highest win because we already have one pair (low cards), and if we randomly are drawn a pair of Jacks or Better, we then have two pairs as the highest win.
Detecting the Straight: In A, we have a straight when five successive rows have sum equal to 1. We can do this iteratively, but there is a better way. Note that if all of the row sums are 0 or 1, we can treat the vector of row sums as a binary number and convert it to its integer representation. Each binary number has 13 bits. If we let 2 be the zeroth power, then straights will lead to the following binary and integer representations:

Bug alert: It just occurred to me that there are many more wrap-around straights such as Q, K, A, 2, 3. This will be fixed this evening.

From basic computer science and number theory, every natural number can be written as the sum of distinct powers or 2 and the representation of such an integer is unique. Furthermore, the sum of n successive powers of 2 is divisible by $2^n - 1$. After some experimentation I came up with the following rule: if all of the row sums are 0/1 and the integer representation of this binary vector is divisible by $\frac{2^5-1}{2}$, then A is a straight. The only straight that does not fit this pattern is the wrap-around straight: J, Q, K, A, 2 which can be checked manually.
The Algorithm
1. Randomly generate a hand containing a pair of low cards (2-10) and at least one face card.
2. Hold the pair of low cards. Under strategy 2, hold one (and only one) of the face cards.
3. Discard the unheld cards from the deck and draw 2 or 3 cards at random from the same deck.
4. Check for wins.
5. Increment a win counter.
6. Repeat steps 1-5 tons of times, recording the percentage of hands that yielded a win, of the n games/hands played.

Results: Hold the Pair of Low Cards Only

My usual strategy is to always hold the low pair and take one face card along for the ride. That way, I hopefully match one of the two denominations I hold. My parents on the other hand, always told me to hold the low pair only, because that gives one more card (degree of freedom) for a win. It turns out they were right. Each game consisted of 1,000 hands. A percentage of these hands yields a win. This percentage is a random variable, so I ran this simulation to play 1,000 games. The table below shows the distribution of the win percentages.

Note that under strategy 1 (hold low pair only), all wins are more likely than under strategy 2! Of course, the estimate in the last column is an average; the mean in this case. The plot below shows the distribution of win percentages for both strategies.

The Code

The code for my simulation is below. Note that it can easily be modified for your own target hands of interest. In my simulation, certain functions were never used because certain winning hands were not possible.

DISCLAIMER: I did this for fun, and it is possible that there are bugs or problems with my code, algorithm or simulation. The results seem correct because I empirically I seem to do about the same using either strategy, and in a gambling perspective, an 8% discrepancy is not likely to set off bells in the head.

### 19 comments to “Hold Only That Pair of 2s?” Studying a Video Poker Hand with R

• Evan Sparks

Very cool post, and nice, easy-to-read code.

You’re pretty explicit about the question here – “what proportion of hands am I likely to win?” in each of the two situations. Of course, the decision of what to do in this situation should be “which of these two options maximizes my expected profit?” Because a full house pays out more than a pair, for example, the second strategy may be more profitable. The way to handle this would be to weight the outcomes by the payouts when you take the average which would give you average dollars expected by from each strategy. Then, pick the strategy that gives you the highest expected payout.

As you say, though, all outcomes are more likely in strategy 1 than in strategy 2, so weighting the outcomes differently won’t have an impact.

• Yes, you’re right. The payout is more important. I did write some code to do that, but after seeing that every win was more likely under strategy 1, and me getting lazy, I didn’t analyze it.

• This is awesome. Ta.

• hi,

I guess you forgot to insert ‘library(ggplot2)’ in your code, because when someone runs it, there s an error in last row (chart using ggplot2 syntax)

Regards,

Leonard

• Thanks. Fixed.

• Ted

I would propose that you answered the wrong question. I found this page looking for an answer to “Which gives a better chance for winning – Holding the pair of 2s OR holding the Jack?” Not AND. Holding the Jack doesn’t help anything in this scenario – it might as well be a 3 you’re holding, because for the 2nd pair, you don’t NEED Jacks or Better. Holding the Jack as a 3rd card only decreases your chances, as your parents told you (and your Simulator discovered). But what about holding the Jack ONLY, compared to the pair of 2s? I know the win would be better with Two Pair, but I’m wondering about the chances for Any Win.

So if you could recode your Simulator to answer that question, I would appreciate it. đź™‚

• Good point. I realize that was something I wanted to test but apparently got wrapped up in the other test and forgot about that one. Anecdotally, I have tried both strategies: holding only the Jack, and holding only the pair of low cards and noticed not much of a difference. That might be worth a part 2.

The simulation outputs the probability of any win by summing across win types.

• Barack Obama

My take is that the J must be better.

My reasoning is that the casino is giving you some extra incentive to hold a pair of 2’s or 3’s.
Giving you that extra thing to bet on, is to me, almost like giving you an option to buy insurance.
So they want you to keep a hand that is less likely to win.

• You state the question as:
Hold the two low cards and deal, hoping for a three of a kind, or
Hold the two low cards AND one of the face cards, hoping for a three of a kind, OR a pair of Jacks of Better.

in these conditions, neither a straight, a flush, or a straight flush are possible.

• I think I stated that, unless you’re implying I was too broad in what hands are possible? I actually computed the probability of a general win, not just the probability of a three of a kind, or a three of a kind or jacks or better.

• Actually,

given that are no straights, flushes, straight flushes, or Royal Flushes, there is no difference between
2,2, 5 ,6 ,7, J

and any other combination of

<10, same <10, different <10, different<10, different 10

so no need to randomly select them. only need to look at all combination under that.

• Yes you’re right. I mentioned that this could be done combinatorially and it probably wouldn’t take too long… in another language like Python, I would probably do it that way. This was just a fun example of a simulation in R (and R is better for that than Python).

• actually =(46*45*44) + (46*45) would be all possible combinations, quite possible to get a complete number.

• Jackson

Shouldn’t there be a 1 in row 13’s total in your matrix graphic? The numbers don’t add up to 5 without it.

• rick

Now somebody needs to implement the algorithm for the optimal playing strategy as outlined here: http://wizardofodds.com/games/video-poker/strategy/jacks-or-better/9-6/optimal/

• Jay

So has anybody been to Atlantic City in the past year to confirm all this? I just played on the link above, and they invariably coach: hold the pair and go from there!
It’s quite a fun game.

• â€śHold Only That Pair of 2s?â€ť Studying a Video Poker Hand with R - R Project Aggregate

[…] Ryan Rosario Whenever I tell people in my family that I study Statistics, one of the first questions I get from […]

• Barack Obama

The other question is

“dump the 22 and keep the J”

• Joseph

Would you mind adding th option to hold the Jack alone, and throw in the awesomest 53rd cars, Mr. Joker, please? Iâ€™m at Harrahâ€™s and find when I hold a low pair when itâ€™s ACES or better, the following cards have about a 60% chance of having an Ace, if not a Joker and Ace (very rare).

Have you considered doing one for duces wild? I need to study the strategies for playing that. Any tips? IE, when youâ€™re dealt two pair, do you go for the full House, or ditch a pair, hoping for four or five of a kind?

Thanks for this!

Joseph