import numpy as np
print('Ready')Lecture 3 — Beginner tutorial
Random variables as maps from Omega to numbers
Lecture 3 Tutorial — Random variables (Beginner)
Based on Lec 03.
Big idea
Sample space can be abstract (H / T).
A random variable is a function that turns those outcomes into numbers.
Run cells with Shift+Enter.
Step 0 — Imports
Step 1 — Probability triplet (quick recall)
- Omega = sample space
- Events = subsets
- P = probability measure
Example: fair coin, Omega = {H, T}.
Omega = {"H", "T"}
def P(event):
event = set(event)
return sum(0.5 for o in event if o in Omega)
print("Omega =", Omega)
print("P({H}) =", P({"H"}))Step 2 — Why we need a random variable
We cannot do much arithmetic with the labels H and T.
Define a map X: Omega -> real numbers, for example:
- X(H) = 1
- X(T) = 0
That map is the random variable (a deterministic function!).
def X(omega):
"""Random variable: abstract outcome -> number."""
return 1 if omega == "H" else 0
for outcome in sorted(Omega):
print(f"X({outcome}) = {X(outcome)}")Step 3 — Data = values in the range of X
When we “observe” many coin tosses, we store numbers (0/1), not the letters.
Those numbers are elements of the range of X.
rng = np.random.default_rng(0)
# simulate abstract outcomes, then map through X
abstract = rng.choice(["H", "T"], size=10)
data = np.array([X(o) for o in abstract])
print("Abstract outcomes:", abstract)
print("Observed data (range of X):", data)
print("Mean of data ≈ P(H) for fair coin:", data.mean())Step 4 — Vector-valued random variable (ML flavor)
For images, X maps Omega -> R^d (a long vector of pixels).
Tiny demo: pretend each outcome maps to a 2-D “feature vector”.
def X_vec(omega):
# toy features: [is_heads, bias_term]
return np.array([1.0, 1.0]) if omega == "H" else np.array([0.0, 1.0])
print("X_vec(H) =", X_vec("H"))
print("X_vec(T) =", X_vec("T"))
print("shape:", X_vec("H").shape) # like a mini R^2 datapointStep 5 — Mini exercises
# Exercise A: redefine X so X(H)=+1 and X(T)=-1 (common coding).
# Compute the average of 5000 simulated values. What do you expect for a fair coin?
def X_pm(omega):
return 1 if omega == "H" else -1
sim = rng.choice(["H", "T"], size=5000)
vals = np.array([X_pm(o) for o in sim])
print("sample mean:", vals.mean()) # expect near 0# Exercise B: die Omega = {1,...,6}, X(omega)=omega (identity).
# This is already numeric — but still formally a random variable Omega -> R.
Omega_die = {1, 2, 3, 4, 5, 6}
def X_die(omega):
return float(omega)
print([X_die(o) for o in sorted(Omega_die)])Recap
| Lecture idea | Here |
|---|---|
| Probability triplet | Omega, events, P |
| Need numbers | cannot stop at H/T labels |
| Random variable | function X: Omega -> R or R^d |
| Datapoint | value in the range of X |
Next lectures: pushforward measure / distribution of X (what happens to P).