Showing posts with label poker. Show all posts
Showing posts with label poker. Show all posts

Wednesday, February 17, 2016

Blackjack: A Paradox

Introduction

A friend of mine asked me to simulate blackjack deals to verify what he believed would be a new winning strategy in blackjack.  If you don't know the rules of blackjack, see here for the Wikipedia page.  The basic idea he had was that, assuming the dealer hits on 16 or below and stays on 17 or above, the face up cards that lead to a dealer bust should be different from what is typically assumed.

The typical assumption is that the next card is always a 10 in value, as 10s are the most common point value (16/52 are worth 10 points).  Assuming this and the dealer rules, we would expect the dealer to bust the most on face up 2s through 6s.  Why?  Because if a dealer gets a 6 face up, we believe him to have a 10 face down, which adds to 16, which means he has to hit.  But getting another 10 gives him 26 which is a bust.  Similarly down to 2 (2->12->22 = bust).

However, while it is true that 10 is the most common point value for a card, the average point value in the deck is about 7 (higher or lower depending on if the Ace is an 11 or 1 in any hand).  So we should expect any hidden cards to average about 7 points.  But if this is true, then the dealer should bust with a different set of face up cards.  For instance, if we assume the average to be 7, then we would expect 2, 8, and 9 to be the bust cards (2->9->16->23; 8->15->22; 9->16->23).  If you choose a different average calculation, you can get a different set of face up bust cards.

So which is it?  As it turns out, the original basic strategy of assuming the next card is a 10 is the correct approach.  But why?  This leads to the following paradox as both approaches seem intuitively correct:

The paradox: If the average value of a card in blackjack is about 7, why is the basic winning strategy to assume that the next card is a 10?

Data Exploration

The dealer busts about 28.6% of the time and stays about 71.4% of the time.  The dealer does in fact bust most frequently on face up 2s-6s.  See the table below (J=11, Q=12, K=13, A=14). This is based on 100,000 simulations.  5s and 6s are highest at about 43% of the time:

Face Up
Busted
Stay
Busted/Total
2
2671
4964
0.350
3
2828
4742
0.374
4
3136
4522
0.410
5
3338
4320
0.436
6
3294
4445
0.426
7
2047
5720
0.264
8
1817
5945
0.234
9
1771
5880
0.231
10
1670
6005
0.218
11
1623
6106
0.210
12
1679
5950
0.220
13
1716
6002
0.222
14
1012
6797
0.130

So the original basic strategy is correct that the bust cards are 2-6.  And there is a significant break between 6 and 7.  But why is this the case?  Let's do some exploring.

 Averages:

The average value of the cards in a dealer's hand when he busts is 7.03.  When he stays, its  7.61.  Breaking this down by face up card, we have the below table:

Face Up
Busted
Stay
Difference
2
6.076
5.548
0.529
3
6.383
5.696
0.688
4
6.726
5.814
0.911
5
7.052
5.877
1.175
6
7.500
6.350
1.150
7
7.162
7.380
-0.217
8
7.283
7.878
-0.596
9
7.467
8.359
-0.892
10
7.579
8.869
-1.290
11
7.597
8.878
-1.281
12
7.570
8.869
-1.299
13
7.586
8.888
-1.302
14
5.419
8.504
-3.085


Interestingly, the bust averages for 2-6 are higher than the stay averages, but the opposite is true for 7-14.  Why is this the case?  Perhaps another table of the average count of cards will help:

Face Up
Busted
Stay
Difference
2
3.946
3.517
0.429
3
3.783
3.422
0.361
4
3.606
3.338
0.269
5
3.478
3.284
0.194
6
3.296
3.047
0.249
7
3.399
2.580
0.819
8
3.317
2.467
0.850
9
3.233
2.366
0.867
10
3.174
2.257
0.917
11
3.169
2.255
0.915
12
3.184
2.256
0.928
13
3.172
2.255
0.917
14
4.454
2.520
1.934


Busts on average require more cards than stays, which makes sense, because a bust goes over 21 while a stay is between 17-21.  On average, it takes more cards to get a higher value, hence, getting over 21 takes more cards than getting between 17 and 21.

However, there is a big break in average counts of cards between face up 2s-6s and face up 7s-14s that matches the break in average values of cards.  Why should this exist?

If we start with the assumption that the next card (or any hidden card) is a 10, then this is to be expected.  With that assumption, a face up 6 or below is a total of 16 or below (hit) while a 7 face up is a total of 17 (stay).  A hit requires one more card at least, so it makes sense that a large break occurs between 6 and 7. 

Also,  the higher the face up card, the more likely a stay is with only 2 cards, meaning that each card is on average larger than a similar stay with 3 cards.  For example, a stay of 17 with a 10 and 7 averages 8.5 per card while a stay of 17 with an 8, 2, and 7 averages 5.7 per card.  As the face up card gets smaller, the amount of cards needed to get to a stay or bust increases, since the cards are worth less, hence, the averages get smaller.

But this isn't the whole story.  The first table really comes from the combination of the second table and another table, the average hand value for the dealer by face up card.  We can see that the busted averages are largely the same with the highest value at 6.  The stay averages are also similar with a notable low value at 7.  The difference between the busted total value and the stay total value shows a peak of 5.6 at a face up value of 7, and this decreases to either side.

This makes sense, since a 7 face up (and a hidden 10) is the lowest possible stay, while a 6 face up (with a hidden 10) is the highest hit.  So we should expect the greatest difference between busts and stays to be right around the 6 and 7 divide.

Face Up
Busted
Stay
Difference
2
23.325
18.917
4.408
3
23.403
18.974
4.429
4
23.570
18.967
4.603
5
23.843
18.920
4.923
6
24.159
18.761
5.398
7
23.740
18.100
5.640
8
23.692
18.466
5.226
9
23.732
18.865
4.867
10
23.765
19.263
4.502
11
23.792
19.276
4.516
12
23.783
19.261
4.522
13
23.784
19.276
4.507
14
23.712
19.528
4.185


So a large difference in the middle combined with decreasing average counts of cards in the dealers hand makes for a switch in the difference of average card values at the 6/7 divide.  And all of this makes sense when we expect the next (or hidden) card to have a value of 10.

Counts of 10s:

Is assuming that the next (or hidden) card is a 10 in value a good assumption? In a way, yes.  71% of dealer hands have a 10 in them.  Thus, it is reasonable to assume that the dealer will have a 10 face down or at least coming as a third card.  That is, most of the time, the dealer will have a 10 in the hand.

Here is a more detailed breakdown:

Count of 10s
Busted
Stay
0
4154
24520
1
15002
37876
2
9446
9002

The dealer busts only 4% of the time without any 10s.  That is, only 15% of busts lack a 10 valued card.  That means that 85% of busts have at least one 10 involved.  With stays, only 34% of them lack a 10. 

If we remove the possibility of getting a 10 valued card as the face up, then 59% of hands will still have a 10 valued card hidden or coming.  In greater detail:


Count of 10s
Busted
Stay
0
4154
24520
1
12587
22815
2
5173
0

81% of these busts have a 10 involved.  Stays are split nearly 50-50 on having a 10 or not.  When a 10 is involved, 43% of the time the dealer busts.

Clearly, one should expect the dealer to get a 10 valued card.

Combining Averages with 10s

Suppose the dealer gets one 10 valued card (not the face up), which happens 51% of the time.  What happens then?
  • Let's assume the other card besides the face up card is a 7 (the average).  If the 10 comes first, then a bust comes on face up 5 and 6, which are the most likely to bust.  If the 10 comes second, then 5-9 are busts.
  • If the other card is an 8 (the median), a bust happens on 4-6 with the 10 coming first, and 4-8 when it comes second.
  • If the other card is a 10 (the mode) (7% of the time), then a bust happens on 2-6.
So we see that busting on 5-6 is very likely in all circumstances, and then depending on what we take to be the best representative of the set (mean vs. median vs. mode), additional face up values lead to busting.

When we combine all of these together, it is not surprising that we do get the following result of bust averages by face up value, now ordered from most likely to least:

