import numpy as np
print("Ready")Lecture 2 — Beginner tutorial
Sample space and probability measure by hand
Lecture 2 Tutorial — Sample space & probability (Beginner)
Based on Lec 02.
Goal
Feel these ideas with tiny examples:
- Random experiment → you choose what it is
- Sample space Ω → all outcomes
- Events → subsets of Ω
- Probability measure P → numbers in [0,1] with simple rules
Run cells top to bottom with Shift+Enter.
Step 0 — Imports
Step 1 — Choose a random experiment
Lecture point: the experiment is your design choice.
We take: one fair coin toss.
Outcomes are not numbers yet — just labels.
Omega = {"H", "T"} # sample space
print("Sample space Ω =", Omega)
print("Number of outcomes =", len(Omega))Step 2 — Events = subsets
| Event (English) | As a set |
|---|---|
| Heads | {H} |
| Tails | {T} |
| Anything | {H, T} = Ω |
| Impossible | empty set = ∅ |
event_heads = {"H"}
event_tails = {"T"}
event_anything = Omega
event_impossible = set()
print("Heads event:", event_heads)
print("Impossible event:", event_impossible)Step 3 — A probability measure on this tiny space
For a fair coin:
- P({H}) = P({T}) = 1/2
- P(Ω) = 1
- P(∅) = 0
Check lecture properties: non-negative, P(Ω)=1, disjoint additivity.
def P_fair_coin(event):
"""Probability of an event for a fair coin (event subset of {H,T})."""
event = set(event)
if not event.issubset(Omega):
raise ValueError("Event must be a subset of Omega")
# fair: each singleton outcome has mass 1/2
return sum(0.5 for outcome in event)
print("P(H) =", P_fair_coin({"H"}))
print("P(T) =", P_fair_coin({"T"}))
print("P(Omega) =", P_fair_coin(Omega))
print("P(empty) =", P_fair_coin(set()))
# Disjoint additivity: H and T do not overlap
print("P(H)+P(T) =", P_fair_coin({"H"}) + P_fair_coin({"T"}))Step 4 — Different experiment → different Ω
Same physical coin, new experiment: toss twice.
Ω₂ = {HH, HT, TH, TT}
This matches the lecture: you choose the experiment.
Omega2 = {"HH", "HT", "TH", "TT"}
print("Two-toss sample space:", Omega2)
# Event: exactly one head
exactly_one_head = {o for o in Omega2 if o.count("H") == 1}
print("Exactly one head:", exactly_one_head)
# Fair independent tosses -> each of 4 outcomes has probability 1/4
P_exact = len(exactly_one_head) / len(Omega2)
print("P(exactly one head) =", P_exact)Step 5 — Simulate many outcomes (statistical viewpoint)
Lecture: when physics is hard, repeat observations and use statistics.
Simulate many fair tosses; the fraction of heads should approach 1/2.
rng = np.random.default_rng(0)
n = 10_000
tosses = rng.choice(["H", "T"], size=n)
frac_heads = np.mean(tosses == "H")
print(f"In {n} tosses, fraction of heads ≈ {frac_heads:.3f}")
print("True P(H) for fair coin = 0.5")Step 6 — Mini exercises
# Exercise A: unfair coin with P(H)=0.7
def P_unfair(event, p_heads=0.7):
event = set(event)
mass = {"H": p_heads, "T": 1 - p_heads}
return sum(mass[o] for o in event)
print("Unfair P(H) =", P_unfair({"H"}))
print("Unfair P(T) =", P_unfair({"T"}))
print("Unfair P(Omega) =", P_unfair({"H", "T"}))# Exercise B: die Omega = {1,...,6}, fair — what is P(even)?
Omega_die = {1, 2, 3, 4, 5, 6}
even = {2, 4, 6}
P_even = len(even) / len(Omega_die)
print("P(even) =", P_even)Recap
| Lecture idea | In this notebook |
|---|---|
| Random experiment | coin once / twice / die |
| Sample space Ω | Python set of outcomes |
| Event | subset |
| Probability measure | function → [0,1] with rules |
| Statistical view | simulate many tosses |
Next: random variables (turning abstract outcomes into numbers/vectors).