Hello! Welcome to the eighth module of our course, Probability and Statistics Essentials.
In the previous modules, we've built a strong foundation in Python and the mathematics of optimization, culminating in our implementation of gradient descent. We saw how calculus helps us find the "best" parameters for a model. But how do we handle the inherent randomness and uncertainty in data? And how do we evaluate how good our "best" model actually is? That's where probability and statistics come in.
This lesson marks our first step into this new area. Your learning outcome for today is to calculate probabilities using basic probability rules. We'll start from the ground up, defining the core language of probability and then learning the fundamental rule that governs many probability calculations. Given your background in computer science and mathematics, you'll find that much of this boils down to logical problem-solving and systematic counting.
Let's begin our journey into quantifying uncertainty.
1. The Language of Probability: Sample Spaces and Events
Before we can calculate any probabilities, we need a formal way to describe random experiments and their outcomes. The two most important concepts are the sample space and the event space.
- Sample Space (S): The set of all possible outcomes of an experiment.
- Event Space (E): A subset of the sample space that corresponds to an event we are interested in.
To get a clear understanding of these terms, please watch the following segment from a Stanford University lecture on probability for computer scientists. The instructor uses simple examples like coin flips and dice rolls to make these abstract definitions concrete.
Stanford CS109 Probability for Computer Scientists I What is Probability? I 2022 I Lecture 3
This video from Stanford Online introduces the foundational concepts of sample space and event space.
Watch the segment from 12:14 to 17:07. Focus on how the instructor defines the sample space and event space for different experiments, such as flipping one or two coins.
As you saw, defining these spaces is the first step in solving any probability problem. For example, when rolling a single six-sided die:
- The sample space is
S = {1, 2, 3, 4, 5, 6}. - If we're interested in the event "rolling an even number," the event space is
E = {2, 4, 6}.
2. The First Rule: Equally Likely Outcomes
Now that we can define our spaces, how do we calculate the probability of an event? The simplest and most fundamental rule applies when all outcomes in the sample space are equally likely.
In this special but common case, the probability of an event E is the ratio of the number of outcomes in the event space to the number of outcomes in the sample space.
Let's continue with the Stanford lecture, where this rule is introduced along with some important axioms of probability. Pay close attention to the lottery example, as it's a great illustration of a common pitfall: applying this rule when outcomes are not equally likely.
Stanford CS109 Probability for Computer Scientists I What is Probability? I 2022 I Lecture 3
This segment explains the basic axioms of probability and the crucial 'equally likely outcomes' assumption that allows us to calculate probability by counting.
Watch from 23:25 to 27:40. Focus on the formula P(E) = |E| / |S| and the condition under which it is valid.
3. Applying the Rule: Counting is Key
The rule P(E) = |E| / |S| transforms probability problems into counting problems. Your task becomes:
- Define a sample space where all outcomes are equally likely.
- Count the total number of outcomes (
|S|). - Count the number of outcomes that satisfy your event (
|E|). - Calculate the ratio.
The next part of the video provides an excellent, detailed walkthrough of this process for the experiment of rolling two dice. Since you have a background in Python, you'll appreciate that the instructor even writes a short simulation to empirically verify the calculated probability.
Stanford CS109 Probability for Computer Scientists I What is Probability? I 2022 I Lecture 3
Watch this example of calculating the probability of the sum of two dice. It's a classic problem that demonstrates the counting-based approach perfectly.
Watch from 27:40 to 37:42. Pay special attention to the discussion around 35:00, where the instructor explains why choosing the right sample space (one with equally likely outcomes) is critical for getting the correct answer.
The key insight here is subtle but powerful: sometimes you have to be clever in how you define your sample space. By treating the two dice as distinct (e.g., a red die and a green die), you create a sample space of 36 equally likely outcomes, like (1, 1), (1, 2), ..., (6, 6). If you had incorrectly defined the sample space as just the possible sums {2, 3, ..., 12}, the outcomes would not be equally likely, and our rule would fail.
Test your understanding!
Using the same two-dice experiment, what is the probability that the sum of the two dice is exactly 5?
Show answer
- Sample Space (
S): The sample space remains the 36 equally likely outcomes from(1,1)to(6,6). So,|S| = 36. - Event Space (
E): We need to find all pairs that sum to 5. These are:(1, 4),(2, 3),(3, 2), and(4, 1). There are 4 such outcomes. So,|E| = 4. - Probability:
P(sum is 5) = |E| / |S| = 4 / 36 = 1/9.
4. More Advanced Counting: Combinations
Let's tackle a more complex problem. This next example involves drawing toys from a box and introduces the idea of using combinations (a concept from combinatorics often written as "n choose k" or ) to count the outcomes. This problem reinforces the strategy of making items theoretically "distinct" to ensure an equally likely sample space.
Stanford CS109 Probability for Computer Scientists I What is Probability? I 2022 I Lecture 3
This challenging 'cows and pigs' problem will test your understanding of how to construct a sample space. It's a great example of how probability problems are often puzzles in disguise.
Watch from 37:34 to 52:01. The central takeaway is that to use the counting rule, it's often best to treat all items as distinct (e.g., Cow 1, Cow 2, Pig 1) even if they seem identical. This helps create an equally likely sample space.
The two successful approaches in the video (ordered and unordered) highlight a deep point: as long as you are consistent in how you count for your event space and your sample space, you can arrive at the right answer. The unordered approach, using combinations, is often more direct.
For reference, the formula for combinations (choosing k items from a set of n where order doesn't matter) is:
Your Python skills might come in handy here, as libraries like scipy.special.comb can calculate this for you.
5. Combining Probabilities: The Addition Rule
So far, we've calculated the probability of a single event. What if we want to know the probability of event A or event B happening? For this, we use the General Rule of Addition.
The probability of A or B is the sum of their individual probabilities, minus the probability of their intersection (the part where they overlap).
The notation ∪ means "union" (or), and ∩ means "intersection" (and).

If two events are mutually exclusive (they cannot happen at the same time, so their intersection is empty), the rule simplifies to P(A ∪ B) = P(A) + P(B).
The following resource provides a clear textual summary and then a worked example of applying these rules.
Probability Beginner to Advanced for Data Science Part 1
This article from The Tech Intel provides a solid textual summary of probability axioms and rules, including the General Addition Rule.
Read the section titled 'Probability Axioms and Properties'. It lists the fundamental axioms and derives several properties, including the 'General Addition Rule' with a clear example.
Conclusion
In this lesson, we laid the essential groundwork for our study of probability. You've moved from the world of deterministic calculus to the world of uncertainty, and you now have the tools to begin quantifying it.
Key Takeaways:
- Probability problems start with defining a sample space (all possible outcomes) and an event space (the outcomes you are interested in).
- When all outcomes in the sample space are equally likely, the probability of an event is simply the ratio of the size of the event space to the size of the sample space:
P(E) = |E| / |S|. - Applying this rule often turns probability problems into counting problems. A key strategy is to define your sample space in a way that makes outcomes distinct and equally likely.
- The Addition Rule (
P(A ∪ B) = P(A) + P(B) - P(A ∩ B)) allows you to calculate the probability that at least one of two events will occur.
Preview of the Next Lesson:
Today, we focused on counting total and favorable outcomes. In the next lesson, we will introduce a pivotal concept: conditional probability. We'll ask questions like, "What is the probability of event A, given that event B has already happened?" This will lead us directly to Bayes' Theorem, a cornerstone of modern data science and machine learning.