Face Up
Busted
Stay
Busted/Total
5
3338
4320
0.436
6
3294
4445
0.426
4
3136
4522
0.410
3
2828
4742
0.374
2
2671
4964
0.350
7
2047
5720
0.264
8
1817
5945
0.234
9
1771
5880
0.231
13
1716
6002
0.222
12
1679
5950
0.220
10
1670
6005
0.218
11
1623
6106
0.210
14
1012
6797
0.130

Still, if we had to pick one number to represent the set of values, it seems that 10 (the mode) is the best number to do so, and not the mean or the median.  This best aligns with the face up value busts.  But why?

Resolving the Paradox: A Statistical Lesson

If assuming that a hidden card is a 10 in value is the right approach, why is assuming that the next card is a 7 in value a bad approach?  In other words, why does the mode take priority over the mean in representing the distribution of values of cards in blackjack?

The answer lies in the distribution of values in blackjack.  A hidden assumption in my friend's approach is that the average value of the cards was from an approximately normal distribution.  If that were the case, then we could expect the next card to be, on average, a 7, because in fact, the average card would be about a 7.

However, blackjack values are not normally distributed.  There are 4 times as many 10s as there are of any other card value.  As a result, using the average value of the cards to represent any hidden card values is misleading.  This is a misuse of the mean to represent the sample.



To make this point more obvious, suppose we played a game in which every card was worth 200 points except for the 2 of clubs and 2 of spades, which were both worth -5000 points.  The average value of the cards in this set is 0 points.  However, most of the time, the next card played will be worth 200 points. And no card is in fact worth 0 points.




In this circumstance, it makes sense to treat every next card as being worth 200 points, knowing full well that 2 of the cards will be worth much less.  Why?  Because in fact, most of the time, the next card will be 200 points.  The best strategy for winning the game will likely assume that every hidden card is worth 200 points.

Conclusion

Going back to blackjack, and given the foregoing, the best simple assumption does seem to be that any hidden card is a 10 in value.  If we make this a bit more complex, the assumption becomes that the dealer will get at least one 10 valued card.  So if this is not the face up card, we should expect it to be the face down card or a hit card.  This, combined with what we have discovered about the mean and median, leads us view face up cards 2-6 as bust cards, with 5s and 6s being especially important.

The statistical lesson to be learned is that one must be careful in using the mean (or median or mode) to represent the dataset.  Do a sanity check.  Look at the data.  Does it make sense, or does it mischaracterize the data?  Using the word "expected" helps.  Does the mean represent what I expect any unknown value to be, or is the mode or something else a better representation of what is expected? 

Context is also important.  Saying that the average value of a data set is X really does not tell us much about that dataset.  Putting that into the context of the distribution, standard deviation, skew, mode, median, and other various statistical measures and descriptions can help us understand what the data is really like, and how we can best use that data to make good decisions.

It has been said that there are three kinds of lies: "lies, damned lies, and statistics."  Make sure your statistics do not misrepresent the true nature of the data and become worse than damned lies.

Wednesday, September 9, 2015

Hold'em or Fold'em: Validating a Winning Strategy

Introduction

In previous posts (here and here) I analyzed simulated hold'em poker hands to determine a winning strategy.  Bluffing aside, when should you play and when should you fold in order to have the best chances of winning?

The observations I made do not readily yield a single strategy for play.  They may sometimes conflict or be overly conservative when combined.  For example, my advice for hole cards may offer a more than 50% chance of winning on its own, but when combined with the advice on hand rankings (which also offers a better than 50% chance of winning) it may require the folding of many winning hands.

Still, we need a place to start.  Perhaps the principles I generated can be condensed, simplified, reorganized, and summed up in this way:

A Strategy

  • Hole cards
    • If your hole cards form a pair, contain an Ace, or have a rounded average rank greater than or equal to 10...
      • Continue playing
    • Else
      • Fold
  • Flop, Turn, River
    • If you have two pair 5s or better, one pair Queens or better, or three of a kind or better
      • Continue playing
    • Else
      • Fold
