Hello! Welcome to your fifth lesson in the "Mathematical Foundations for AI" module.
In our last lesson, you implemented gradient descent from scratch, which was a deep dive into the practical side of optimization. We saw how an algorithm can "learn" to find the minimum of a function. Now, we're shifting focus to the second major pillar of mathematics for AI: probability and statistics. These concepts are crucial for modeling the uncertainty and variability inherent in data.
Today's learning outcome is to describe common probability distributions, including Bernoulli and Normal distributions. We will explore what a probability distribution is and then focus on these two fundamental examples. Understanding them is essential because they form the basis for many AI techniques, from modeling simple binary outcomes to understanding the distribution of weights in a neural network.
1. What is a Probability Distribution?
At its core, a probability distribution is a function that describes the likelihood of all possible outcomes for a random event. Think of it as a complete map of probabilities for a variable.
To get a formal introduction, let's start with a short video that defines probability distributions and introduces some key terminology.
Introduction to Probability Distributions
The video 'Introduction to Probability Distributions' by 365 Data Science provides a clear overview of the core concepts, including the crucial distinction between discrete and continuous distributions.
Starting right at the beginning, watch the first part of the video on probability distributions. Pay close attention to the notation P(Y=y) and the difference between events with finite versus infinite outcomes.
As the video explained, we can categorize distributions into two main types:
- Discrete Distributions: These apply to variables that can only take on a finite or countably infinite number of distinct values. For example:
- The outcome of a single coin flip (Heads or Tails).
- The number of users who click an ad (0, 1, 2, ...).
- The result of rolling a six-sided die (1, 2, 3, 4, 5, 6).
- Continuous Distributions: These apply to variables that can take any value within a given range. Examples include:
- The exact height of a person.
- The time it takes for a web page to load.
- The temperature of a CPU.
For any distribution, we are often interested in its key characteristics, primarily its mean (the average value or center) and its variance or standard deviation (how spread out the values are).
2. The Bernoulli Distribution: Modeling Binary Events
Let's start with the simplest discrete distribution: the Bernoulli distribution. It models a single trial with only two possible outcomes, typically labeled as "success" (1) and "failure" (0).
This should feel familiar from a programming perspective. It's the probabilistic equivalent of a boolean variable: true/false, 1/0, on/off. Many real-world events can be modeled this way:
- Did a user click the "buy" button? (Yes/No)
- Is an email spam? (Yes/No)
- Does a manufactured component pass a quality check? (Pass/Fail)
The Bernoulli distribution is defined by a single parameter, , which is the probability of success.
- The probability of success (outcome 1) is .
- The probability of failure (outcome 0) is .
The following image clearly shows how the distribution changes for different values of .
To see how to work with this distribution in Python and explore more real-world applications, the following article is an excellent practical guide.
Bernoulli Distribution with Python | by Gianpiero Andrenacci
The article 'Bernoulli Distribution with Python' by Gianpiero Andrenacci provides a clear definition and shows you how to simulate and visualize Bernoulli trials using Python libraries like SciPy and Matplotlib.
Read the sections 'Understanding the Bernoulli Distribution' (covering the core concepts), 'Bernoulli Distribution in Python' (detailing code simulations), and 'Real-World Applications of the Bernoulli Distribution' (highlighting practical use cases). As you read, notice how simple Python code can simulate complex probabilistic events like A/B testing or customer conversion, which are highly relevant in the tech industry.
One particularly important connection mentioned in the article is the link between the Bernoulli distribution and Logistic Regression, a fundamental classification algorithm in machine learning. Logistic regression models the probability of a binary outcome, which is, by definition, a Bernoulli variable. We'll explore this in depth later in the course.
3. The Normal Distribution: The Bell Curve
Next, we'll look at the most famous continuous distribution: the Normal Distribution, also known as the Gaussian distribution or "bell curve". It's incredibly common in nature and statistics. Many real-world phenomena, like human height, measurement errors, and blood pressure, tend to follow a normal distribution.
In AI, the normal distribution is used everywhere. For example, the weights of a neural network are often initialized with random values drawn from a normal distribution.
The normal distribution is defined by two parameters:
- Mean (): The center of the distribution. It's the peak of the bell curve.
- Standard Deviation (): The measure of the spread or width of the curve. A small means the data is tightly clustered around the mean, resulting in a tall, narrow curve. A large means the data is spread out, resulting in a short, wide curve.
The following video from StatQuest gives a fantastic, intuitive explanation of these two parameters.
The Normal Distribution, Clearly Explained!!!
Josh Starmer's video, 'The Normal Distribution, Clearly Explained!!!', provides an excellent visual intuition for how the mean and standard deviation shape the bell curve.
About 30 seconds in, watch the normal distribution. Focus on the comparison between the heights of babies and adults to understand the roles of mean and standard deviation.
Key Properties of the Normal Distribution
- Symmetry: The curve is perfectly symmetric around the mean (). This means the mean, median, and mode are all equal.
- The Empirical Rule (68-95-99.7 Rule): This is a powerful rule of thumb for any normal distribution:
- Approximately 68% of the data falls within 1 standard deviation of the mean (between and ).
- Approximately 95% of the data falls within 2 standard deviations of the mean.
- Approximately 99.7% of the data falls within 3 standard deviations of the mean.

