Basic Probability
Machine learning operates in a world of probability. For example, consider how tokens (words) are predicted in an LLM, where each possible next word is assigned a probability:
“I walked into this cozy cafe where…”
| Next Word Token | Probability |
|---|---|
| “the” | 0.35 |
| “a” | 0.25 |
| “this” | 0.20 |
| “that” | 0.15 |
| “another” | 0.05 |
In this lesson, we’ll explore the basics of probability and how it applies to AI engineering.
This Lesson Will Cover
- 🎲 The concept of probability and its mathematical foundation
- 📏 Key probability rules (addition, multiplication, complement)
- 🔄 Conditional probability and its implications
- 🌍 Real-world applications in machine learning
- 🔗 The impact of independence assumptions in ML models
What Is Probability?
I’m sure we all know this, but let’s write it out anyway.
Probability quantifies uncertainty. It tells us how likely an event is to occur, with values ranging from 0 (impossible) to 1 (certain).
Mathematically, the probability of an event is defined as:
For example, if we flip a fair coin, the probability of getting heads is:
Key Concepts in Probability
Here are some fundamental concepts that we all probably know, at least intuitively, even if we’ve never seen them expressed mathematically.
1. Sample Space ()
The set of all possible outcomes. For a die roll:
2. Events
An event is a subset of the sample space. For example, rolling an even number is:
3. Probability Rules
Here are some rules that every AI engineer should know:
-
Addition Rule: If two events and are mutually exclusive:
For example, rolling a die and getting a (A) 3 or (B) 5. These are mutually exclusive because you can’t roll both at the same time.
-
Multiplication Rule: For independent events and :
For example, the chance that (A) your best friend texts you and (B) your favorite song comes on the radio. This works because the events are unrelated. On the other hand, if your best friend is texting you because they heard your favorite song on the radio then, then you can no longer break down this situation into independent probabilities. More on this below.
-
Complement Rule: The probability of an event not occurring:
4. Conditional Probability
The probability of , given that has occurred, is defined as:
Example 1: Underestimating Conditional Probability
Continuing our example above, let’s figure out the probability that (A) you will get a text from your best friend and (B) your favorite song comes on the radio, given that we know these events are not independent.
If we know that:
- - 10% chance of a text (baseline probability)
- - 5% chance of your favorite song playing from 9-10 AM
- - 50% chance of a text if the song plays during this time
Now we can rearrange the conditional probability equation to solve for :
Plugging in our numbers:
If we had assumed independence, we would have used:
When events are dependent, treating them as independent can significantly underestimate (or overestimate) the true probability of both events occurring together.
Example 2: Overestimating Conditional Probability
As another example, consider drawing cards from a deck without replacement:
If we assumed these were independent, we would calculate:
But since removing a king reduces the probability of drawing another king:
Then the actual probability is:
In this case, treating dependent events as independent would overestimate the true probability by about 33%.
When events are negatively correlated, i.e. the likelihood of decreases if has occurred, then incorrectly assuming independence will cause us to overestimate the true conditional probability.
Notice how this is the opposite of how it worked in the case of positive correlation, with the song on the radio example above.
Applications in Machine Learning
Probability is the foundation of many ML concepts:
- Classifiers: Probabilities help determine class membership (e.g., “Spam vs. Not Spam”).
- Loss Functions: Algorithms use probability to quantify errors, like cross-entropy in classification tasks.
- Data Uncertainty: Models use probabilistic techniques to handle noisy or incomplete data.
Independence Assumptions in Machine Learning
Given our examples on conditional probabilities above, I wanted to round out this lesson with a discussion of independence assumptions in ML.
As it turns out, many ML algorithms assume that features are independent of each other.
At first glance this might be quite alarming, since features in real-world datasets almost always have correlations!
Keep in mind that when we do ML, we are trying to solve practical problems. Therefore we don’t necessarily need a statistically perfect result (although we should strive for this as much as possible) - we just need a model that yields an acceptable error tolerance to solve our specific problem.
Let’s consider how this assumption affects linear models, and what we can do about it.
Feature Independence in Linear Models
Linear models assume that features contribute additively to the target variable:
By adding features in this way, we make the assumption that they are independent. In other words, we assume that the effect of one feature on the target variable does not depend on the values of other features.
When features interact, this assumption can miss those relationships, and therefore result in biased predictions.
Consider this example of predicting the price of a home, given features of square footage and number of bedrooms .
If the true relationship includes an interaction:
But the model assumes additivity:
This omission causes systematic errors:
- A large house with many bedrooms () is undervalued because the interaction is ignored.
- Including an interaction term () corrects this, capturing how features jointly affect .
This example actually makes sense in the real world. Imagine a house with small square footage but an unusually large number of rooms. This would almost certainly be less desirable than a house with the same floor space but fewer rooms, i.e., an appropriate number of rooms for the size.
Conclusion
Probability is important for ML, but it can also help us make better choices in everyday life and change how we think about things. We’ll talk more about that in the Bayesian Thinking lesson (after the next one).
Key Takeaways
🎲 Probability quantifies uncertainty in AI models.
🔗 Independent vs. dependent events impact probability calculations.
❓ Conditional probability is important to understand and account for.
🤖 ML models often assume feature independence, but real-world data usually has correlations.
Next: Distributions and Random Variables
Up next, we’re going to talk stats.
Comments (0)
No comments yet.