Skip to main content
HomeAbout PythonLearn Python

Poker Probability and Statistics with Python

Tackle probability and statistics in Python: learn more about combinations and permutations, dependent and independent events, and expected value.
Sep 2017  · 29 min read

Data scientists create machine learning models to make predictions and optimize decisions. In online poker, the options are whether to bet, call, or fold. You aren't allowed to use software to make those decisions though. That's where most online poker sites draw the line in the rules. Since you can't train a machine learning model, you must train your brain. This requires an endless stream of equity calculations away from the poker table, which use many different probability and statistics concepts.

In this tutorial, you'll learn some of these concepts using a deck of cards and generic poker situations.

Poker Card Banner

If you're interested in tackling statistics with Python, consider DataCamp's Statistical Thinking in Python course.

Personal Motivation

For several years, I made a living playing online poker professionally. Data science was a natural progression for me as it requires a similar skill-set as earning a profit from online poker. I wrote a blog about what data science has in common with poker, and I mentioned that each time a poker hand is played at an online poker site, a hand history is generated. These hand histories explain everything that each player did during that hand. I used software called Hold’em Manager (think Tableau for poker) to take advantage of this data. Hold'em Manager downloads each of these hand histories in real-time to a PostgreSQL database so you can keep track of your opponent’s tendencies.

Probability Theory: An Introduction

Before you get your hands dirty, it's time to consider what probability theory is and why it's important to learn about it when you're getting into data science. Additionally, you'll learn some key concepts that will be handy to consider throughout the tutorial and you'll learn how to calculate the probability of single events.

You'll often wonder in real-life situations what the probabilities are of some event occurring, such as winning the lottery, the victory of your soccer team or a discount on your favorite pair of shoes. "What are the chances..." is an expression you probably use very often. Determining the chances of an event occurring is called "probability".

This type of probability is different from the mathematical way of looking at probability, which you can find in probability theory, a branch of mathematics. And in mathematics, you have two broad categories of interpretations on "probability" is - the "physical" and "evidential" probabilities.

The former are also called objective or frequency probabilities and are associated with random physical systems such as flipping coins, roulette wheels, or rolling dice. In such systems, a given type of event tends to occur at a persistent rate, or "relative frequency", in a long run of trials.

The latter is also called Bayesian probability, which can be assigned to any statement whatsoever, even when no random process is involved, as a way to represent its subjective plausibility, or the degree to which the statement is supported by the available evidence. On most accounts, evidential probabilities are considered to be degrees of belief, defined in terms of dispositions to gamble at certain odds.

Note that probability theory is mainly concerned with predicting the likelihood of future events, while statistics analyzes the frequency of past events. This also explains why probability theory is also one of the core topics that you should cover if you want to become a data scientist: as you well know, in data science and machine learning, you'll use data from events that have already occurred to predict future events.

So while probability theory is generally considered to be hard to understand intuitively, the concepts are crucial to data science and predictive analytics.

Key Concepts and Symbols

Some key concepts that you should probably be aware of for this tutorial are mostly concerned with the frequentist perspective on probability, as this tutorial works with a deck of 52 playing cards.

From that perspective, the fundamental ingredient of probability theory is an experiment that can be repeated, at least hypothetically, under essentially identical conditions. This experiment may lead to different outcomes on different trials or single performances of an experiment. The set of all possible outcomes or results of an experiment is then called a "sample space". An event is a well-defined subset of the sample space.

This is all very theoretical.

Let's consider some examples:

  • A somewhat cliché example would be flipping a coin. In this case, the experiment is, in fact, the flipping of a coin. You can toss the coin multiple times, and all these trials might have different outcomes. As there are two possible outcomes -heads or tails- the sample space is 2. However, the event "tossing a coin" can, for example, consist of one outcome "Heads". Similarly, when you toss a coin twice, your event "the first toss results in a Heads" might have an outcome "Heads-Heads" or "Heads-Tails".
  • Another example that is maybe less straightforward is an experiment where you spin a globe and you stop it by putting your finger on it. You can spin the globe multiple times and all these times might have different outcomes - You can either land your finger on land or on water. That means that the sample space is 2. An event "my finger is on land" might have an outcome "Land - Water" or "Land - Land".
  • A last example is the experiment where you toss a die. You can toss the die multiple times and all of these throws can have different outcomes: 6 to be exact, since your die has 6 numbers (1,2,3,4,5,6). An event "The sum of the results of the two toss is equal to 10" can consist of 10, while the event "the number is even" can consist of 2, 4, or 6.

Now that you have an idea of the key concepts that you'll be using throughout this tutorial, it's time to also consider some probability symbols that you will also encounter:

Symbol Meaning
$$\cap$$ And
$$\cup$$ Or
| Given

Calculating Probability For Single Events

Now that you're completely up to date, you can start to determine the probability of a single event happenings, such as a coin landing on tails. To calculate this probability, you divide the number of possible event outcomes by the sample space.

This means that you have to consider first how many possible ways there are for the coin to land on tails, and the number of possible outcomes. The former is 1, as you have only one possible way to get tails. The latter is 2, as you will either get heads or tails when you flip the coin.

To summarize, the calculation of the probability of an event A will look something like this:

$$P(A) = \frac{Event \ outcomes \ favorable \ to \ A}{Sample \ space}$$

In the case of the coin flipping, the probability of the coin landing on tails is 1/2 or 0.5.

Note how the probability is always between 0 and 1, where 0 indicates that it's not very probable that the event will happen, where 1 indicates that it's probable that the event will happen.

Now let's consider a second example in which you'll calculate the probability of an event.

Statistics Sample Space for Deck of Cards

There are 52 cards In a standard deck of cards and of those 52 cards, 4 are Aces. If you follow the example of the coin flipping from above to know the probability of drawing an Ace, you'll divide the number of possible event outcomes (4), by the sample space (52):

$$P(A) = \frac{4}{52}$$

Note how $A$ represents the event of "drawing an Ace".

Now, determine the probability of drawing an Ace with the help of Python:

# Sample Space
cards = 52

# Outcomes
aces = 4

# Divide possible outcomes by the sample set
ace_probability = aces / cards

# Print probability rounded to two decimal places
print(round(ace_probability, 2))
0.08

The probability of drawing an Ace from a standard deck is 0.08. To determine probability in percentage form, simply multiply by 100.

# Ace Probability Percent Code
ace_probability_percent = ace_probability * 100

# Print probability percent rounded to one decimal place
print(str(round(ace_probability_percent, 0)) + '%')
8.0%

The probability of drawing an Ace as a percent is 8%.

Now that you have seen two examples where you calculated probabilities, it's easy to assume that you might build out your probability calculations to determine, for example, the probability of drawing a card that is a Heart, a face card (such as Jacks, Queens, or Kings), or a combination of both, such as a Queen of Hearts.

In such cases, you might want to create a User-Defined Function (UDF) event_probability() to which you pass the event_outcomes and the sample_space to find the probability of an event in percentage form, since you'll be reusing a lot of the code:

# Create function that returns probability percent rounded to one decimal place
def event_probability(event_outcomes, sample_space):
    probability = (event_outcomes / sample_space) * 100
    return round(probability, 1)

# Sample Space
cards = 52

# Determine the probability of drawing a heart
hearts = 13
heart_probability = event_probability(hearts, cards)

# Determine the probability of drawing a face card
face_cards = 12
face_card_probability = event_probability(face_cards, cards)

# Determine the probability of drawing the queen of hearts
queen_of_hearts = 1
queen_of_hearts_probability = event_probability(queen_of_hearts, cards)

# Print each probability
print(str(heart_probability) + '%')
print(str(face_card_probability) + '%')
print(str(queen_of_hearts_probability) + '%')
25.0%
23.1%
1.9%

