Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 4: Conditional Probability

Let’s use Python to explore the concepts of conditional probability. We’ll need libraries like numpy for numerical operations and random sampling, and potentially pandas for handling data.

Simulating Conditional Events: Drawing Cards

Let’s simulate drawing two cards from a standard 52-card deck without replacement and verify the conditional probability P(2nd is King∣1st is King)P(\text{2nd is King} | \text{1st is King}). We expect this to be 3/513/51.

We can also estimate the overall probability P(2nd is King)P(\text{2nd is King}). By symmetry or using the Law of Total Probability, this should be 4/524/52.

So, both our intuition and a more formal application of probability rules confirm these results! Now, are you ready to simulate this?

--- Theoretical Probabilities ---
P(1st is King): 0.076923 (4/52)
P(2nd is King): 0.076923 (4/52)
P(Both Kings): 0.004525
P(2nd is King | 1st is King): 0.058824 (3/51)


--- Simulation Results (100000 runs) ---
Estimated P(1st is King): 0.077110
Estimated P(2nd is King): 0.077370
Estimated P(Both Kings): 0.004490
Estimated P(2nd is King | 1st is King): 0.058229

The simulation results should be very close to the theoretical values, demonstrating how simulation can approximate probabilistic calculations. The larger num_simulations, the closer the estimates will typically be.

Calculating Conditional Probabilities from Data

Imagine we have data about website visitors, including whether they made a purchase and whether they visited a specific product page first. We can use Pandas to calculate conditional probabilities from this data.

Sample Data

A DataFrame is created with data on website visitors, including whether they visited a specific product page and whether they made a purchase.

Sample Visitor Data:
Loading...


Approach 1: Calculated Conditional Probability

First calculate basic probabilities:

P(Visited Page) = 0.60
P(Purchased) = 0.40
P(Visited and Purchased) = 0.30


Next calculate conditional probabilities:

Calculated P(Purchased | Visited Page) = 0.50
Calculated P(Visited Page | Purchased) = 0.75
<Figure size 400x300 with 1 Axes>

This Venn diagram visually clarifies how different probabilities are related:

Approach 2: Direct Conditional Probability

Direct P(Purchased | Visited Page) = 0.50 (3/6)
Direct P(Visited Page | Purchased) = 0.75 (3/4)
<Figure size 1400x900 with 1 Axes>

This tree diagram illustrates the direct approach to conditional probability by visually segmenting the visitor population based on a sequence of events or conditions. To find P(Purchased∣Visited Page)P(\text{Purchased} | \text{Visited Page}), for instance, you first follow the branch representing ‘Visited Page’, effectively filtering the data, and then observe the proportion of those visitors who subsequently ‘Made Purchase’. The diagram clearly shows how the initial condition narrows the focus to a specific subgroup before the probability of the second event is determined within that subgroup.

Summary

Both approaches should yield the same results if done correctly, and the choice between them can depend on the context and what data or probabilities you already have at hand.

This example shows how to compute conditional probabilities directly from observed data, a common task in data analysis. P(Purchased∣Visited Page)P(\text{Purchased} | \text{Visited Page}) tells us the likelihood of a purchase among those who visited the specific page, while P(Visited Page∣Purchased)P(\text{Visited Page} | \text{Purchased}) tells us the likelihood that someone who did purchase had previously visited that page. These can be very different values!

Applying the Law of Total Probability

Let’s use the Law of Total Probability with a manufacturing example. Suppose a factory has two machines, M1 and M2, producing widgets.

What is the overall probability that a randomly selected widget is defective (P(D)P(D))?

The events M1 (widget produced by M1) and M2 (widget produced by M2) form a partition of the sample space (all widgets). Using the Law of Total Probability:

P(D)=P(D∣M1)P(M1)+P(D∣M2)P(M2)P(D) = P(D|M1)P(M1) + P(D|M2)P(M2)

P(Defective | M1) = 0.02
P(M1) = 0.6
P(Defective | M2) = 0.05
P(M2) = 0.4


Overall Probability of a Defective Widget P(D) = (0.02 * 0.6) + (0.05 * 0.4) = 0.0320

So, the overall probability of finding a defective widget is 3.2%. This weighted average reflects that while M2 produces more defective items proportionatly, M1 produces more items overall.

We could also simulate this:


--- Simulation Results (100000 widgets) ---
Estimated overall P(Defective): 0.0322

Again, the simulation provides an estimate very close to the calculated value.


This chapter introduced conditional probability, the multiplication rule, the law of total probability, and tree diagrams. These concepts are crucial for reasoning under uncertainty and form the basis for more advanced topics like Bayes’ Theorem, which we will explore in the next chapter. The hands-on examples demonstrated how to calculate and simulate these probabilities using Python.

Okay, here is the entire “Exercises” section with the questions followed by their respective dropdown explanations: