Recommended resources
The freely available resources this lesson will direct you to.
Hello! Welcome to your next lesson on the mathematical foundations of AI.
In our last lesson, we completed a rigorous derivation of the mean and variance for the univariate Gaussian distribution. That entire analysis, like most of our work so far, focused on a single random variable at a time. However, real-world systems and data are rarely that simple. An AI model for house price prediction doesn't just use square footage; it uses the number of bedrooms, location, age, and dozens of other interacting variables.
Today, we move from one dimension to many. We will build the foundational framework for describing the relationships between multiple random variables. Your learning outcome is to define joint, marginal, and conditional distributions for multiple random variables. Mastering these concepts is essential for understanding everything from feature dependencies in a dataset to the architecture of sophisticated probabilistic models.
1. From One Variable to Many: A Discrete Example
The easiest way to grasp these new concepts is to start with a simple, discrete case. Imagine we're tracking two random variables:
- : Whether it rains (1 for rain, 0 for no rain).
- : Whether it's windy (1 for windy, 0 for no wind).
We can describe the probability of every possible weather combination in a single table.

This table is the heart of our discussion today. Let's break down what it represents.
Joint Distribution
The four values in the center of the table represent the joint probability mass function (PMF), denoted as . It tells us the probability of a specific pair of outcomes occurring simultaneously. For example, the probability of it being rainy and windy is . A joint distribution captures the complete probabilistic relationship between the variables.
Marginal Distribution
What if we only care about the probability of rain, regardless of the wind? We can get this by summing the joint probabilities over all possibilities for wind. This is called marginalization.
The marginal probability mass function for rain, , is found by summing the rows:
Notice that these sums are written in the "margin" of the table, which is where the name comes from. The marginal distribution is the probability distribution of the random variable on its own.
Conditional Distribution
Finally, what if we want to know the probability of rain, given that we know it is windy? This brings us to the conditional probability mass function, , which is defined using the familiar rule for conditional probability:
For example, the probability of rain given that it's windy is:
This tells us that if you observe it's windy, the probability of it also being rainy is about 16.7%.
Test your understanding!
Using the table above, calculate the probability of it being windy given that there is no rain, i.e., .
Show answer
First, we identify the necessary probabilities from the table:
- The joint probability is .
- The marginal probability is .
Now, we apply the definition of conditional probability:
So, there's about a 14.7% chance of it being windy if we know it's not raining.
2. Formal Definitions for Discrete and Continuous Variables
The intuitive ideas of joint, marginal, and conditional distributions apply to continuous variables as well, but we replace summations with integrals. The following document from Stanford's CS229 course provides the formal, rigorous definitions you'll need.
Review of Probability Theory - CS229
These notes on Probability Theory provide concise, formal definitions for handling two random variables. They cover both discrete (PMF) and continuous (PDF) cases.
Please read Sections 3.1 through 3.4 (from 'Joint and marginal distributions' to 'Conditional distributions'). Focus on how the core concepts from our discrete example are formalized and extended to the continuous case: Joint distributions: Note the definitions of the joint CDF F_{XY}(x,y), joint PMF p_{XY}(x,y), and joint PDF f_{XY}(x,y). Marginalization: Pay close attention to how summing over a variable in the discrete case (Section 3.2) becomes integrating over that variable in the continuous case (Section 3.3). Conditional distributions: See how the formula P(A|B) = P(A,B)/P(B) is directly adapted for both conditional PMFs and conditional PDFs (Section 3.4).
To summarize the key parallels between discrete and continuous cases:
| Concept | Discrete Case (PMF) | Continuous Case (PDF) |
|---|---|---|
| Joint | where | |
| Marginal | ||
| Conditional | $ p_{Y | X}(y |
3. A Deeper Dive: A Continuous Example
Let's apply these definitions to a more complex continuous problem. A common way to test understanding is with a uniform distribution over a non-rectangular region. This forces careful application of the integral definitions.
The following video from Harvard's Statistics 110 course works through an excellent example: finding the marginal and conditional PDFs for a point chosen uniformly at random from a disk of radius 1.
Lecture 19: Joint, Conditional, and Marginal Distributions | Statistics 110
In this segment of 'Lecture 19: Joint, Conditional, and Marginal Distributions', the instructor demonstrates how to derive marginal and conditional PDFs from a joint PDF for a uniform distribution on a disk.
Watch the segment from 12:39 to 22:09. This is a crucial application of the theory. As you watch, focus on: Defining the Joint PDF: Why is the joint PDF a constant 1/\pi inside the disk and 0 outside? Calculating the Marginal PDF: This is the most challenging step. Pay close attention to how the limits of integration for y are determined by the value of x. They are not constant from -\infty to \infty. Calculating the Conditional PDF: See how it's a simple division once the joint and marginal are known. The Result: What does the final conditional distribution, Y | X=x, represent geometrically?
This example beautifully illustrates that even if the joint distribution is "uniform," the marginal distributions are not. The marginal PDF for is , which is highest at and zero at . This should make intuitive sense: if you pick a random point in a disk, it's more likely to have an x-coordinate near the center than near the edge.
4. Generalization to Multiple Variables and the Chain Rule
These concepts extend naturally from two to random variables. A joint distribution can be defined over , and we can find the marginal distribution of any subset of these variables by integrating or summing out the others.
The definition of conditional probability leads to one of the most important formulas in probabilistic modeling: the chain rule of probability. Just as , we can decompose a joint distribution into a product of conditional distributions. For three variables, this is:
In general, for variables:
This rule allows us to build complex joint distributions from simpler conditional ones. This is the foundational principle behind Bayesian Networks and other graphical models you may have encountered in your AI studies. The following video explains this rule and also introduces the important related concept of conditional independence.
#11 Independence Conditional Independence Chain Rule Of Probability
This video from NPTEL provides a clear explanation of the chain rule and the crucial distinction between independence and conditional independence.
Watch from 05:55 to 17:28. The first part (up to 11:44) introduces conditional independence, using excellent examples to show that two variables can be dependent, but become independent once you know a third variable (and vice versa). The second part (from 11:44) derives the chain rule and shows how it can be used to represent the probability of a high-dimensional event, like an image.
Conclusion
Today's lesson provided the essential vocabulary and mathematical tools for working with multiple random variables. While the concepts may seem abstract, they form the bedrock of how we model uncertainty in complex systems.
Key Takeaways:
- A joint distribution describes the simultaneous behavior of multiple random variables.
- A marginal distribution describes the behavior of a single variable, which is found by "marginalizing" (summing or integrating) out all other variables from the joint distribution.
- A conditional distribution describes the behavior of one variable given the known value of another, defined as the ratio of the joint to the marginal distribution.
- The chain rule allows us to decompose a complex joint distribution into a product of simpler conditional distributions, a cornerstone of probabilistic AI models.
Preview of the Next Lesson:
In this lesson, we focused on defining the concepts. In the next lesson, "Compute marginal and conditional distributions from a given joint distribution," we will put this theory into practice. You will work through concrete problems where you are given a joint PMF or PDF and must derive the corresponding marginal and conditional distributions yourself.