These results probably don't surprise you: as you expected, the chances of drawing a Queen of Hearts are much smaller than the chances of drawing a regular face card or a Heart.

Probability with Combinations and Permutations

You have seen in the previous section that determining the size of your sample space is key to calculating probabilities. However, this can sometimes prove to be a challenge!

Fortunately, there are ways to make the counting task easier. Two of these ways are permutations and combinations. In this section, you'll see what both of these concepts exactly mean and how you can use them to calculate the size of your sample space!

Permutations

Permutations are the number of ways a subset of a specified size can be arranged from a given set, generally without replacement. An example of this would be a 4 digit PIN with no repeated digits. The probability of having no repeated digits can be calculated by executing the following calculation:

$$10 \times 9 \times 8 \times 7$$.

You have 10 numbers to choose from, but as you're working without replacement, one option always falls away as you pick a number for the 4-digit pin. This means that in picking the first number for your pin, you'll have 10 numbers to choose from (0 to 9), but for the second number of your pin, you'll only have 9 options to choose from, etc.

On a higher level, you see that the previous paragraph actually considers two things: (1) the numbers to choose from, and (2) the numbers that you actually choose. In the example above, 10 is the number of digits that you can choose from, as you consider all numbers between 0 and 9. However, the actual number of things that you choose is 4, since you have a 4-digit pin.

When calculating the permutations, this means that you consider the full set of the numbers to choose from, which is in reality $$10 \times 9 \times 8 \times 7 \times 6 \times 5 \times 4 \times 3 \times 2 \times 1$$ and you divide the result of this calculation by the difference in the numbers to choose from (10) and the numbers that you actually choose (4). Since you're considering probabilities, this means that this difference will be $$6 \times 5 \times 4 \times 3 \times 2 \times 1$$.

Note that you can also write the above as

$$10P4 = \frac{10!}{(10 - 4)!}$$

You'll notice that there is an "10!" and "6!" or "10 factorial" and "6 factorial" in the equation, which is used to indicate that all the consecutive positive integers from 1 up to and including 10 or 6 are to be multiplied together.

The result of this calculation is 5040 permutations. Note how this is exactly the same as the calculation that you made above, when you multiplied 10, 9, 8 and 7.

Generalizing the calculations above, this means that the formula to calculate permutations is the following:

$$nPk = \frac{n!}{(n - k)!}$$

Let's practice this with an example!

To find the number of permutations of pocket Aces, from which you only pick 2, you'll consider the full set of aces to choose from (4) and you also consider the number of aces that you actually choose (2):

$$4P2 = \frac{4!}{(4 - 2)!}$$

Combinations

You have seen that when you're working with permutations, the order matters. With combinations, however, this isn't the case: the order doesn't matter. Combinations refers to the number of ways a subset of a specified size can be drawn from a given set.

An example here is the following situation where you have your deck of cards, which consists of 52 cards. Three cards are going to be taken out of the deck. How many different ways can you choose these three cards?

In fact, this should be $$52 \times 51 \times 50$$, which is actually the same as the permutations formula that you have just used! However, with combinations, you don't take the order into account. This means that if you want to figure out how many combinations you actually have, you just create all the permutations and divide by all the redundancies or $$3 \times 2 \times 1$$.

This means that your calculation of the combinations will look like this:

$$52C3 = \frac{\frac {52!}{(52-3)!}}{3!}$$

This calculation can be generalized to the following formula:

$$nCk = \frac{nPk}{k!}$$

Where you clearly see that the numerator is exactly the same formula as the permutations formula that you have just seen, while the denominator is the factorial of the number of cards that you will actually choose.

Consider another example with Aces. There are four Aces in a deck of cards, and these are all the different combinations of pocket Aces;

  1. Ace Hearts / Ace Diamonds
  2. Ace Hearts / Ace Clubs
  3. Ace Hearts / Ace Spades
  4. Ace Diamonds / Ace Clubs
  5. Ace Diamonds / Ace Spades
  6. Ace Clubs / Ace Spades

