Exercise: Analyzing Poker Hand Outcomes

Exercise: Analyzing Poker Hand Outcomes#

Let’s explore a question: how can we best visualize the likely outcomes of a poker hand given that we haven’t been dealt all the cards yet? In this case, we start with a set of cards and then deal out the remaining 7 cards to complete the hand and determine the highest rank of each hand that results. Let’s create a function that plots the counts of each set of resulting hands by rank so that we can compare each partial hand’s strategic value. This could be very useful in terms of determining how likely a winning hand was from a particular staring point.

Let’s familiarize ourselves with the data. Our virtual deck of cards consists of cards with 13 possible values: 2,3,4,5,6,7,8,9,0,J,Q,K,A. Here 0 represents the value 10, and Jack, Queen, King, and Ace are represented as J,Q,K, and A, respectively. The four possible suits are represented as s (spades), c (clubs), d, diamonds, and h (hearts). The number of starting cards in the hands could vary (we may have been dealt 1 card, 2 cards, 3 cards, etc.), but the full hand consists of 7 cards. After being dealt, each hand is evaluated for the highest hand rank it contains ranging from the lowest (high card) to the highest (straight flush).

(1) Begin by loading and viewing the raw data which you can find in the file “poker_results.csv”. You’ll notice that this is essentially the content of a histogram of sorts where instead of bins with ranges of values, we have hand ranks.

(2) Next, create a function to plot a single row of the above dataset as a histogram. This plot should take as inputs the Axes object on which to plot the histogram, the counts for the highest rank for each starting hand (which is provided in the data), and the names of each of the hands, hist_rank(ax, counts, hand_names)

You may need to rotate your x tick labels to be able to read them clearly. This can be done using the ax.set_xticklabels(ax.get_xticklabels(), rotation=90) construction assuming that ax represents the Axes object you’re plotting on.

(3) Test out your function on our data. To do this, choose the ‘Ah’ (ace of hearts) partial starting hand and plot the histogram of the highest ranks resulting from that ace of hearts with the function you just created. Make sure to label your x- and y-axis labels and set the title of your plot to be the partial starting hand (‘Ah’).

(4) Take a look at the resulting distribution of outcomes. Verify that your plot is working correctly given the raw data.

(5) Looking at the data, how likely is a high ranking hand (three of a kind or better) starting with the ace of hearts?

(6) Now, let’s compare all of the starting hands that we have to see how they shake out to see if there are any trends that can be seen across the different sets of starting cards. To do this, construct a 3-by-3 subplot matrix of histograms, one for each set of starting cards, titling each with the starting cards themselves. Note: if your plots overlap one another adjust the figsize property of subplots; we recommend 10 inches wide by 8 inches tall. And if you’re finding there’s overlap between the figures, try fig.tight_layout(), which is generally a good idea to use at the end of most subplot plots since it makes the most of available space without overlapping plot components.

(7) Now, lets make some tweaks to this plot to make it more professional and easier to read. The current plot, with so many redundant x tick labels is very hard to read, but we can fix this:

  1. Set the x and y axis limits to be the same for all subplots (while there are a couple of ways of doing this, one way is to use the sharex and sharey keyword parameters for subplots)

  2. Remove the space between subplots to waste less space. You can do this with the ax.subplots_adjust() method.

  3. Add axis labels to ONLY the left-most axes for the y-label and bottom-most for the x-label to keep the plot easier to read

  4. The titles probably overlap with other content. Instead of using a title, add text to the upper right of the plot with the hand.

The type of plot above is called a small multiple, where the axes are the same for all the plots allowing for easier direct comparison across each of the plots and are very helpful for visualizing data with shared properties.

(8) Compare how starting with each subset of cards affects possible hand ranks with all seven cards. Which starting subsets appear to be the most advantageous? Which seems to be the least advantageous?