Random Variables
What is a Random Variable?
A random variable is a function from the sample space to the real numbers:
Formally, it assigns a numerical value to each outcome.
Why Functions?
Outcomes (like “three coin flips”) are often complex or qualitative. Random variables let us work numerically: counting heads, summing dice, measuring time.
Example: Coin Flips
Flip three coins. The outcome is a sequence like . Define = number of heads.
| Outcome | |
|---|---|
| HHH | 3 |
| HHT | 2 |
| HTH | 2 |
| HTT | 1 |
| THH | 2 |
| THT | 1 |
| TTH | 1 |
| TTT | 0 |
Discrete vs Continuous Random Variables
Discrete
is discrete if it takes values from a finite or countably infinite set.
Examples:
- Number of heads in 10 flips:
- Rolls until first 6:
- Number of customers arriving:
Continuous
is continuous if it can take any value in an interval.
Examples:
- Time until failure:
- Temperature:
- Height: cm
Indicator Variables
An indicator variable (or Bernoulli variable) for event is:
Properties
The expected value of an indicator equals the probability of the event.
This is incredibly useful: to compute , compute .
Example: Matching Cards
Shuffle a deck. What is the expected number of cards in their original position?
Let indicate card is in position . Then counts matches.
By linearity:
On average, exactly one card is in its original position, regardless of deck size.
Types of Random Variables
| Type | Values | Example |
|---|---|---|
| Indicator | Success/failure | |
| Binary | (or ) | Yes/no outcomes |
| Count | Number of arrivals | |
| Continuous | Real interval | Time, weight |
Random Variables in Programming
Think of a random variable like a variable in a programming language:
- It has a type (discrete or continuous)
- It has a range of possible values
- It has a distribution describing likelihood of each value
- It can be sampled to produce a concrete value
Example in Context
# X is a random variable
# - Type: discrete
# - Range: {1, 2, 3, 4, 5, 6}
# - Distribution: uniform
import random
x = random.randint(1, 6) # Sample a value
Functions of Random Variables
If is a random variable and is a function, then is also a random variable.
Example: If is the roll of a die, then takes values .
Summary
| Concept | Definition |
|---|---|
| Random variable | Function |
| Discrete RV | Takes countably many values |
| Continuous RV | Takes values in an interval |
| Indicator |