Random Variables

A random variable turns outcomes into numbers.

If we roll a die, one random variable might be:

X = the number shown on the die

Another random variable might be:

Y = 1 if the roll is even
Y = 0 otherwise

The word "variable" can be misleading at first. A random variable is a function from outcomes to values. The randomness comes from the outcome we get.

For the die example, the outcome is the roll. The random variable Y maps each roll to either 1 or 0:

1 -> 0
2 -> 1
3 -> 0
4 -> 1
5 -> 0
6 -> 1

So the event "roll is even" and the random variable Y are related but not the same object. The event is the set {2, 4, 6}. The random variable is the rule that returns 1 on that event and 0 otherwise.

Why Random Variables Help

Once outcomes become numbers, we can compute with them.

We can ask for:

  • expected value
  • variance
  • probability of a range
  • relationships between variables

ML Reading

In ML, labels, inputs, predictions, and losses can all be treated as random variables.

For example, a label Y may be random because different examples have different labels. A prediction may be random because it depends on sampled data, model parameters, or stochastic generation.

Loss is also often treated as a random variable. Each sampled example may produce a different loss value. Training tries to reduce average loss over the data-generating process.

MATH-C07-T04-001Exercise: Read a random variable

For a die roll, define Y = 1 if the roll is even and 0 otherwise.

What is Y when the roll is 4?

Compute it first, then check your number.

Hint

Check whether 4 is even.

Solution

4 is even, so Y = 1. The random variable maps outcomes in the even event to the number 1.

MATH-C07-T04-002Exercise: Map an outcome

For the same random variable Y, what is Y when the roll is 5?

Compute it first, then check your number.

Hint

Check whether 5 is even.

Solution

5 is not even, so Y = 0. Outcomes outside the even event are mapped to the number 0.

MATH-C07-T04-003Exercise: Random variable or event

Is Y = 1 if the roll is even, 0 otherwise a random variable or an event?

Answer it first, then check.

Hint

It assigns a number to each outcome.

Solution

It is a random variable because it maps each outcome to a number.

MATH-C07-T04-004Exercise: Loss as a random variable

If each sampled example produces a different loss value, can the loss be treated as a random variable?

Answer it first, then check.

Hint

A random variable is a numerical quantity depending on uncertain outcomes.

Solution

Yes. If the sampled example is uncertain and the loss is a number computed from that example, the loss can be treated as a random variable.

MATH-C07-T04-005Exercise: Event versus indicator variable

For a die roll, let Y = 1 if the roll is even and 0 otherwise.

Enter 1 if the event is {2, 4, 6} and Y is the numerical rule attached to that event.

Compute it first, then check your number.

Hint

An event is a subset of the sample space. A random variable returns numbers.

Solution

Enter 1. {2,4,6} is the event. Y is the function that maps rolls in that event to 1 and all other rolls to 0.

Before Moving On

A random variable lets us use arithmetic on uncertain outcomes.