Standardization and the Z-score
What if you have two normal distributions with different means and standard deviations? For example, comparing exam scores from two different tests. To make them comparable, we standardize them. This process converts any normal distribution into a Standard Normal Distribution, which has a mean of 0 and a standard deviation of 1.
We do this by calculating a z-score for each value:
The z-score tells you exactly how many standard deviations a value is away from the mean . A z-score of 1.5 means the value is 1.5 standard deviations above the mean. A z-score of -2.0 means it's 2 standard deviations below the mean.
Putting It into Practice with Python
The best way to solidify these concepts is to work with them in code. The following article shows you how to use Python's scipy.stats library to solve common problems involving normal distributions.
The Normal Distribution with Python | by Sneha Bajaj
The article 'The Normal Distribution with Python' demonstrates how to standardize a distribution and use Python to answer practical questions about probabilities.
Read the sections 'Properties of a Normal Distribution' (focusing on the properties), 'Standard Normal Distribution' (focusing on standardization), and 'Problem Solving using Python' (focusing on practical exercises). Pay close attention to the code examples using norm.cdf() (to find probability) and norm.ppf() (to find a value given a probability). This is the practical application of the theory.
Test your understanding!
Suppose the time it takes for your backend service to process an API request is normally distributed with a mean of 120 milliseconds and a standard deviation of 20 milliseconds.
What percentage of requests take longer than 160 milliseconds to process?
You can solve this conceptually using the empirical rule or precisely with a short Python script using scipy.stats.norm.
Show answer
Conceptual Answer (using the Empirical Rule):
- The value 160 ms is 40 ms away from the mean of 120 ms.
- Since the standard deviation is 20 ms, 160 ms is exactly 2 standard deviations above the mean ().
- The empirical rule states that 95% of data is within 2 standard deviations. Due to symmetry, this means 47.5% is between the mean and +2, and 47.5% is between the mean and -2.
- Since 50% of the data is above the mean, the percentage of data above +2 is .
- So, approximately 2.5% of requests take longer than 160 ms.
Precise Answer (using Python):
from scipy.stats import norm
mu = 120
sigma = 20
value = 160
# norm.cdf(value, loc=mu, scale=sigma) calculates the probability of a value being LESS than or equal to 'value'.
# We want the probability of being GREATER, so we do 1 - cdf.
prob_greater_than_160 = 1 - norm.cdf(value, loc=mu, scale=sigma)
print(f"The percentage of requests taking longer than 160ms is: {prob_greater_than_160:.2%}")
# Output: The percentage of requests taking longer than 160ms is: 2.28%
The precise answer is very close to our estimate from the empirical rule!
Conclusion
Great work! You've just covered two of the most important probability distributions in data science and AI. Understanding their properties is a key step toward building and interpreting sophisticated models.
Here are the main takeaways from this lesson:
- Probability distributions map all possible outcomes of a random event to their corresponding likelihoods.
- The Bernoulli distribution models a single trial with two outcomes (success/failure) and is governed by a single parameter, (the probability of success). It's the foundation for modeling binary events.
- The Normal distribution is a continuous, bell-shaped distribution defined by its mean () and standard deviation (). Its properties are described by the Empirical Rule.
- We can standardize any normal distribution by calculating z-scores, allowing us to compare different datasets and use standard functions like those in
scipy.
In our next lesson, we will formalize some of the concepts we touched on today. We will focus on how to calculate and interpret the expectation (mean) and variance of random variables, giving you a more general toolkit for analyzing any probability distribution you encounter.