Expectation, Variance, and Covariance
A distribution contains everything there is to know about a random variable, but we often want a few numbers that summarize it. The expectation summarizes where a distribution is centered, the variance how spread out it is, and the covariance how two random variables move together. The conditional expectation then asks what we expect once we know something about the outcome, and it is the quantity that value functions in reinforcement learning are built on.
This chapter covers
- the expectation of a random variable and of a function of it,
- linearity of expectation,
- variance and standard deviation,
- covariance, correlation, the variance of a sum, and the covariance matrix, and
- conditional expectation and the law of total expectation.
Expectation
The expectation (or expected value) of a random variable is the average of its values, each weighted by its probability,
It is also called the mean of and written . If you repeat the experiment many times and average the results, the average approaches , which chapter 5 makes precise.
For a fair d20, every face has probability , so
The expected value is a value the die can never show. It describes the long-run average, not a typical roll.
For the crit indicator , . In general, the expectation of the indicator of an event is the probability of that event.
Expectation of a Function
To find the expectation of , weight by the probabilities of . There's no need to work out the distribution of first,
For the d20, . This is not . In general unless is linear.
Linearity
Expectation is linear. For any random variables and and constants , and ,
This holds whether or not and are related, which makes it one of the most useful facts in probability. A damage roll of 2d6+3 is , where and are the two six-sided dice. Each die has mean , so without ever writing out the distribution of .
In RL, the return satisfies , and linearity gives , the first step toward the Bellman equations.
Variance and Standard Deviation
The variance measures how far a random variable typically falls from its mean. It is the expected squared distance from the mean,
Expanding the square and using linearity gives a shortcut that is usually easier to compute,
For the d20, . Because the variance is in squared units, we often report the standard deviation instead, which has the same units as . A d20 roll has a standard deviation of about .
Shifting a random variable doesn't change its spread, and scaling it scales the standard deviation by the same factor,
Covariance and Correlation
The covariance of two random variables measures whether they tend to be above their means at the same time,
It is positive when large values of come with large values of , negative when large values of one come with small values of the other, and . Its size depends on the units of and , so the correlation rescales it to lie between and ,
A correlation of means is an exact linear function of .
Independent random variables, whose values don't affect each other's probabilities, have zero covariance. The converse is false, because covariance only detects linear relationships. If is equally likely to be , or and , then , even though is completely determined by .
Variance of a Sum
Covariance is what makes variances of sums more complicated than expectations of sums,
When and are independent the covariance term vanishes and the variances simply add. The two dice in a 2d6+3 damage roll are independent and each has variance , so . The same rule for independent random variables, , is the reason averages become less noisy as grows, which is the subject of chapter 5.
Covariance Matrix
For a random vector , the mean vector collects the means of the coordinates, and the covariance matrix collects all their pairwise covariances,
The diagonal holds the variances of the coordinates. A covariance matrix is always symmetric and positive semidefinite. Linear transformations act on it in a simple way,
The covariance matrix is the second parameter of the multivariate normal distribution.
Conditional Expectation
Often we learn something about an outcome and want to update what we expect. The conditional expectation of given is the expectation computed with the conditional distribution of once is known,
Back to the dragon fight. The paladin hits armor class 15 with probability , deals 2d6+3 damage on a hit and nothing on a miss (ignoring the extra dice on a critical hit). Let be the damage and indicate a hit. Then and .
Before is observed, is itself a random variable, a function of that takes the value when .
Law of Total Expectation
Averaging the conditional expectations over the possible values of recovers the overall expectation,
This is the law of total expectation. For the paladin, the expected damage per attack is .
The value of a state in RL is a conditional expectation, . The Bellman equation for comes from applying the law of total expectation twice, first conditioning on the action and then on the next state and reward, together with linearity and the Markov property (see Markov Decision Processes). In supervised learning, the prediction of from that minimizes the expected squared error is , which is what a regression model tries to approximate.
Recap
- The expectation is the probability-weighted average of a random variable, and is computed by weighting with the probabilities of .
- Expectation is linear for any random variables, dependent or not.
- The variance is the expected squared distance from the mean, and .
- Covariance measures linear co-movement, independence implies zero covariance but not the other way around, and variances of independent random variables add.
- The covariance matrix collects the pairwise covariances of a random vector.
- The conditional expectation updates the expectation once is known, and averaging it over gives back .
1. What is the expected damage of a 2d6+3 roll?
2. If , what is ?
3. Two random variables have . What can you conclude?
What's Next
So far we have computed expectations from known distributions. In practice the distribution is unknown and we only have samples. Sampling Distributions and the Central Limit Theorem studies how well the average of a sample estimates the expectation.