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 P(A)P(A) of an event AA is defined as:

P(A)=Number of favorable outcomesTotal number of outcomes P(A) = \frac{\text{Number of favorable outcomes}}{\text{Total number of outcomes}}

For example, if we flip a fair coin, the probability of getting heads is:

P(Heads)=12=0.5 P(\text{Heads}) = \frac{1}{2} = 0.5

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 (SS)

The set of all possible outcomes. For a die roll:

S={1,2,3,4,5,6} S = \{1, 2, 3, 4, 5, 6\}

2. Events

An event is a subset of the sample space. For example, rolling an even number is:

E={2,4,6} E = \{2, 4, 6\}

3. Probability Rules

Here are some rules that every AI engineer should know:

  • Addition Rule: If two events AA and BB are mutually exclusive:

    P(A or B)=P(A)+P(B) P(A \text{ or } B) = P(A) + P(B)

    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 AA and BB:

    P(A and B)=P(A)⋅P(B) P(A \text{ and } B) = P(A) \cdot P(B)

    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:

    P(not A)=1−P(A) P(\text{not } A) = 1 - P(A)

4. Conditional Probability

The probability of AA, given that BB has occurred, is defined as:

P(A∣B)=P(A and B)P(B) P(A | B) = \frac{P(A \text{ and } B)}{P(B)}
Probability Rules Reference Flowchart
Decision tree for choosing the correct addition, multiplication, or complement rule when working with two events A and B.

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:

  • P(A)=0.1P(A) = 0.1 - 10% chance of a text (baseline probability)
  • P(B)=0.05P(B) = 0.05 - 5% chance of your favorite song playing from 9-10 AM
  • P(A∣B)=0.5P(A|B) = 0.5 - 50% chance of a text if the song plays during this time

Now we can rearrange the conditional probability equation to solve for P(A and B)P(A \text{ and } B):

P(A and B)=P(A∣B)⋅P(B) P(A \text{ and } B) = P(A|B) \cdot P(B)

Plugging in our numbers:

P(A and B)=0.5⋅0.05=0.025=2.5% P(A \text{ and } B) = 0.5 \cdot 0.05 = 0.025 = 2.5\%

If we had assumed independence, we would have used:

P(A)⋅P(B)=0.1⋅0.05=0.005=0.5% P(A) \cdot P(B) = 0.1 \cdot 0.05 = 0.005 = 0.5\%

When events are dependent, treating them as independent can significantly underestimate (or overestimate) the true probability of both events occurring together.

Underestimating Conditional Probability Example - Receiving a Text
Flowchart illustration of the "text from a friend" example above.

Example 2: Overestimating Conditional Probability

As another example, consider drawing cards from a deck without replacement:

P(drawing a king first)=452≈0.077P(drawing a king second)=452≈0.077 P(\text{drawing a king first}) = \frac{4}{52} \approx 0.077 \\ P(\text{drawing a king second}) = \frac{4}{52} \approx 0.077

If we assumed these were independent, we would calculate:

P(drawing two kings)=0.077×0.077≈0.006 P(\text{drawing two kings}) = 0.077 \times 0.077 \approx 0.006

But since removing a king reduces the probability of drawing another king:

P(king second∣king first)=351≈0.059 P(\text{king second} | \text{king first}) = \frac{3}{51} \approx 0.059

Then the actual probability is:

P(drawing two kings)=0.077×0.059≈0.0045 P(\text{drawing two kings}) = 0.077 \times 0.059 \approx 0.0045

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 BB decreases if AA 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.

Overestimating Conditional Probability Example - Drawing Cards
Flowchart illustration of the "drawing cards" example above.

Applications in Machine Learning

Probability is the foundation of many ML concepts:

  1. Classifiers: Probabilities help determine class membership (e.g., “Spam vs. Not Spam”).
  2. Loss Functions: Algorithms use probability to quantify errors, like cross-entropy in classification tasks.
  3. 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:

y=β0+β1x1+β2x2+⋯+ϵ y = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \cdots + \epsilon

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 x1x_1 and number of bedrooms x2x_2.

If the true relationship includes an interaction:

y=100+50x1+30x2+10(x1⋅x2)+ϵ y = 100 + 50x_1 + 30x_2 + 10(x_1 \cdot x_2) + \epsilon

But the model assumes additivity:

y^=β0+β1x1+β2x2 \hat{y} = \beta_0 + \beta_1 x_1 + \beta_2 x_2

This omission causes systematic errors:

  • A large house with many bedrooms (x1=1000,x2=4x_1 = 1000, x_2 = 4) is undervalued because the interaction 10(x1⋅x2)10(x_1 \cdot x_2) is ignored.
  • Including an interaction term (β3x1⋅x2\beta_3 x_1 \cdot x_2) corrects this, capturing how features jointly affect yy.

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.