MachineSex/figures/plot_E4.py
Giorgio Gilestro 1721d047fa Layer 1 complete: E3-E6 + E2 analysis add-ons
Finishes the Layer 1 analytical core. All six experiments run with honest,
publication-quality figures; 71 tests green.

- E3 region-matched grounding: `grounding.exercised` knob + per-region tail
  survival. Matched holds the exercised region's tail (0.49) where uniform
  spreads thin and lets it collapse (0.07).
- E4 multi-teacher recombination: `run_coverage` runner. Union coverage matches
  U(K_T,rho,q) exactly. Finding: mean-mixture distillation shows NO surviving
  benefit (a conservation law — 1/K_T dilution cancels the union gain); a
  union-preserving max-merge (M2N2-style) does. E4 reports both operators.
- E5 QD vs greedy: greedy drives fixation (H~0.01); QD holds H at 0.48-0.88,
  rising with the novelty exponent.
- E6 re-mint gate: `arm` multi-override sweep. Re-minting a collapsed lineage
  locks in divergence of KL-to-original; gating on diversity prevents it.
- E2 analysis add-ons (from the companion work order, numbers verified): new
  analysis.py (reduce_to_stationary, critical_grounding with bootstrap CI ->
  g*=0.048, 95% CI [0.047,0.050]); tail_band_metrics + per-band logging; the
  E2 figure rebuilt as a 2x2 (defined g*+CI, g=0 flagged as a finite-time
  artifact, tail item-vs-mass, per-rarity-band panel). Uses truth-mass-weighted
  tail coverage rather than the raw (martingale) tail_mass.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-04 18:54:42 +02:00

79 lines
3.3 KiB
Python

"""E4 figure: multi-teacher recombination — supply vs realisation.
Three panels tell the honest story: (A) union coverage rises with K_T and decorrelation,
matching the exact closed form (recombination *supplies* the tail); (B) that supply is
realised in the pupil only under a union-preserving merge — mean-mixture distillation
dilutes it away (flat in K_T) while max-merge keeps it; (C) the union-surviving gap.
Usage: python figures/plot_E4.py [results/E4]
"""
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, savefig # noqa: E402
def U_closed(K_T, rho, q):
return rho * q + (1 - rho) * (1 - (1 - q) ** K_T)
def main(results_dir: str = "results/E4") -> None:
df, cfg = load_bundle(results_dir)
q = cfg["coverage"]["q"]
K_Ts = sorted(df["K_T"].unique())
rhos = sorted(df["rho"].unique())
g0 = df[df["g"] == 0.0]
colors = plt.cm.viridis(np.linspace(0, 0.85, len(K_Ts)))
fig, axes = plt.subplots(1, 3, figsize=(15, 4.3))
# Panel A: union coverage vs rho per K_T, with closed-form overlay
ax = axes[0]
for K, c in zip(K_Ts, colors):
sub = g0[g0["K_T"] == K].groupby("rho")["union_coverage"].mean()
ax.plot(sub.index, sub.values, "o", color=c, label=f"K_T={K}")
ax.plot(rhos, [U_closed(K, r, q) for r in rhos], "-", color=c, lw=1)
ax.set(xlabel=r"teacher correlation $\rho$", ylabel="union tail coverage",
title=r"Supply: union matches $U(K_T,\rho,q)$")
ax.legend(frameon=False, fontsize=8)
# Panel B: surviving coverage vs rho per K_T — mean (dashed) vs max (solid)
ax = axes[1]
for K, c in zip(K_Ts, colors):
sub = g0[g0["K_T"] == K].groupby("rho")
ax.plot(sub["surviving_max"].mean().index, sub["surviving_max"].mean().values,
"-o", color=c, label=f"K_T={K}", ms=4)
ax.plot(sub["surviving_mean"].mean().index, sub["surviving_mean"].mean().values,
"--", color=c, lw=1, alpha=0.7)
ax.set(xlabel=r"teacher correlation $\rho$", ylabel="surviving tail coverage",
title="Realised: max-merge (solid) rises;\nmean-distill (dashed) stays flat")
ax.legend(frameon=False, fontsize=8)
# Panel C: surviving vs K_T at rho=0, both operators — the recombination benefit
ax = axes[2]
r0 = g0[g0["rho"] == 0.0]
mx = r0.groupby("K_T")["surviving_max"].agg(["mean", "sem"])
mn = r0.groupby("K_T")["surviving_mean"].agg(["mean", "sem"])
ax.errorbar(mx.index, mx["mean"], yerr=1.96 * mx["sem"], fmt="-o",
color="#1f77b4", capsize=3, label="max-merge (M2N2-style)")
ax.errorbar(mn.index, mn["mean"], yerr=1.96 * mn["sem"], fmt="--s",
color="#d62728", capsize=3, label="mean-mixture distillation")
ax.set(xlabel="number of teachers $K_T$", ylabel="surviving tail coverage",
title=r"Benefit needs a union-preserving merge ($\rho=0$)",
xticks=K_Ts)
ax.legend(frameon=False, fontsize=9)
fig.suptitle("E4 — recombination supplies the tail; only a union-preserving merge "
"realises it in the pupil", y=1.03)
fig.tight_layout()
savefig(fig, results_dir, "E4")
if __name__ == "__main__":
main(*sys.argv[1:])