There are six combinations of pocket Aces. To find the number of combinations, you first must find the number of permutations:

# Permutations Code
import math
n = 4
k = 2

# Determine permutations and print result
Permutations = math.factorial(n) / math.factorial(k)
print(Permutations)
12.0

To determine the number of combinations, simply divide the number of permutations by the factorial of the size of the subset. Try finding the number of starting hand combinations that can be dealt in Texas Hold’em.

$$52C2 = \frac{52P2}{2!}$$

# Combinations Code
n = 52
k = 2

# Determine Permutations
Permutations = math.factorial(n) / math.factorial(n - k)

# Determine Combinations and print result
Combinations = Permutations / math.factorial(k)
print(Combinations)
1326.0

Independent versus Dependent Events

You have read in the introduction that an event is a well-defined subset of the sample space. Events can be classified into two categories: dependent or independent.

Independent events are events that don't impact the probability of the other event(s). Two events A and B are independent if knowing whether event A occurred gives no information about whether event B occurred.

This is true when you, for example, draw an Ace from the deck, replace the card, shuffle the deck, and then drawing another card. The probability of drawing an Ace the first draw is the same as the second.

Dependent events, then, are events that have an impact on the probability of the other event(s).

For example, you draw a card from the deck and then draw a second card from the deck without replacing the first card. In this case, the probability of drawing an Ace the fist draw is not the same as the probability of drawing an Ace on the second draw. After the first card is drawn, the sample space has reduced by 1, from 52 to 51. Depending on what the card was on the first draw, the number of event outcomes may have also changed. If the card was an Ace, there are now only 3 Aces remaining for the second draw.

Let's consider these definitions in formal terms now. Events A and B (which have nonzero probability) are independent if and only if one of the following equivalent statements holds: $$P (A ∩ B) = P(A)P(B)$$ $$P (A|B) = P(A)$$ $$P (B|A) = P(B)$$

Or, in other words, events A and B are independent if:

  • The probability of events A and B to occur equals the product of the probabilities of each event occurring.
  • the probability of event A to occur if an event B has already occurred is equal to the probability of an event A to occur.
  • The probability of an event B to occur if an event A has already occurred is the same as the probability of an event B to occur.

Let's consider the following example, where you already know the probability of drawing an Ace on the first draw. Now you need to determine the probability of drawing an Ace on the second draw, if the first card drawn was either a King or an Ace:

# Sample Space
cards = 52
cards_drawn = 1 
cards = cards - cards_drawn 

# Determine the probability of drawing an Ace after drawing a King on the first draw
aces = 4
ace_probability1 = event_probability(aces, cards)

# Determine the probability of drawing an Ace after drawing an Ace on the first draw
aces_drawn = 1
aces = aces - aces_drawn
ace_probability2 = event_probability(aces, cards)

# Print each probability
print(ace_probability1)
print(ace_probability2)
7.8
5.9

There are a few situations common to poker which are relevant to the concept of dependent events.

But before you get started, a little background info is in order. The game is Texas Hold’em. Played with a standard 52 card deck, Texas Hold’em is the most popular of all the poker variations. Each player is dealt two cards to start the hand and will make the best five-card hand possible by using their two cards combined with the five community cards that are dealt throughout the hand. Cards are dealt in four rounds:

  • Pre-Flop: Each player is dealt two cards, known as "hole cards"
  • Flop: Three community cards are dealt
  • Turn: One community card is dealt
  • River: Final community card is dealt

Dependent Events: Flush Draw

Your Hand

cards

Community Cards

cards

Your on the Turn and you have four cards to an Ace high Flush. A Flush is a strong poker hand where all five cards are the same suit. What's the probability that the last community card, known as the River Card, is a Diamond?

