MachineSex/figures/plot_architectures.py
Giorgio Gilestro 84124de143 Manuscript revision and pending experiment work, snapshot before restructuring
Clarity pass over the main text (36-item audit), Discussion rewrite and cut,
acknowledgements, Souly et al. as ref 62, lettered SI panels, model section
moved under Results; plus the untracked curriculum/society/compose/smol
configs, runners, figures, stats and tests that the SI already cites.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y64o8FKP7rCuXzC48pxpMm
2026-09-13 16:54:09 +01:00

81 lines
3.5 KiB
Python

"""`architectures` figure — the Wright-Fisher collapse operator is architecture-general.
The same dry-collapse / grounding-rescue signature appears in three distinct inductive biases:
the exact histogram (multinomial), an autoregressive GRU, and a causal-masked MLP. If the
signs held only for the histogram, the effect would be an artefact of the exact operator;
seeing them in every trained architecture is the generality claim.
Three panels: (A) forward-KL trajectories per architecture, dry (solid) vs grounded (dashed);
(B) stationary forward-KL, dry vs grounded, grouped by architecture (all fall with grounding);
(C) tail-item survival, dry vs grounded, grouped by architecture (all rise). Reads only the
committed bundle.
Usage: python figures/plot_architectures.py [results/architectures]
"""
from __future__ import annotations
import sys
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
sys.path.insert(0, str(Path(__file__).parent))
from _figlib import load_bundle, mean_ci, savefig, letter_axes # noqa: E402
_ARCH_ORDER = ["histogram", "rnn", "mlp"]
_ARCH_LABEL = {"histogram": "histogram\n(exact)", "rnn": "GRU\n(autoregressive)",
"mlp": "MLP\n(causal-masked)"}
def main(results_dir: str = "results/architectures") -> None:
df, cfg = load_bundle(results_dir)
kinds = [k for k in _ARCH_ORDER if k in set(df["kind"].unique())]
g_dry, g_wet = min(df["g"].unique()), max(df["g"].unique())
last = int(cfg["generations"] * 0.6)
stat = df[df["generation"] >= last]
fig, axes = plt.subplots(1, 3, figsize=(16, 4.6))
arch_colors = dict(zip(kinds, plt.cm.tab10(np.arange(len(kinds)))))
# Panel A: forward-KL trajectories per architecture, dry (solid) vs grounded (dashed).
ax = axes[0]
for k in kinds:
for g, ls, alpha in [(g_dry, "-", 1.0), (g_wet, "--", 0.7)]:
s = df[(df["kind"] == k) & (df["g"] == g)].groupby("generation")["forward_kl"].mean()
ax.plot(s.index, s.values, ls, color=arch_colors[k], alpha=alpha, lw=1.8,
label=f"{k} (g={g:g})")
ax.set(xlabel="generation", ylabel=r"forward-KL $D(p^*\Vert\hat p)$",
title="No real data (solid) collapses;\ngrounded (dashed) holds in every architecture")
ax.legend(frameon=False, fontsize=7, ncol=1)
# Panels B & C: grouped bars, dry vs grounded per architecture.
def grouped_bar(ax, metric, title, ylabel):
x = np.arange(len(kinds))
w = 0.36
for off, g, lab, col in [(-w / 2, g_dry, f"no real data (g={g_dry:g})", "#d62728"),
(w / 2, g_wet, f"grounded (g={g_wet:g})", "#2ca02c")]:
means, errs = [], []
for k in kinds:
sub = stat[(stat["kind"] == k) & (stat["g"] == g)]
_, m, ci = mean_ci(sub.assign(_x=0), "_x", metric)
means.append(m[0]); errs.append(ci[0])
ax.bar(x + off, means, w, yerr=errs, capsize=3, label=lab, color=col, alpha=0.85)
ax.set_xticks(x)
ax.set_xticklabels([_ARCH_LABEL[k] for k in kinds], fontsize=8)
ax.set(ylabel=ylabel, title=title)
ax.legend(frameon=False, fontsize=8)
grouped_bar(axes[1], "forward_kl", "Stationary forward-KL falls with grounding",
r"stationary forward-KL")
grouped_bar(axes[2], "tail_frac_alive", "Tail-item survival rises with grounding",
"tail items alive")
fig.tight_layout()
letter_axes(fig)
savefig(fig, results_dir, "architectures")
if __name__ == "__main__":
main(*sys.argv[1:])