MathalyseResearch

Learn AI · Chapter 3 · Probability and Information Theory

Random Variables

What a random variable is, why it needs a probability distribution before it says anything about chance, how to read the notation for a variable and its values, and how discrete and continuous variables differ.

Book §3.2, pages 56About 30 minutes
Section 1

A variable driven by chance

How to use this page

Click the colored symbols. Every symbol, in an equation or in the text, opens its meaning. Move the controls before you read the paragraph under each demo, and commit to a guess in every Predict first box before you look at the answer.

Colors: ink for values and counts, teal for random variables and probabilities, violet for shares and estimates, gold for widths.

values and counts random variables and probabilities shares and averages widths and small sizes

A random variable is a variable whose value is set by chance, so it can differ from one draw to the next. Think of a die. Before it lands, six outcomes are possible. After it lands, exactly one of them is the value. The random variable is the name for the thing that will show one of those values. A slot machine works the same way: its printed symbols are the states, and its odds are a separate matter.

Predict first. In a dry climate, sunny has a chance of 0.6 on each draw. In 300 draws, about how many are sunny?

The book adds a point that is easy to read past. On its own, is only a description of the states that are possible. It must be coupled with a probability distribution that says how likely each state is.

The same variable, three different distributions

chance of the state (the distribution)share of draws so far

The states are the same under all three tabs: sunny, cloudy and rainy. Only the distribution changes. The chances in each tab are invented for this demo and are not from the book. The strip at the bottom shows the last 30 draws, one square each.

The shares follow the distribution. The list of states only says what can happen, and it cannot say how often.

Challenge +8 XP

Switch to the wet climate, draw at least 300 times, and make rainy the most common state.

Section 2

Variable and value

Two things need two symbols. The random variable is the unknown, the thing that has not been drawn yet. The book writes the variable in plain type and its values in script letters. This module writes values in italic, so and are two values that can take.

The book gives and as possible values of . The statement that takes one of them is written

Click any coloured symbol to see what it means.

This display is added here and is not printed in the book. On any one draw, the variable equals one value or the other.

Draw a value, keep the variable

the most recent valueearlier draws (vector tab)
Challenge +8 XP

Keep drawing single values in the die tab until every state has come up at least once.

In the die tab, the variable has six possible states and each draw selects one. In the vector tab each value is a point in the plane. The points come from a toy cloud generated here from a seeded function for illustration. The cloud is not from the book. The whole cloud stands for the variable, and one point is one value.

Predict first. A vector-valued variable has two numbers in each value. After you draw 20 values, how many numbers have you written down?

For a vector-valued variable the book writes the random variable in bold, and one of its values in bold italic. Here the variable is , and one of its values is . An example is a pair of measurements taken from one person.

Click any coloured symbol to see what it means.

The book states this convention in a sentence and does not print a display. The display is added here. The difference between the two typefaces is the difference between the next card from a deck and the seven of hearts. The first has not happened. The second has.

Section 3

Discrete random variables

Predict first. Each flip shows heads with a chance of 0.5. What is the chance that a game needs more than 3 flips?

No largest state

chance of the stateshare of games so far

A discrete random variable has states that can be counted. Finite means a fixed list, such as the six faces of a die. Countably infinite means the states can be listed one after another without end, as 1, 2, 3 and so on.

The number of flips to the first head has this form: no state is the largest, and each state is still a separate value. The chart shows states 1 to 11 and gathers everything from 12 upwards into the last bar. A low chance of heads makes long games common.

Going deeper: the chance that the first head comes on flip (added)

This derivation is added here and is not printed in the book. The first head comes on flip when flips 1 to are all tails and flip is heads. Each flip is tails with chance , and assuming the flips are independent, the chances multiply:

Click any coloured symbol to see what it means.

Add these chances over every from 1 upwards and the total is 1. That is the sum of a geometric series. The chance of needing more than 3 flips is the chance that the first three flips are all tails, which is .

Challenge +8 XP

Lower the chance of heads, then play games until one game needs at least 30 flips.

Named states carry no numbers

The book adds that the states of a discrete variable need not be integers. They can be named states, labels with no numerical value. The color of a traffic light, a word in a sentence and the class of an image are all of this kind. A number given to a named state is a convenience, not a fact about the variable.

Predict first. Red, yellow and green have chances of 0.5, 0.1 and 0.4. Their labels are 1, 2 and 3. You relabel them 3, 2 and 1. What happens to the chances?

Numbers on named states are arbitrary

chance of each stateweighted average of the labels (diamond)

The chances stay fixed at 0.5, 0.1 and 0.4, but the average moves with the labels. An average that depends on a naming choice says nothing about the world, so averages of named states carry no meaning on their own.

Going deeper: the weighted average of the labels (added)

This definition is added here and is not printed in the book. Each label is multiplied by the chance of its state, and the products are added. With chances 0.5, 0.1 and 0.4 and labels 1, 2 and 3, the sum is 0.5 times 1, plus 0.1 times 2, plus 0.4 times 3, which is 1.9. Relabel the states 3, 2 and 1 with the same chances and the sum is 2.1.

Click any coloured symbol to see what it means.

The chances are attached to the states, so changing the numbers attached to the states changes the average and nothing else.

Section 4

Continuous random variables

A continuous random variable is associated with a real value: a position, a temperature, a time. Between any two real numbers there are infinitely many others, so the states cannot be listed one after another the way integers can. They form a continuum.

Predict first. A spinner picks a real number between 0 and 1, with every value equally likely. You set a window of width 0.2 around the middle. About what share of the 2000 points fall inside it?

Windows, not points

windowpoints insidepoints outside

Narrow the width and the number of points inside falls in proportion to it. When you move the center , the points inside change, but their number stays about the same as long as the window stays inside the interval.

Challenge +8 XP

Make the window hold between 180 and 220 of the 2000 points. The readout gives the count.

The count of points exactly on the center stays at zero however often you redraw, and this does not come from rounding. The box below gives the argument, which is added here and is not printed in the book.

Going deeper: why one exact value has zero chance (added)

This argument is added here and is not printed in the book. Suppose every exact value in the interval from 0 to 1 had the same positive chance. The interval contains infinitely many exact values, so the chances would add up to infinity, not to 1. The chance of each exact value must therefore be zero.

A window of width inside the interval covers a share of the values that equals its width, because equally likely values spread evenly across the interval:

Click any coloured symbol to see what it means.

Narrow the window and the share falls in step with it, which is the behavior the demo shows.

What to take away
  • A random variable describes the possible states. It needs a probability distribution to say how likely each state is.
  • The variable is written in plain upright type, , and its values in italic, such as . Vectors use bold type: for the variable and for one value.
  • A discrete variable has finitely many or countably infinitely many states. Its states may be names and need not be numbers.
  • A continuous variable is associated with a real value. In this module, chance attaches to ranges of values, not to single exact points.
Final quiz

Check yourself

Questions that mix the whole module. Each one is scored, and the score feeds your mastery of this module.

Source: Ian Goodfellow, Yoshua Bengio and Aaron Courville, Deep Learning, MIT Press, 2016, Section 3.2 (pages 56), free to read at deeplearningbook.org. Where the book prints equation numbers, the numbers here follow the book. The order of ideas follows the book. The explanations and all demos were written for this page.