# Sample Space
cards = 52
hole_cards = 2
turn_community_cards = 4
cards = cards - (hole_cards + turn_community_cards)

# Outcomes
diamonds = 13
diamonds_drawn = 4
# In poker, cards that complete a draw are known as "outs"
outs = diamonds - diamonds_drawn

#Determine river flush probability
river_flush_probability = event_probability(outs, cards)
print(river_flush_probability)
19.6

There is roughly a 20% chance of hitting your Flush draw on the River. Here’s another one:

Dependent Events: Open-Ended Straight Draw

Your Hand

cards

Community Cards

cards

Your on the Turn and you have an open-ended Straight draw. A Straight is another strong hand where there are five cards in sequential order. The Straight draw is open-ended because any Eight ( 8, 9, 10, Jack, Queen) or any King (9, 10, Jack, Queen, King) will complete the straight.

What's the probability that the River card completes the Straight?

# Sample Space
cards = 52
hole_cards = 2
turn_community_cards = 4
cards = cards - (hole_cards + turn_community_cards)

# Outcomes
eights = 4
kings = 4
outs = eights + kings

# Determine river straight probability
river_straight_probability = event_probability(outs, cards)
print(river_straight_probability)
17.4

There is roughly a 17% chance of hitting your Straight draw on the River.

Multiple Events

Up until now, you considered only one event when you were calculating the probabilities, but what when you are dealing with multiple events?

An example of multiple events is the question "what is the probability of eating three oatmeal cookies followed by a chocolate chip cookie when you eat four cookies out of a cookie jar filled with these two types of cookies?" Eating four cookies is actually four events.

To calculate the probability for multiple events, you basically determine the number of events (4 in this case), you then determine the probability for each event occurring separately and you multiply all of these probabilities to get your final answer. In the example that was described above, this would be 0.5 x 0.5 x 0.5 x 0.5 or 0.0625.

$$P(Event A \cap Event B)=P(Event A) \times P(Event A)$$

Note that in this case, you calculate the probabilities of eating an oatmeal cookie AND another oatmeal cookie AND a third oatmeal, AND a last chocolate chip cookie. When you're considering events that all have to happen, you multiply the probabilities.

For your deck of playing cards, you could ask yourself the question "What is the probability of getting three Hearts when choosing without replacement?". When you sample or choose without replacement, it means that you choose a card but do not put it back, so that your final selection cannot include that same card. In this case, your probability calculation will be the following: 13/52 x 12/51 x 11/50.

Mutually Exclusive Events

When you're working with multiple events, you might also have events that are mutually exclusive or disjoint: they cannot both occur. In such cases, you might want to calculate the probability (or the union) of any of multiple mutually exclusive events occurring. In such cases, you don't multiply probabilities, but you simply add together the probability of each event occurring:

$$P(Event A \cup Event B) = P(Event A) + P(Event B)$$

It's key here to understand that the "OR" component is very important: drawing a heart OR drawing a club are two mutually exclusive events. A heart is a heart and a club is a club. To determine the probability of drawing a heart or drawing a club, add the probability of drawing a heart to the probability of drawing a club.

$$P(Heart \cup Club) = (\frac{13}{52}) + (\frac{13}{52})$$

Now it's time for you to determine the probability of the following mutually exclusive events;

  1. Drawing a heart or drawing a club;
  2. Drawing an ace, a king or a queen.
# Sample Space
cards = 52

# Calculate the probability of drawing a heart or a club
hearts = 13
clubs = 13
heart_or_club = event_probability(hearts, cards) + event_probability(clubs, cards)

# Calculate the probability of drawing an ace, king, or a queen
aces = 4
kings = 4
queens = 4
ace_king_or_queen = event_probability(aces, cards) + event_probability(kings, cards) + event_probability(queens, cards)

print(heart_or_club)
print(ace_king_or_queen)
50.0
23.1

Non-Mutually Exclusive Events

