Part I · Foundations | Chapter 7
Math Refresher for Statistics 🐍 Notebook
Six bite-sized demos covering exactly the math you need: summation, sets, counting, logarithms, a pinch of calculus, and the linear algebra of data, each tied straight to statistics.
Author: John Fisher · Statistics, Data Science and AI: A Visual Handbook · 2026
🎯 What you'll build in this notebook¶
| # | Demo | Statistics it powers |
|---|---|---|
| 1 | Summation Σ → mean & variance | Reading every stats formula |
| 2 | Sets & a Venn diagram | Probability rules |
| 3 | Permutations & combinations | Counting & odds |
| 4 | Logarithms | Log scales, likelihood |
| 5 | A pinch of calculus | Optimization / model training |
| 6 | Vectors & matrices | Data as a table the computer can crunch |
⚙️ Setup, imports & the book's plotting style¶
In [1]:
import numpy as np
import pandas as pd
import math
import matplotlib.pyplot as plt
NAVY="#0a1230"; INK="#1a2138"; INK_SOFT="#4a5578"
CYAN="#0891b2"; PURPLE="#7c3aed"; AMBER="#d97706"
GREEN="#059669"; PINK="#db2777"; BLUE="#2563eb"; GRID="#e6e9f2"
plt.rcParams.update({
"figure.facecolor":"white","axes.facecolor":"white","figure.dpi":110,"font.size":11,
"axes.edgecolor":GRID,"axes.linewidth":1.2,"axes.grid":True,"grid.color":GRID,"axes.axisbelow":True,
"axes.spines.top":False,"axes.spines.right":False,"axes.titlesize":15,"axes.titleweight":"bold","axes.titlecolor":INK,
"axes.labelcolor":INK_SOFT,"axes.labelsize":11.5,"xtick.color":INK_SOFT,"ytick.color":INK_SOFT,"legend.frameon":False,
})
def titlecard(ax, title, subtitle=None):
ax.set_title(title, loc="left", pad=18)
if subtitle:
ax.text(0, 1.02, subtitle, transform=ax.transAxes, fontsize=10.5, color=INK_SOFT, va="bottom")
print("✅ Environment ready.")
✅ Environment ready.
DEMO 1 · SUMMATION (Σ)
➕ The symbol behind every stats formula
Σ just means "add them all up." The mean is (1/n)·Σxᵢ and the variance is (1/n)·Σ(xᵢ−mean)². Let's prove the formulas equal NumPy.
In [2]:
x = np.array([4, 8, 6, 5, 3, 9, 7])
n = len(x)
# Mean from the formula: x̄ = (1/n) Σ xᵢ
mean_formula = x.sum() / n
# Variance from the formula: σ² = (1/n) Σ (xᵢ − x̄)²
var_formula = ((x - mean_formula)**2).sum() / n
print(f"Σ xᵢ = {x.sum()}")
print(f"mean (formula) = {mean_formula:.3f} | np.mean = {np.mean(x):.3f}")
print(f"var (formula) = {var_formula:.3f} | np.var = {np.var(x):.3f}")
print("\nThe Greek Σ is just a for-loop that adds things up.")
Σ xᵢ = 42 mean (formula) = 6.000 | np.mean = 6.000 var (formula) = 4.000 | np.var = 4.000 The Greek Σ is just a for-loop that adds things up.
DEMO 2 · SETS & VENN DIAGRAMS
🔵 The language of probability
Events are sets. Union (A∪B = "A or B"), intersection (A∩B = "A and B"), and difference power the probability rules in Part V.
In [3]:
A = {2, 4, 6, 8, 10}
B = {3, 6, 9}
print("A ∪ B (union) =", A | B)
print("A ∩ B (intersection) =", A & B)
print("A − B (difference) =", A - B)
A ∪ B (union) = {2, 3, 4, 6, 8, 9, 10}
A ∩ B (intersection) = {6}
A − B (difference) = {8, 2, 10, 4}
In [4]:
fig, ax = plt.subplots(figsize=(7.5,4.2))
ax.add_patch(plt.Circle((0.40,0.5), 0.30, color=CYAN, alpha=0.30))
ax.add_patch(plt.Circle((0.62,0.5), 0.30, color=PURPLE, alpha=0.30))
ax.text(0.27,0.5, "A only\n2,4,8,10", ha="center", va="center", fontsize=11, color=INK)
ax.text(0.51,0.5, "A∩B\n{6}", ha="center", va="center", fontsize=11, fontweight="bold", color=INK)
ax.text(0.74,0.5, "B only\n3,9", ha="center", va="center", fontsize=11, color=INK)
ax.text(0.30,0.86,"A", fontsize=16, fontweight="bold", color=CYAN)
ax.text(0.72,0.86,"B", fontsize=16, fontweight="bold", color=PURPLE)
ax.set_xlim(0,1); ax.set_ylim(0,1); ax.set_aspect("equal"); ax.axis("off")
ax.set_title("A Venn diagram of two sets", loc="left", fontsize=14)
plt.tight_layout(); plt.show()
DEMO 3 · PERMUTATIONS & COMBINATIONS
🔢 Counting the possibilities
Permutations count arrangements (order matters); combinations count selections (order doesn't). This is the backbone of probability and the binomial distribution.
In [5]:
# From 5 people, arrange 3 in a row (order matters) -> PERMUTATIONS
print(f"Permutations P(5,3) = {math.perm(5,3)} (order matters)")
# From 5 people, choose a committee of 3 (order does NOT matter) -> COMBINATIONS
print(f"Combinations C(5,3) = {math.comb(5,3)} (order does not matter)")
# Real odds: a 6-from-49 lottery
ways = math.comb(49, 6)
print(f"\nLottery: choose 6 numbers from 49 -> {ways:,} combinations")
print(f"Your chance of winning: 1 in {ways:,}")
Permutations P(5,3) = 60 (order matters) Combinations C(5,3) = 10 (order does not matter) Lottery: choose 6 numbers from 49 -> 13,983,816 combinations Your chance of winning: 1 in 13,983,816
DEMO 4 · LOGARITHMS
📐 Taming huge numbers & products
Logs turn multiplication into addition and squeeze enormous ranges onto a readable scale, used everywhere from log-likelihood to log-scale charts.
In [6]:
a, b = 1000, 100
print("log(a·b) =", round(np.log(a*b), 4))
print("log(a) + log(b) =", round(np.log(a) + np.log(b), 4), " ← same! products become sums")
# Why a log scale helps: exponential growth looks linear on a log axis
x = np.arange(1, 11)
y = 2.0**x # 2, 4, 8, ... 1024 (explodes)
fig, (a1, a2) = plt.subplots(1, 2, figsize=(11,4.2))
a1.plot(x, y, "o-", color=GREEN); a1.set_title("Linear scale: small values hide", loc="left", fontsize=12.5)
a1.set_xlabel("x"); a1.set_ylabel("2ˣ")
a2.plot(x, y, "o-", color=PURPLE); a2.set_yscale("log")
a2.set_title("Log scale: it becomes a line", loc="left", fontsize=12.5)
a2.set_xlabel("x"); a2.set_ylabel("2ˣ (log axis)")
plt.tight_layout(); plt.show()
log(a·b) = 11.5129 log(a) + log(b) = 11.5129 ← same! products become sums
DEMO 5 · A PINCH OF CALCULUS
📉 Slopes that find the minimum
A derivative is the slope of a curve. Setting the slope toward zero is how models "learn", here we minimize a simple loss with gradient descent (the engine of ML training).
In [7]:
# Minimize the loss f(x) = (x − 3)² whose derivative is f'(x) = 2(x − 3)
f = lambda x: (x - 3)**2
grad = lambda x: 2*(x - 3)
x = 0.0; lr = 0.18; path = [x]
for _ in range(15):
x = x - lr*grad(x) # step downhill, opposite the slope
path.append(x)
path = np.array(path)
print(f"Started at x=0, slid to x={path[-1]:.3f} (true minimum is x=3)")
xs = np.linspace(-1, 7, 200)
fig, ax = plt.subplots(figsize=(9,4.6))
ax.plot(xs, f(xs), color=INK, lw=2)
ax.plot(path, f(path), "o-", color=AMBER, markersize=8, markeredgecolor="white", lw=1.5)
ax.scatter([3],[0], color=PINK, s=120, zorder=5, marker="*", edgecolor="white", label="minimum (x=3)")
titlecard(ax, "Gradient descent rolls downhill to the minimum", "each step moves opposite the slope")
ax.set_xlabel("x"); ax.set_ylabel("loss f(x)"); ax.legend(fontsize=9)
plt.tight_layout(); plt.show()
Started at x=0, slid to x=2.996 (true minimum is x=3)
DEMO 6 · VECTORS & MATRICES
🔢 Data is a matrix
A dataset is just a grid of numbers, a matrix (rows = records, columns = features). Linear algebra lets the computer crunch the whole table at once.
In [8]:
# Rows = people, columns = [height_cm, weight_kg] → a 4×2 matrix
X = np.array([[170, 70],
[160, 55],
[180, 82],
[175, 77]])
print("Data matrix X (shape", X.shape, "):")
print(X)
# Column means in ONE operation (no loop)
print("\nMean of each column [height, weight]:", X.mean(axis=0))
# Matrix × vector: combine columns with weights (e.g., a simple body index)
weights = np.array([0.5, 1.0])
scores = X @ weights # the @ operator is matrix multiplication
print("\nX @ weights (one weighted score per person):", scores)
# --- added visual ---
import matplotlib.pyplot as plt, numpy as np
_CY,_PU,_AM,_GR,_PK,_INK,_GRY,_BL = "#0891b2","#7c3aed","#d97706","#059669","#db2777","#1a2138","#c7ccda","#2563eb"
fig, ax = plt.subplots(figsize=(4.8, 3.8))
im = ax.imshow(X, cmap="Blues", aspect="auto")
ax.set_xticks([0,1]); ax.set_xticklabels(["height_cm","weight_kg"])
ax.set_yticks(range(4)); ax.set_yticklabels([f"person {i+1}" for i in range(4)])
for i in range(4):
for j in range(2):
ax.text(j, i, X[i,j], ha="center", va="center", fontweight="bold", color="white" if X[i,j]>120 else _INK)
ax.set_title("Data as a matrix: rows = people, columns = features"); ax.grid(False)
plt.tight_layout(); plt.show()
Data matrix X (shape (4, 2) ): [[170 70] [160 55] [180 82] [175 77]] Mean of each column [height, weight]: [171.25 71. ] X @ weights (one weighted score per person): [155. 135. 172. 164.5]
Every model in this book ultimately runs on matrices like X, linear algebra is how statistics scales from a handful of rows to millions.
🎓 Recap, the math toolkit
- Σ (summation) is "add them all up", the heart of mean and variance.
- Sets (∪, ∩) are the language of probability events.
- Permutations vs combinations count arrangements vs selections.
- Logarithms turn products into sums and tame huge ranges.
- Derivatives are slopes, gradient descent uses them to train models.
- Matrices hold your data so it can all be computed at once.
That wraps Part I, Foundations. Next: Part II, where we start describing data.
Statistics, Data Science and AI: A Visual Handbook · © 2026 John Fisher