Measure TheoryMath for LLMs

Measure Theory

Measure Theory

Exercises Notebook

Converted from exercises.ipynb for web reading.

Exercises: Sigma Algebras

There are 10 exercises. Exercises 1-3 are mechanics, 4-6 are theory, and 7-10 connect measure theory to ML systems.

Code cell 2

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

try:
    import seaborn as sns
    sns.set_theme(style="whitegrid", palette="colorblind")
    HAS_SNS = True
except ImportError:
    plt.style.use("seaborn-v0_8-whitegrid")
    HAS_SNS = False

mpl.rcParams.update({
    "figure.figsize":    (10, 6),
    "figure.dpi":         120,
    "font.size":           13,
    "axes.titlesize":      15,
    "axes.labelsize":      13,
    "xtick.labelsize":     11,
    "ytick.labelsize":     11,
    "legend.fontsize":     11,
    "legend.framealpha":   0.85,
    "lines.linewidth":      2.0,
    "axes.spines.top":     False,
    "axes.spines.right":   False,
    "savefig.bbox":       "tight",
    "savefig.dpi":         150,
})
np.random.seed(42)
print("Plot setup complete.")

Code cell 3


COLORS = {
    "primary":   "#0077BB",
    "secondary": "#EE7733",
    "tertiary":  "#009988",
    "error":     "#CC3311",
    "neutral":   "#555555",
    "highlight": "#EE3377",
}

def header(title):
    print("\n" + "=" * 72)
    print(title)
    print("=" * 72)

def check_true(condition, name):
    ok = bool(condition)
    print(f"{'PASS' if ok else 'FAIL'} - {name}")
    assert ok, name

def check_close(value, target, tol=1e-8, name="value"):
    ok = abs(float(value) - float(target)) <= tol
    print(f"{'PASS' if ok else 'FAIL'} - {name}: got {float(value):.6f}, expected {float(target):.6f}")
    assert ok, name

def powerset(universe):
    items = tuple(universe)
    out = [frozenset()]
    for item in items:
        out += [s | {item} for s in out]
    return set(out)

def sigma_generated(universe, generators):
    universe = frozenset(universe)
    family = {frozenset(), universe} | {frozenset(g) for g in generators}
    changed = True
    while changed:
        old = set(family)
        family |= {universe - a for a in old}
        for a in old:
            for b in old:
                family.add(a | b)
                family.add(a & b)
        changed = len(family) != len(old)
    return family

def is_sigma_algebra(universe, family):
    universe = frozenset(universe)
    family = {frozenset(a) for a in family}
    if frozenset() not in family or universe not in family:
        return False
    for a in list(family):
        if universe - a not in family:
            return False
        for b in list(family):
            if a | b not in family:
                return False
    return True

def simple_integral(values, masses):
    values = np.asarray(values, dtype=float)
    masses = np.asarray(masses, dtype=float)
    return float(np.sum(values * masses))

def pushforward(prob, mapping):
    out = {}
    for omega, mass in prob.items():
        y = mapping[omega]
        out[y] = out.get(y, 0.0) + float(mass)
    return out

def product_measure(p, q):
    return {(a, b): float(pa) * float(qb) for a, pa in p.items() for b, qb in q.items()}

def rn_derivative(target, proposal):
    ratio = {}
    for key, p_mass in target.items():
        q_mass = proposal.get(key, 0.0)
        if p_mass > 0 and q_mass <= 0:
            raise ValueError("target is not absolutely continuous with respect to proposal")
        ratio[key] = 0.0 if q_mass == 0 else float(p_mass) / float(q_mass)
    return ratio

def expectation_under(prob, values):
    return float(sum(prob[k] * values[k] for k in prob))

print("Measure-theory helpers ready.")

Exercise 1: Why measurable sets are needed (*)

State the measurable objects, compute the finite example, and interpret the ML relevance.

Code cell 5

# Your Solution - Exercise 1
answer = None
print("Your answer placeholder:", answer)

Code cell 6

# Solution
header("Exercise 1: Sigma Algebras")
universe = {0, 1, 2, 3}
family = sigma_generated(universe, [{0, 1}])
check_true(is_sigma_algebra(universe, family), "finite generated sigma algebra works")
print("Events:", sorted([sorted(s) for s in family]))
print("\nTakeaway: measure-theoretic notation keeps events, functions, measures, and densities separate.")

Exercise 2: Events observations and information in AI systems (*)

State the measurable objects, compute the finite example, and interpret the ML relevance.

Code cell 8

# Your Solution - Exercise 2
answer = None
print("Your answer placeholder:", answer)

Code cell 9

# Solution
header("Exercise 2: Sigma Algebras")
values = np.array([1.0, 3.0, 5.0])
masses = np.array([0.2, 0.5, 0.3])
integral = simple_integral(values, masses)
check_close(integral, 3.2, name="simple integral")
print("Integral:", integral)
print("\nTakeaway: measure-theoretic notation keeps events, functions, measures, and densities separate.")

Exercise 3: Countable operations vs finite operations (*)

State the measurable objects, compute the finite example, and interpret the ML relevance.

Code cell 11

# Your Solution - Exercise 3
answer = None
print("Your answer placeholder:", answer)

Code cell 12