You can imagine that not all events are mutually exclusive: Drawing a heart or drawing an ace are two non-mutually exclusive events. The ace of hearts is both an ace and a heart. When events are not mutually exclusive, you must correct for the overlap.

$$P(Event A \cup Event B) = P(Event A) + P(Event B) - P(EventA \cup EventB)$$

To calculate the probability of drawing a heart or an ace, add the probability of drawing a heart to the probability of drawing an ace and then subtract the probability of drawing the ace of hearts.

$$P(Heart \cup Ace) = (\frac{13}{52}) + (\frac{4}{52}) - (\frac{1}{52})$$

Calculate the probability of the following non mutually exclusive events;

  1. Drawing a heart or an ace;
  2. Drawing a red card or drawing a face card.
# Sample Space
cards = 52

# Calculate the probability of drawing a heart or an ace
hearts = 13
aces = 4
ace_of_hearts = 1
heart_or_ace = event_probability(hearts, cards) + event_probability(aces, cards) - event_probability(ace_of_hearts, cards)

# Calculate the probability of drawing a red card or a face card
red_cards = 26
face_cards = 12
red_face_cards = 6
red_or_face_cards = event_probability(red_cards, cards) + event_probability(face_cards, cards) - event_probability(red_face_cards, cards)

print(round(heart_or_ace, 1))
print(round(red_or_face_cards, 1))
30.8
61.6

Intersection of Independent Events

The probability of the intersection of two independent events is determined by multiplying the probabilities of each event occurring.

$$P(Event A \cap Event B) = P(Event A) \times P(Event B)$$

If you want to know the probability of drawing an Ace from a deck of cards, replacing it, reshuffling the deck, and drawing another Ace, you multiply the probability of drawing and Ace times the probability of drawing an Ace.

$$P(Ace \cap Ace) = (\frac{4}{52}) \times (\frac{4}{52})$$

# Sample Space
cards = 52

# Outcomes
aces = 4

# Probability of one ace
ace_probability = aces / cards

# Probability of two consecutive independant aces 
two_aces_probability = ace_probability * ace_probability

# Two Ace Probability Percent Code
two_ace_probability_percent = two_aces_probability * 100
print(round(two_ace_probability_percent, 1))
0.6

The probability of drawing two Aces in a row, independently, is 0.592%. What if the second event is dependant?

Intersection of Dependent Events

The probability of the intersection of two non independent events (Event A & Event B given A) is determined by multiplying the probability of Event A occurring times the probability of Event B given A.

$$P(Event A \cap Event B | A) = P(Event A) \times P(Event B | A)$$

The best starting hand you can have in Texas Hold’em is pocket Aces. What is the probability of being dealt two Aces?

$$P(Ace \cap Ace | Ace) = (\frac{4}{52}) \times (\frac{3}{51})$$

# Sample Space first draw
cards = 52

# Outcomes first draw
aces = 4

# Probability of ace on first draw
first_ace_probability = aces / cards

# Sample Space second draw
cards = cards - 1

# Outcomes second draw
aces = aces - 1

# Probability of ace on second draw after ace on first
second_ace_probability = aces / cards

# Probability of two consecutive aces (dependent)
both_aces_probability = first_ace_probability * second_ace_probability * 100
print(both_aces_probability)
0.4524886877828055

The probability of drawing two dependent Aces in a row is 0.452%. Let's take a look at a couple situations where this comes into play at the poker table.

Intersection of Dependent Events: Flop Flush Draw

Your Hand

cards

Community Cards

cards

This is a similar situation as the Flush draw above, except this time you're on the flop and have two more community cards to come instead of just one. How can you determine the probability of getting a Flush by the River? First you need to determine all the different possible scenarios;

  • A) Turn Diamond, River Non Diamond (Made Flush)
  • B) Turn Non Diamond, River Diamond (Made Flush)
  • C) Turn Diamond, River Diamond (Made Flush)
  • D) Turn Non Diamond, River Non Diamond (No Flush)