Will this strategy work?  Let's validate it by simulating a game against one other player.

The Program

The program remains largely the same as before, except that I have now implemented the above logic into the program.  If one folds on the hole cards, then this is tracked as "Fold - hole cards".  If one passes this test but fails on the flop, turn, or river, this is tracked as "Fold - Flop, Turn, River".  If one passes both tests, then this is tracked as "Play".

I reran 100,000 simulations of hold'em deals to Player1 and Player2.  A successful strategy will have a high percentage of Win/Play combos along with a high percentage of Lose/Fold combos.  That is, we want to play when we will win, and fold when we will lose.  If our strategy is not successful, then there will be a high percentage of Win/Fold combos (we folded when we would have won the hand) and Lose/Play combos (we played when we would have lost).

In other words, the ideal simplified scenario can be represented by the below confusion matrix:
Play
Fold
Win
50
0
Lose
0
50

In the above scenario, a win is as equally likely as a loss, so each occurs 50% of the time, adding to 100%.  However, we have predicted perfectly, so we won all the hands we have played, and lost only the hands we have folded.  Assuming we have been betting well (betting minimally with hands we will fold, betting maximally with hands we will win), the eventual result will be that we win the game and all of the money.

What does the actual confusion matrix look like?

Validation

The results of implementing this strategy are below:
Player1 Result/Action
Fold - Flop, Turn, River
Fold - hole cards
Play
Grand Total
Lose
10773
32593
4548
47914
Tie
326
3044
816
4186
Win
7158
24708
16034
47900
Grand Total
18257
60345
21398
100000


The table above gives the final result for Player1's hand (left side) and Player1's action based on the simplified principles we gave him (top).  The combinations are displayed in the middle, with row/column totals on the ends.

A simplified version of the table:
Player1 Result/Action
Fold
Play
Grand Total
Lose
43366
4548
47914
Tie
3370
816
4186
Win
31866
16034
47900
Grand Total
78602
21398
100000


Some observations:
  • As we would expect, Player1's hands lose and win about equally
  • Player1 wins about 75% of the hands he plays
  • Player1 only plays 21% of the hands
  • Of all the hands Player1 would have lost, he only plays 9% of them
  • Of all the hands Player1 would have won, he only plays 33% of them
  • 40% of Player1's folds were winning hands
  • Player1 only plays 19% of ties.

Strengths of this approach:
  • Player1 plays very few of the games he will lose
  • The strategy is simple
  • The strategy relies on information known at the time of decision

Weakness of this Approach
  • Player1 folds quite a few games in which, if he had played, he would have won
  • Player1 folds most of the games in which there is a tie (and would split the pot)
  • Player1 plays very few games

This is definitely a conservative strategy.  The result of combining the various insights we have gathered in previous posts is too cautious.  Player1 only plays when there is a great chance of winning.  Perhaps with good betting and bluffing, this can be a very effective strategy.  However, it folds a lot of winning hands, in fact, most winning hands. 

The strategy misclassifies (ignoring ties) 38% of all hands.  Counting ties (assuming you should play ties), it misclassifies 40% of all hands, that is, 40% of the time it recommends a fold when you should play and recommends that you play when you should fold.

Is there a better way?  Most certainly.  Let's use machine learning to analyze our data and come up with a more effective model.

Machine Learning: Decision Tree

I use a decision tree to develop a model for hold'em strategy because it is easy to understand the decision paths.  Here is a plot of the model along with the associated splits and ratios of classification:



The details may be difficult to see, so here is the decision tree strategy with words:
  • Flop, Turn, River
    • If Player1 has two pair or better
      • Play
    • Else (Player1 has one pair or high card)
      • If Player1 has a high card hand
        • Fold
      • Else (if Player1 has one pair)
        • If both of the cards that make up the one pair are in the community cards
          • Fold
        • Else (if only 0 or 1 of the cards that make up the one pair are in the community cards)
          • Play

This model has only 27% misclassification, that is, it predicts a loss when you really win or predicts a win when you really lose, about 27% of the time.  The confusion matrix is below for play/fold and win/lose/tie combinations:

