Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 9

Modeling Binary and Count Data

Recommended resources

Hello! Welcome to your third lesson in the "Probability and Distributions for Modeling Data" module.

In our last lesson, we learned how to summarize a probability distribution using its expected value (mean) and variance. These two numbers give us a snapshot of a distribution's center and spread.

Today, we'll apply these concepts to three of the most fundamental discrete probability distributions in machine learning. Our learning outcome is to model binary outcomes and count data using Bernoulli, Binomial, and Poisson distributions. You'll learn to identify which distribution to use for different scenarios, understand their parameters, and see how they are used to model real-world data like user clicks, manufacturing defects, or website traffic.

1. The Building Block: The Bernoulli Distribution

The simplest probabilistic scenario involves a single event with only two possible outcomes, often labeled as "success" (1) and "failure" (0). Think of flipping a coin once, a user clicking on an ad or not, or a single manufactured part being defective or not. This is modeled by the Bernoulli distribution.

The Bernoulli distribution is defined by a single parameter, , which is the probability of success.

The following short video provides a clear definition of the Bernoulli distribution and, importantly, connects it to the concepts from our last lesson by stating its mean and variance.

Bernoulli, Binomial and Poisson Random Variables

Watch this first segment from the slcmath@pc channel to understand the fundamentals of the Bernoulli distribution.

Watch the video from the beginning until 04:42. Focus on the definition of a Bernoulli experiment, its single parameter 'P', and the formulas for its mean and variance. This directly builds on what we covered last time.

As the video explained, the expected value and variance are straightforward to calculate:

  • Expected Value:
  • Variance:

The article below provides a quick, practical example of a Bernoulli distribution in the context of a medical trial and shows how you might visualize it.

5 Data Distributions for Data Scientists

Read this brief section from Towards Data Science for a practical take on the Bernoulli distribution.

Read the section 'Bernoulli Distribution – Discrete Distribution'. Note the accessible medical treatment example and the simple visualization of the probabilities.


2. Counting Successes: The Binomial Distribution

What happens if we repeat a Bernoulli trial multiple times? For example, what is the probability of getting 7 heads in 10 coin flips? Or the probability that 15 out of 100 users will click on an ad?

This is where the Binomial distribution comes in. It models the number of successes, , in a fixed number of independent Bernoulli trials, .

The Binomial distribution has two parameters:

  • : The total number of trials.
  • : The probability of success on any single trial.

The probability of observing exactly successes in trials is given by the probability mass function (PMF):

Where ("n choose k") is the binomial coefficient, which counts the number of ways to choose successes from trials.

The next segment of the video you just watched explains this formula and provides a powerful insight into the mean and variance of the Binomial distribution.

Bernoulli, Binomial and Poisson Random Variables

Continue with the slcmath@pc video to see how the Binomial distribution is constructed from Bernoulli trials.

Watch the video from 04:42 to 19:07. Pay close attention to two key parts: The explanation of the probability formula with the coin-flipping example. The derivation of the mean (np) and variance (np(1-p)) starting around 14:25. The idea that a Binomial random variable is just a sum of independent Bernoulli variables is a key concept that elegantly explains why its mean and variance are what they are.

This connection is crucial: since a Binomial experiment is the sum of independent Bernoulli trials, its mean is simply times the mean of a single trial (), and its variance is times the variance of a single trial (). This is a great example of how simpler concepts build into more complex ones.

Binomial vs. Bernoulli Distribution Visual Comparison
This image visually compares the Binomial and Bernoulli distributions. The plot on the right shows a Bernoulli distribution is just a Binomial with a single trial (n=1). The plots on the left show how the shape of the Binomial distribution changes for different numbers of trials (n) and success probabilities (p).
Test your understanding!

A quality control engineer inspects a batch of 10 computer chips. The probability that any single chip is defective is 5% (). What is the probability that exactly 2 chips in the batch are defective?

Hint: This is a Binomial scenario. Identify n, k, and p, and set up the formula.

Show answer

Here, we have:

  • (number of trials/chips)
  • (number of successes/defective chips we're interested in)
  • (probability of success/a chip being defective)

The formula is:

Let's break it down:

So, the probability is .
There is approximately a 7.5% chance of finding exactly 2 defective chips in the batch.


3. Counting Events in an Interval: The Poisson Distribution

Sometimes, we're not counting successes out of a fixed number of trials, but rather the number of times an event occurs over a fixed interval of time or space. For example:

  • The number of emails you receive in an hour.
  • The number of typos on a page of a book.
  • The number of customers arriving at a store in a day.

For these scenarios, we use the Poisson distribution. It's defined by a single parameter, (lambda).

  • : The average number of events in the specified interval.

The probability of observing exactly events in an interval is given by the PMF:

Where is the base of the natural logarithm ().

A key property of the Poisson distribution is that its mean and variance are both equal to .

  • Expected Value:
  • Variance:

The final section of the first video introduces the Poisson distribution and its formula.

Bernoulli, Binomial and Poisson Random Variables

Finally, let's learn about the Poisson distribution from the slcmath@pc video.

Watch from 19:07 to the end (25:12). Focus on the type of problem it solves (counting occurrences over a fixed interval), its parameter \lambda, and its mean and variance.

The Connection Between Binomial and Poisson

You might wonder how the Binomial and Poisson distributions are related. Given your engineering background, you'll appreciate that the Poisson distribution can be derived as a limiting case of the Binomial distribution.

Imagine a Binomial scenario where the number of trials is very large, but the probability of success is very small. For instance, the number of cars passing a certain point on a highway in an hour ( is huge, as every passing moment is a "trial") and the probability of a car being a rare classic model ( is tiny). In this situation, the Binomial distribution starts to look just like a Poisson distribution, where .

The following video provides a brilliant, intuitive explanation of this connection.

The Binomial and Poisson Distributions

This video from Serrano.Academy will give you a deeper understanding of the 'why' behind the Poisson distribution by showing how it emerges from the Binomial distribution.

Watch the video from 10:55 to 23:28. The core idea is presented by dividing an hour into more and more sub-intervals. Don't worry about memorizing every step of the algebraic derivation; focus on the conceptual journey of how a Binomial process with many trials and a low success rate converges to a Poisson process.


4. The Distributions in Practice

To see these distributions in a machine learning context and understand how to work with them in Python, the Neuromatch Academy tutorial is an excellent resource. It uses examples like a rat in a maze (Binomial) and neural spiking (Poisson).

Tutorial 1: Probability Distributions — Neuromatch Academy

This tutorial provides practical examples and Python code to sample from these distributions, connecting theory to application.

First, read 'Section 2.1: Binomial distributions'. Notice the formula and the T-maze example. Then, read 'Section 2.2: Poisson distribution', focusing on the neural spiking example. The Python code snippets using np.random.binomial and np.random.poisson show how you would generate data from these models in practice.


Conclusion

Today we've covered the three most important discrete probability distributions for modeling data. By understanding their assumptions and parameters, you can now choose the right tool for modeling binary events and counts.

Key Takeaways:

Distribution What It Models Parameters Mean () Variance ()
Bernoulli A single trial with two outcomes (0 or 1). (probability of success)
Binomial The number of successes in fixed trials. (number of trials), (success prob.)
Poisson The number of events in a fixed interval. (average rate of events)

Next Steps:
We've now covered the essential distributions for discrete data (counts). In our next lesson, we will shift our focus to continuous data—measurements that can take any value within a range. We'll explore the two most important continuous distributions: the Normal (Gaussian) distribution for modeling symmetric, bell-shaped data, and the Exponential distribution for modeling waiting times between events.