Those are the only four possibilities, and each of those scenarios are mutually exclusive. This means that if you add the probabilities of each of those scenarios occurring, the total will be 1. In other words, one of those four scenarios is definitely going to occur. You want to know the probability of scenario A, B, or C occurring. The simplest approach to figure this out is to determine the probability of scenario D, and subtract that from 1.

# Sample Space on turn
cards = 52
hole_cards = 2
flop_community_cards = 3
cards = cards - (hole_cards + flop_community_cards)

# Outcomes
diamonds = 13
diamonds_drawn = 4
non_diamonds_drawn = 1
outs = diamonds - diamonds_drawn
turn_non_diamonds = cards - outs - non_diamonds_drawn 

# Probability of not getting a diamond on the turn
no_diamond_turn_probability = turn_non_diamonds / cards

# Sample Space on river
turn_community_card = 1
cards = cards - turn_community_card

# Outcomes on river
river_non_diamonds = turn_non_diamonds - turn_community_card

# Probability of not getting a diamond on the river
no_diamond_river_probability = river_non_diamonds / cards

# Probability of not getting a flush
no_flush_probability = no_diamond_turn_probability * no_diamond_river_probability

# Probability of getting a flush
flush_probability = 1 - no_flush_probability
flush_probability_percent = flush_probability * 100

# Print probability percent rounded to one decimal place
print(round(flush_probability_percent, 1))
38.4

Now let's change the hand slightly:

Your Hand

cards

Community Cards

cards

This is a similar situation as the last one, except for one big difference. You still have a flush draw, but this time you don’t have the Ace. If a Diamond falls on the turn and the river, there is a good chance someone will have a better flush draw. Determine the probability of a Diamond falling on the Turn or the River, but not both. You already have the probability of not hitting a flush.

Now find the probability of a diamond falling on the turn and river, add that to the probability of not hitting a flush, and subtract from 1.

# Sample Space on turn
cards = 52
hole_cards = 2
flop_community_cards = 3
cards = cards - (hole_cards + flop_community_cards)

# Outcomes on turn
diamonds = 13
diamonds_drawn = 4
outs = diamonds - diamonds_drawn

# Probability of diamond on turn
diamond_turn_probability = outs / cards

# Sample Space on river
turn_diamonds = 1
cards = cards - turn_diamonds

# Outcomes on river
river_diamonds = outs - turn_diamonds

# Probability of diamond on river
diamond_river_probability = river_diamonds / cards

# Probability of getting a diamond on the turn and river
two_diamonds_probability = diamond_turn_probability * diamond_river_probability

# Determine the probability of getting only one diamond by the river
one_diamond_probability = 1 - (no_flush_probability + two_diamonds_probability)
one_diamond_probability_percent = one_diamond_probability * 100

# Print probability percent rounded to one decimal place
print(round(one_diamond_probability_percent, 1))
35.1

Expected Value

When playing a game such as poker, you're fairly concerned with questions such as "how much do I gain - or lose - on average, if I repeatedly play this game?". You can imagine that this is no different for poker, especially when you're a professional poker player!

Now, if the possible outcomes of the game and their associated probabilities can be described by a random variable, then you can answer the above question by computing its expected value, which is equal to a weighted average of the outcomes where each outcome is weighted by its probability.

Or, in other words, you simply multiply the Total Value times the probability of winning to get your Expected Value:

$$Expected Value = Total Value \times Probability$$

What is the expected value if there is \$100 (Total Value) in the pot, and your probability of winning the pot is 0.75?

$$Expected Value = \$100 \times 0.75$$

# Initialize `pot` and `probability` variables
pot = 100
probability = 0.75

# Determine expected value
expected_value = pot * probability
print(expected_value)
75.0

You're expected value is \$75. Expected value is an important concept in poker. Let’s go back to the first flush example to see how to use expected values to your advantage.