Player1 Result/Action
Fold
Play
Grand Total
Lose
29591
18323
47914
Tie
1452
2734
4186
Win
7429
40471
47900
Grand Total
38472
61528
100000


Some observations:
  • Player1 wins about 66% of the hands he plays
  • Player1 plays 62% of the hands
  • Of all the hands Player1 would have lost, he plays 39% of them
  • Of all the hands Player1 would have won, he plays 84% of them
  • Only 19% of Player1's folds were winning hands
  • Player1 plays 65% of ties.

Strengths of this approach:
  • Most folds are losses; most plays are wins
  • The strategy is simple

Weakness of this Approach
  • The strategy relies on information not known at time of decision (at least before flop)
  • Player1 plays too many hands (in particular, too many losing hands)
This strategy is much more effective.  However, it offers no guidance for the hole cards, recommending that one should always play at least through the flop.

Do the hole cards really matter in an overall strategy?  Looking at another decision tree using only the hole card information (max rank, min rank, average rank, pair (yes/no)) reveals that, at best, the model will still misclassify about 43% of the time.  A simple split based on the average rank of the hole cards being >= 7.5 yields only a slight advantage in winning (54%) and correctly classifies only 45% of the time. 

If we force the previous decision to use the hole card information to its full extent, the model becomes incredibly complex (over 1000 nodes) and only improves to about 26% misclassification.  The decision tree looks like this:



Clearly, this isn't helpful.  But we know hole cards are important to some degree, so shouldn't this count for something?  In addition, this strategy plays too many hands, and as a result, plays too many losing hands.

A Compromise


What should we do now?  If we compare the first strategy, we see that the major problem is that one folds too much, particularly, on hands that would have been won (the lower left corner green box).  It plays too little.  In contrast, the decision tree model plays too much, in particular, it plays too many hands that it will lose (the upper right yellow box).

Is there a way to combine both strategies in a way that is more effective than either, is still simple, plays about 50% of the time, and provides more guidance at each stage of the game?  I combined both approaches and played with the logic until coming up with a more balanced strategy:
  • Hole Cards
    • If the rounded average rank of the hole cards is >= 6 or the hole cards form a pair
      • Continue Playing
    • Else
      • Fold
  • Flop, Turn, River
    • If you have two pair or better
      • Continue Playing
    • Else (one pair or high card)
      • If one pair, only 0 or 1 of the cards that make up the one pair are in the community cards, and the pair is 5s or better
        • Continue playing
      • Else (high card, one pair of 4s or worse, or one pair with both pair cards in the community)
        • Fold
The confusion matrix is as follows (I validated the approach against a test set and the results were virtually the same):

Player1 Result/Action
Fold
Play
Grand Total
Lose
35010
12904
47914
Tie
2005
2181
4186
Win
13500
34400
47900
Grand Total
50515
49485
100000


Some observations:
  • Player1 wins about 70% of the hands he plays
  • Player1 plays 49% of the hands
  • Of all the hands Player1 would have lost, he only plays 27% of them
  • Of all the hands Player1 would have won, he plays 72% of them
  • 26% of Player1's folds were winning hands
  • Player1 plays 52% of ties.
  • The strategy misclassifies 28% of the time.

Strengths of this approach:
  • Most plays are wins; most folds are losses
  • The strategy is still fairly simple
  • The strategy relies more on information known at the time of decision
  • Player1 plays about half the time
  • The misclassification is low.

This approach does not suffer the weakness of the other two approaches, but represents a balanced compromise between them. While the initial strategy is too conservative, the decision tree strategy is too liberal.  This compromise still has good predictive power, is simple, and useful, and represents a middle ground strategy.

Conclusion

As the foregoing shows, there is no single master strategy for winning at poker.  Any strategy will have strengths and weaknesses.  And different strategies may, for this reason, appeal to different people.  What I have shown above are some data driven strategies designed to maximize strengths and minimize weaknesses, but how they do so differs.  Feel free to choose the strategy (or modified suitably) that appeals to you.

This concludes my analysis of poker hands.  For now at least....

Good luck!