Hello! Welcome to your sixth lesson in the "Mathematical Foundations for AI" module.
In our last lesson, we explored the concept of probability distributions and took a close look at two fundamental examples: the discrete Bernoulli distribution for binary outcomes and the continuous Normal (or Gaussian) distribution. We saw that these distributions are characterized by key parameters like a mean () and a standard deviation ().
Today, we will formalize these concepts. The learning outcome for this lesson is to calculate and interpret the expectation and variance of random variables. We'll define what these terms mean mathematically, learn how to calculate them, and, most importantly, understand what they tell us about the data we're modeling. This is crucial because these two measures form the statistical backbone for summarizing and understanding almost any random process you'll encounter in AI.
1. Expected Value: The Center of Mass
In the previous lesson, we referred to the "mean" of a distribution. In probability theory, the formal term for this is the Expected Value, often denoted as or .
Intuitively, the expected value is the long-run average of a random variable. If you were to run an experiment millions of times and average the outcomes, that average would converge to the expected value. Think of it as the "center of mass" of the probability distribution.
The following video provides an excellent intuitive and mathematical introduction to this concept.
The Expected Value (Mean) of a Probability Distribution
This video, 'The Expected Value (Mean) of a Probability Distribution' from the Steve Brunton channel, explains the expected value as the center of mass and the long-run average of samples. It also introduces the formulas for both discrete and continuous variables.
Watch the video from the beginning to about seven and a half minutes in. Focus on: The 'center of mass' analogy. The formulas for discrete and continuous variables. The connection between the sample mean and the expected value (the Law of Large Numbers).
Calculating the Expected Value
As explained in the video, the calculation method depends on whether the random variable is discrete or continuous.
For a discrete random variable, the expected value is a weighted average of all possible outcomes. You calculate it by summing the product of each outcome value and its corresponding probability:
where is a possible value of the random variable , and is its probability.
Example: Rolling a Fair Six-Sided Die
Let be the outcome of a roll. The possible values are {1, 2, 3, 4, 5, 6}, and each has a probability of .
Notice a key insight: the expected value (3.5) is not an outcome that can actually occur. This is common and highlights that the expected value is a theoretical average, not necessarily the most likely outcome.
For a continuous random variable, the logic is the same, but the sum becomes an integral over the probability density function (PDF), :
You won't be expected to solve complex integrals in this course, but it's important to recognize the parallel structure. In both cases, we are just weighting each possible value by its likelihood.
2. Variance and Standard Deviation: Measuring the Spread
The expected value tells us about the center of a distribution, but it doesn't tell the whole story. Two distributions can have the same mean but look completely different.

This is where variance comes in. Variance, denoted or , measures the spread or dispersion of a random variable around its mean. A small variance means the data points tend to be very close to the mean; a large variance means they are spread out over a wider range.
Calculating the Variance
The variance is formally defined as the expected value of the squared difference from the mean:
This definition is intuitive—it's the average "squared distance" from the center. We square the difference so that deviations above and below the mean don't cancel each other out.
While this formula is great for interpretation, a different one is often easier for calculations. This is sometimes called the computational formula:
To use this, you first calculate (the expected value of ) and then subtract the square of the mean (), which you've likely already calculated.
Standard Deviation
The variance is measured in squared units (e.g., dollars-squared), which can be hard to interpret. To bring the measure of spread back into the original units of the random variable, we simply take the square root of the variance. This is called the standard deviation, denoted :
The standard deviation is often more useful for interpretation because it's in the same units as the mean. For example, if a model's prediction error has a mean of 0 and a standard deviation of 5, you know the typical error is "around 5 units".
The following video walks through a full example of calculating the expected value, variance (using both formulas), and standard deviation for a discrete random variable.
Expected Value and Variance of Discrete Random Variables
The video 'Expected Value and Variance of Discrete Random Variables' by jbstatistics is a very clear, step-by-step walkthrough of the entire calculation process. It reinforces the formulas and their practical application.
Watch the entire video. Starting around six minutes in, pay close attention to the variance calculation to see how both the definitional and computational formulas for variance are used and how they yield the same result. The example is concrete and easy to follow.
Test your understanding!
Consider the following probability function for a discrete random variable . This might represent the number of servers in a cluster that fail in a given hour.
| (Number of Failures) | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| 0.6 | 0.2 | 0.1 | 0.1 |
Calculate the expected value and the variance .
Show answer
1. Calculate the Expected Value :
So, on average, we expect 0.7 server failures per hour.
2. Calculate the Variance using the computational formula:
First, we need :
Now, use the formula :
The variance of server failures is 1.01. The standard deviation would be failures.
3. Application to a Known Distribution: Bernoulli
Let's apply these formulas to the Bernoulli distribution we learned about in the last lesson. This will connect our new knowledge back to a practical model for binary events.
Statistics: Probability mass functions(PMFs) - Mathematical ...
The article 'Statistics: Probability mass functions(PMFs) - Mathematical ...' provides clear derivations for the expected value and variance of several common distributions. We'll focus on the Bernoulli case.
Read the section '2.1 PMF of Bernoulli random variable'. The text provides concise mathematical derivations for both the expected value and the variance.
Let's recap the derivation from the article:
A Bernoulli random variable can be 1 (success) with probability or 0 (failure) with probability .
- Expected Value:
- Variance:
First, .
Then, .
Interpretation:
The result is intuitive. For an event like a button click with , the expected number of clicks in a single trial is 0.05.
The variance, , is maximized when (variance = 0.25). This makes sense: a fair coin flip has the highest uncertainty. The variance is minimized as approaches 0 or 1 (e.g., for , variance is 0.0099). This also makes sense: if an event is very rare or very common, the outcome is more predictable, and thus has lower variance.
4. Properties of Expectation and Variance
When working with models, you'll often perform linear transformations on variables (e.g., scaling data, calculating profits). It's helpful to know how expectation and variance behave under these transformations.
Let be a random variable, and let and be constants.
-
Expectation:
This property is called linearity of expectation. It behaves just as you'd expect. If you double all values () and add 10 (), the new mean is double the old mean plus 10. -
Variance:
Notice two things:- Adding a constant does not change the variance. This is because adding a constant shifts the entire distribution but doesn't change its spread.
- Multiplying by a constant scales the variance by . The factor is squared because variance itself is a squared measure.
These properties are extremely useful and are clearly explained in the following document.
Lesson 1: Discrete random variables, probability functions ...
This document from the University of Waterloo provides a concise summary of key probability concepts. We will look at a specific section on the properties of expectation and variance.
Read the section titled 'Properties of Expectation and Variance' (it's after 'You Try 3'). Focus on the two main properties and the worked-out 'Example 4' about the business deal, which clearly demonstrates how to apply them.
Conclusion
Excellent work today. You've now formalized your understanding of the two most important summary statistics for any probability distribution. These are not just theoretical concepts; they are the numbers you will use constantly to describe data, initialize models, and evaluate performance.
Here are the key takeaways:
- Expected Value is the theoretical, long-run average of a random variable, calculated as a weighted average of its possible outcomes.
- Variance measures the average squared deviation from the mean, quantifying the spread of a distribution.
- Standard Deviation is the square root of the variance and is often more interpretable as it's in the same units as the data.
- The computational formula is often the most practical way to calculate variance.
- Expectation and variance have predictable properties under linear transformations, such as scaling and shifting.
In our next lesson, we will move on to the final topic in our math foundations module: conditional probability and Bayes' theorem. This will shift our focus from describing a single variable to understanding the relationship between multiple variables—a concept at the very heart of learning from data.