Your Hand

cards

Opponents Hand

cards

Community Cards

cards

  • Total Pot = \$60

  • Opponents Bet = \$20

Your opponent has decided to be helpful and show you his cards, and has a set of 2s. To win the hand on the River, you must hit any Diamond except a Jack or 2. The Jack or 2 of Diamonds would give your opponent a better hand, a full house and four of a kind respectively. You have to call \$20 to stay in the hand, and if you win the hand you win \$60. If your expected value is greater than \$20 you should call the bet, and if not you should fold.

Figure out if you should call the bet:

# Sample Space
cards = 52
hole_cards = 2
# Your opponent provided you information... use it!
opponents_hole_cards = 2 

turn_community_cards = 4
cards = cards - (hole_cards + opponents_hole_cards + turn_community_cards)

# Outcomes
diamonds = 13
diamonds_drawn = 4

# You can't count the two diamonds that won't help you win
diamond_non_outs = 2 

outs = diamonds - diamonds_drawn - diamond_non_outs

# Determine win probability
win_probability = outs / cards

# Determine expected value
pot = 60
ev = pot * win_probability

# Print ev and appropriate decision
call_amount = 20
if ev >= 20:
    print(round(ev, 2), 'Call')
else:
    print(round(ev, 2), 'Fold')
9.55 Fold

Your expected value is only \$9.55, which is less than the \$20 you would need to risk to get that reward, so you must fold. Determining Expected Value is critical to understand at the poker table and Part 2 will dig much deeper into the concept.

Conclusion

Congrats, you have made it to the end of this tutorial on probability theory with Python! This concludes Part 1 of the tutorial. You learned about several core probability concepts including Independent/Dependent events, Permutations/Combinations, Multiple events, Expected Values, and how to calculate each of them.

In Part 2, you will apply these concepts to actual poker hands that I played during my career.

*One card is dealt face down, known as the Burn card, before the Flop, Turn, and River. Since the card is dealt face down, and no player knows what it is, it does not count as a trial.

Check out DataCamp's Probability Distributions in Python Tutorial.

Topics

Learn more about Python

Course

Intermediate Regression with statsmodels in Python

4 hr
8.4K
Learn to perform linear and logistic regression with multiple explanatory variables.
See DetailsRight Arrow
Start Course
Certification available

Course

Introduction to Regression with statsmodels in Python

4 hr
29.2K
Predict housing prices and ad click-through rate by implementing, analyzing, and interpreting regression analysis with statsmodels in Python.
See MoreRight Arrow
Related

A Data Science Roadmap for 2024

Do you want to start or grow in the field of data science? This data science roadmap helps you understand and get started in the data science landscape.
Mark Graus's photo

Mark Graus

10 min

Python NaN: 4 Ways to Check for Missing Values in Python

Explore 4 ways to detect NaN values in Python, using NumPy and Pandas. Learn key differences between NaN and None to clean and analyze data efficiently.
Adel Nehme's photo

Adel Nehme

5 min

Seaborn Heatmaps: A Guide to Data Visualization

Learn how to create eye-catching Seaborn heatmaps
Joleen Bothma's photo

Joleen Bothma

9 min

Test-Driven Development in Python: A Beginner's Guide

Dive into test-driven development (TDD) with our comprehensive Python tutorial. Learn how to write robust tests before coding with practical examples.
Amina Edmunds's photo

Amina Edmunds

7 min

Exponents in Python: A Comprehensive Guide for Beginners

Master exponents in Python using various methods, from built-in functions to powerful libraries like NumPy, and leverage them in real-world scenarios to gain a deeper understanding.
Satyam Tripathi's photo

Satyam Tripathi

9 min

Python Linked Lists: Tutorial With Examples

Learn everything you need to know about linked lists: when to use them, their types, and implementation in Python.
Natassha Selvaraj's photo

Natassha Selvaraj

9 min

See MoreSee More