UC Berkeley EECS/CS 126 — Probability and Random Processes
Applied probability, implemented and verified in Python — an independent,
from-scratch implementation of EECS 126 / CS 126 — Probability and Random
Processes (UC Berkeley), part of a csdiy.wiki
full-catalog build.

Overview
EECS 126 is Berkeley’s upper-division course on probability theory and random
processes — the mathematics behind communication, inference, and control. This
repo implements all nine programming labs from scratch as clean, tested
Python modules, and provides solution write-ups plus verifying simulations for
a representative set of the fourteen written homeworks. Every result is
measured, not asserted: each lab runs end-to-end on CPU and its output is
checked against the closed-form theory (empirical mean → analytic expectation,
simulated stationary distribution → eigenvector, filter covariance → Riccati
solution, and so on).
Results (measured on Windows, CPU-only, Python 3.11)
Every number below is reproduced by python run_all.py; the full log is saved
to results/run_log.txt.
| Lab |
What it does |
Result (measured vs theory) |
| 1 · Monte Carlo |
estimate probabilities/expectations by simulation |
P(flush) sim 0.001982 vs theory 0.001981; E[flips→HHH] sim 13.98 vs 14 |
| 2 · Fountain codes |
LT codes + peeling decoder over an erasure channel |
decodes k=200 from 1.306× packets, recovery byte-exact |
| 3 · Matrix sketching |
JL, sketched least-squares, randomized SVD |
randomized rank-20 SVD error ratio 1.0000 vs Eckart-Young; sketched LS ratio 1.07 |
| 4 · MCMC decryption |
break a substitution cipher with Metropolis-Hastings |
99.8% character accuracy (English bigram model from War and Peace) |
| 5 · Random graphs |
Erdős–Rényi thresholds + SBM community recovery |
giant-component max gap 0.046 vs s=1−e^{−cs}; SBM spectral recovery 0.963 |
| 6 · Interacting particles |
SIS epidemic / contact process |
complete-graph steady state within 0.013 of mean-field fixed point; phase transition at R₀=1 |
| 7 · Guess My Word |
optimal frequency-aware guessing (entropy) |
optimal BST 5.88 guesses = simulated 5.89, vs blind log₂n = 9.23 |
| 8 · Hypothesis testing |
LRT / Neyman-Pearson detection |
ROC vs Q-function max gap 0.005; error decay Q(√n·μ/2) matches at every n |
| 9 · Kalman filter |
2-D tracking / sensor fusion |
filtered RMSE 1.33 vs raw 2.01 (1.5×); covariance → Riccati to 1.5e-14 |
Homework simulations (see homework/) additionally verify
the CLT (KS distance → 0.019), Poisson-process superposition/thinning, Markov
hitting times, PageRank (eigenvector = power iteration = random surfer, to 1e-16
/ 5e-4), and MMSE = LLSE for jointly Gaussian variables.
Example figure — the SIS contact process phase transition and a spatial
snapshot of the 2-D grid version (results/lab06_sis_ips.png):

Implemented assignments
Programming labs (all nine, fully implemented and verified):
Written homework (representative solution write-ups + simulations):
Project structure
ucb-cs126/
├── labs/ # the 9 programming labs (one package each)
│ ├── common.py # shared plotting / results helpers
│ ├── lab01_monte_carlo/ # {module}.py + run.py per lab
│ ├── lab02_fountain_codes/
│ ├── lab03_matrix_sketching/
│ ├── lab04_mcmc_decrypt/
│ ├── lab05_random_graphs/
│ ├── lab06_interacting_particles/
│ ├── lab07_guess_my_word/
│ ├── lab08_hypothesis_testing/
│ └── lab09_kalman_filter/
├── homework/
│ ├── writeups/ # concise math solution write-ups (Markdown)
│ └── simulations/ # Python simulations that reproduce each result
├── tests/test_theory.py # 20 simulation-vs-theory assertions
├── results/ # captured outputs: run_log.txt + 9 figures
├── run_all.py # run every lab + simulation, capture the log
└── requirements.txt
How to run
# Python repos use the shared csdiy env (Python 3.11):
# D:\Project\_csdiy\.venv-ml\Scripts\python.exe
python -m pip install -r requirements.txt # numpy, scipy, matplotlib, pytest
# Run a single lab (prints measured vs theory, writes a figure to results/):
python -m labs.lab09_kalman_filter.run
# Run everything and capture the full log + all figures:
python run_all.py
# Verify simulation-vs-theory agreement:
python -m pytest tests/ -q
Lab 4 downloads War and Peace from Project Gutenberg into data/ at runtime
(git-ignored); if the network is unavailable it falls back to a bundled English
sample so the lab still runs.
Verification
pytest tests/ → 20 passed in ~32 s. Each test fixes a seed and asserts a
simulated quantity matches its analytic value within tolerance — e.g. the
randomized SVD error is within 1.05× of the Eckart-Young optimum, the Kalman
covariance converges to the Riccati solution (< 1e-6), the SBM is recovered
above threshold (> 0.9) and fails below it (< 0.7), and the optimal BST cost
respects the H(p)/log₂3 entropy lower bound.
results/run_log.txt is the captured output of a full run_all.py run
(all nine labs + five homework simulations), reproducing every number in the
table above.
- Nine figures under
results/ (convergence curves, ROC, tracking, phase
transitions, …) are regenerated by the same run.
Tech stack
Python 3.11 · NumPy · SciPy (stats, linalg, optimize, sparse.linalg) ·
Matplotlib · pytest. No machine-learning frameworks — everything (LT decoder,
randomized SVD, MH sampler, spectral clustering, optimal-BST DP, Kalman filter)
is implemented directly.
Key ideas / what I learned
- Simulation as proof of understanding. Every closed form in the course has
a matching experiment here; when the empirical number lands on the analytic
one, the derivation is validated end-to-end.
- Markov chains are everywhere. Stationary distributions power PageRank,
hitting times solve gambler’s ruin, MCMC turns an intractable posterior over
cipher keys into a tractable random walk, and interacting particle systems
lift a single chain to a whole lattice.
- Randomness makes linear algebra cheap. Sketching and randomized SVD trade
a controllable
(1±ε) error for large speedups — and the error really is
controllable, matching the JL bound.
- Estimation and detection are geometry. The orthogonality principle (LLSE),
the coincidence MMSE = LLSE for Gaussians, and the likelihood-ratio threshold
all fall out of projecting onto the right subspace.
Credits & license
Based on the assignments of EECS 126 / CS 126 — Probability and Random
Processes by Professors Kannan Ramchandran, Abhay Parekh, and Jean Walrand
(UC Berkeley). Official course page:
https://inst.eecs.berkeley.edu/~ee126/. This repository is an independent
educational reimplementation; all course materials, problem statements, and
datasets belong to their original authors. Original code in this repo is
released under the MIT License.
评论