# Solution
header("Exercise 3: Sigma Algebras")
prob = {"x1": 0.2, "x2": 0.3, "x3": 0.5}
mapping = {"x1": 0, "x2": 1, "x3": 1}
law = pushforward(prob, mapping)
check_close(law[1], 0.8, name="pushforward mass at 1")
print("Law:", law)
print("\nTakeaway: measure-theoretic notation keeps events, functions, measures, and densities separate.")

Exercise 4: Pathologies: why not every subset should be measurable (**)

State the measurable objects, compute the finite example, and interpret the ML relevance.

Code cell 14

# Your Solution - Exercise 4
answer = None
print("Your answer placeholder:", answer)

Code cell 15

# Solution
header("Exercise 4: Sigma Algebras")
p = {"a": 0.5, "b": 0.5}
q = {"left": 0.4, "right": 0.6}
joint = product_measure(p, q)
check_close(sum(joint.values()), 1.0, name="product total mass")
print("Joint:", joint)
print("\nTakeaway: measure-theoretic notation keeps events, functions, measures, and densities separate.")

Exercise 5: Historical bridge from set theory to probability (**)

State the measurable objects, compute the finite example, and interpret the ML relevance.

Code cell 17

# Your Solution - Exercise 5
answer = None
print("Your answer placeholder:", answer)

Code cell 18

# Solution
header("Exercise 5: Sigma Algebras")
target = {"a": 0.25, "b": 0.75}
proposal = {"a": 0.5, "b": 0.5}
ratio = rn_derivative(target, proposal)
values = {"a": 2.0, "b": 6.0}
direct = expectation_under(target, values)
weighted = sum(proposal[k] * ratio[k] * values[k] for k in proposal)
check_close(direct, weighted, name="change of measure")
print("Ratio:", ratio)
print("\nTakeaway: measure-theoretic notation keeps events, functions, measures, and densities separate.")

Exercise 6: Algebras of sets vs sigma algebras (**)

State the measurable objects, compute the finite example, and interpret the ML relevance.

Code cell 20

# Your Solution - Exercise 6
answer = None
print("Your answer placeholder:", answer)

Code cell 21

# Solution
header("Exercise 6: Sigma Algebras")
universe = {0, 1, 2, 3}
family = sigma_generated(universe, [{0, 1}])
check_true(is_sigma_algebra(universe, family), "finite generated sigma algebra works")
print("Events:", sorted([sorted(s) for s in family]))
print("\nTakeaway: measure-theoretic notation keeps events, functions, measures, and densities separate.")

Exercise 7: Generated sigma algebra σ(C)\sigma(\mathcal{C}) (***)

State the measurable objects, compute the finite example, and interpret the ML relevance.

Code cell 23

# Your Solution - Exercise 7
answer = None
print("Your answer placeholder:", answer)

Code cell 24

# Solution
header("Exercise 7: Sigma Algebras")
values = np.array([1.0, 3.0, 5.0])
masses = np.array([0.2, 0.5, 0.3])
integral = simple_integral(values, masses)
check_close(integral, 3.2, name="simple integral")
print("Integral:", integral)
print("\nTakeaway: measure-theoretic notation keeps events, functions, measures, and densities separate.")

Exercise 8: Borel sigma algebra on Rn\mathbb{R}^n (***)

State the measurable objects, compute the finite example, and interpret the ML relevance.

Code cell 26

# Your Solution - Exercise 8
answer = None
print("Your answer placeholder:", answer)

Code cell 27

# Solution
header("Exercise 8: Sigma Algebras")
prob = {"x1": 0.2, "x2": 0.3, "x3": 0.5}
mapping = {"x1": 0, "x2": 1, "x3": 1}
law = pushforward(prob, mapping)
check_close(law[1], 0.8, name="pushforward mass at 1")
print("Law:", law)
print("\nTakeaway: measure-theoretic notation keeps events, functions, measures, and densities separate.")

Exercise 9: Measurable spaces (Ω,F)(\Omega,\mathcal{F}) (***)

State the measurable objects, compute the finite example, and interpret the ML relevance.

Code cell 29

# Your Solution - Exercise 9
answer = None
print("Your answer placeholder:", answer)

Code cell 30

# Solution
header("Exercise 9: Sigma Algebras")
p = {"a": 0.5, "b": 0.5}
q = {"left": 0.4, "right": 0.6}
joint = product_measure(p, q)
check_close(sum(joint.values()), 1.0, name="product total mass")
print("Joint:", joint)
print("\nTakeaway: measure-theoretic notation keeps events, functions, measures, and densities separate.")

Exercise 10: Measurable maps and random variables (***)

State the measurable objects, compute the finite example, and interpret the ML relevance.

Code cell 32

# Your Solution - Exercise 10
answer = None
print("Your answer placeholder:", answer)

Code cell 33

# Solution
header("Exercise 10: Sigma Algebras")
target = {"a": 0.25, "b": 0.75}
proposal = {"a": 0.5, "b": 0.5}
ratio = rn_derivative(target, proposal)
values = {"a": 2.0, "b": 6.0}
direct = expectation_under(target, values)
weighted = sum(proposal[k] * ratio[k] * values[k] for k in proposal)
check_close(direct, weighted, name="change of measure")
print("Ratio:", ratio)
print("\nTakeaway: measure-theoretic notation keeps events, functions, measures, and densities separate.")
PreviousNext