A three-way table has nine hierarchical log-linear models that keep all three main effects, ordered from mutual independence (bottom) to the saturated model (top); each step up adds one interaction. Every model is fitted by iterative proportional fitting. The deviance compares a model with the saturated one; AIC = ℓ − |M| and BIC = ℓ − |M| log(n)/2 trade fit against size (larger is better, the book's convention). The backward search starts at the top and removes one term at a time while the criterion improves.
Simulated tables: n draws from the cell probabilities of the chosen model fitted to the breast cancer data, by inversion with the seeded generator (seed in the readout). |M| counts ψ∅, as the book does; the deviance has N − |M| degrees of freedom (N = number of cells). "Smallest model not rejected" picks, among the models whose deviance test does not reject, one with the fewest parameters (ties: the larger p-value).
code/ch19_log_linear_models.py (fit_model, select, backward_search, js_multinomial; Figures 19.S2 and 19.S4).