File size: 7,040 Bytes
dd138ec | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | """Tetradic (four-way) results: pooled 4x4 H heatmap + descriptive stats + variance decomposition.
Over all COMPLETE tetradic runs (no checkpoint file). Persistent; re-run as more land.
Outputs:
- figures/fig_h_tetradic.png (heatmap, style matched to plot_matrices.py)
- _paper/analyses/tetradic_results.md (numbers to hand to DR)
"""
import sqlite3, json
from collections import defaultdict
from pathlib import Path
import numpy as np
import matplotlib; matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties, fontManager
HERE = Path(__file__).parent
FIGDIR = Path(".")
OUTMD = Path("out/tetradic_results.md")
SHORT = {"Qwen/Qwen3-32B":"Qwen","openai/gpt-oss-20b":"gpt-oss",
"mistralai/Magistral-Small-2509":"Mag","zai-org/GLM-4-32B-0414":"GLM-4"}
MODELS = ["GLM-4","Mag","Qwen","gpt-oss"]
try:
for ttf in (Path.home()/"fonts").rglob("*.ttf"): fontManager.addfont(str(ttf))
FAM = "Ubuntu Mono"
except Exception:
FAM = "monospace"
perH = defaultdict(list) # incoming H per target across runs
matruns = [] # list of 4x4 H matrices
lead = last = 0; rho = []; nrun = 0
agents = [] # for variance decomposition
for db in sorted(HERE.glob("tetra_s*_r*.db")):
base = db.stem
if (HERE/f"{base}_checkpoint.json").exists(): continue
mp = str(db).replace(".db",".metadata.json")
if not Path(mp).exists(): continue
fam = {int(k)+1: SHORT.get(v["model"]) for k,v in json.load(open(mp))["agent_model_map"].items()}
if any(v is None for v in fam.values()): continue
try:
c = sqlite3.connect(db)
posts = c.execute("SELECT post_id,user_id FROM post").fetchall()
coms = c.execute("SELECT post_id,user_id FROM comment").fetchall()
users = dict(c.execute("SELECT user_id,user_name FROM user").fetchall()); c.close()
except Exception: continue
owner = {p:u for p,u in posts}; F = sorted(set(fam.values()))
nf = {f: sum(1 for v in fam.values() if v==f) for f in F}; ntot = sum(nf.values())
edges = [(fam[cu], fam[owner[p]]) for p,cu in coms
if owner.get(p) and owner[p]!=cu and cu in fam and owner[p] in fam]
tot = len(edges)
if tot < 100: continue
nrun += 1
obs = defaultdict(int)
for x,y in edges: obs[(x,y)] += 1
# 4x4 matrix (rows=source/commenter, cols=target/author)
Mx = np.full((4,4), np.nan)
for i,X in enumerate(MODELS):
for j,Y in enumerate(MODELS):
exp = (nf[X]/ntot)*((nf[Y]-(1 if X==Y else 0))/(ntot-1))
if exp>0: Mx[i,j] = obs.get((X,Y),0)/tot/exp - 1
matruns.append(Mx)
# incoming H per target (mean over X!=Y)
for Y in MODELS:
perH[Y].append(np.nanmean([Mx[i,MODELS.index(Y)] for i,X in enumerate(MODELS) if X!=Y]))
# in-degree per agent + lead/last + rho
ind = defaultdict(int)
for p,cu in coms:
o = owner.get(p)
if o in fam and o!=cu: ind[o]+=1
fam_in = defaultdict(list)
for u,f in fam.items(): fam_in[f].append(ind.get(u,0))
mean_in = {f: np.mean(v) for f,v in fam_in.items()}
if max(mean_in, key=mean_in.get)=="gpt-oss": lead+=1
if min(mean_in, key=mean_in.get)=="Mag": last+=1
tot_in = sum(sum(v) for v in fam_in.values())
share = sum(fam_in["gpt-oss"])/tot_in if tot_in else 0
rho.append(share/(nf["gpt-oss"]/ntot))
for u,f in fam.items():
agents.append({"run":base,"model":f,"persona":users.get(u,f"u{u}"),"indeg":ind.get(u,0)})
# ---- pooled matrix + heatmap ----
Mpool = np.nanmean(np.stack(matruns), axis=0)
fig, ax = plt.subplots(figsize=(4.2,3.6))
vmax = np.nanmax(np.abs(Mpool))
im = ax.imshow(Mpool, cmap="RdBu_r", vmin=-vmax, vmax=vmax)
ax.set_xticks(range(4)); ax.set_yticks(range(4))
ax.set_xticklabels(MODELS, fontproperties=FontProperties(family=FAM,size=9), rotation=30, ha="right")
ax.set_yticklabels(MODELS, fontproperties=FontProperties(family=FAM,size=9))
ax.set_xlabel("post author (target)", fontproperties=FontProperties(family=FAM,size=9))
ax.set_ylabel("commenter (source)", fontproperties=FontProperties(family=FAM,size=9))
for i in range(4):
for j in range(4):
v = Mpool[i,j]
ax.text(j,i,f"{v:+.2f}",ha="center",va="center",
fontproperties=FontProperties(family=FAM,size=8),
color="white" if abs(v)>0.55*vmax else "black")
cb = fig.colorbar(im, fraction=0.046, pad=0.04); cb.ax.tick_params(labelsize=7)
ax.set_title(f"Four-way $H_{{X\\to Y}}$ (pooled, n={nrun} runs)",
fontproperties=FontProperties(family=FAM,size=10))
fig.tight_layout(); fig.savefig(FIGDIR/"fig_h_tetradic.png", dpi=300, bbox_inches="tight")
print("wrote", FIGDIR/"fig_h_tetradic.png")
# ---- variance decomposition (in-sample R^2, matches tab:ols_decomp convention) ----
y = np.array([a["indeg"] for a in agents], float)
def dummies(keys):
levels = sorted(set(keys)); idx = {l:i for i,l in enumerate(levels)}
D = np.zeros((len(keys), len(levels)))
for r,k in enumerate(keys): D[r, idx[k]] = 1
return D[:,1:] # drop one for intercept
def r2(*blocks):
blocks = [b for b in blocks if b is not None and b.shape[1]>0]
X = np.column_stack([np.ones(len(y))] + blocks) if blocks else np.ones((len(y),1))
b,*_ = np.linalg.lstsq(X, y, rcond=None); pred = X@b
return 1 - ((y-pred)**2).sum()/((y-y.mean())**2).sum()
P = dummies([a["persona"] for a in agents])
Mod = dummies([a["model"] for a in agents])
R = dummies([a["run"] for a in agents])
ossind = np.array([[1.0 if a["model"]=="gpt-oss" else 0.0] for a in agents])
vd = {"persona_only":r2(P), "model_only":r2(Mod), "oss_only":r2(ossind),
"run_only":r2(R), "persona+model":r2(P,Mod), "persona+run":r2(P,R), "full":r2(P,Mod,R)}
dmodel = vd["full"] - vd["persona+run"]
# ---- write md ----
o = [f"# Tetradic (four-way) results — {nrun} complete runs\n",
"## Incoming H per model (mean over commenter families, across runs)\n",
"| model | mean H | SD | min | max |", "|---|---:|---:|---:|---:|"]
for m in ["gpt-oss","Qwen","GLM-4","Mag"]:
v = np.array(perH[m]); o.append(f"| {m} | {v.mean():+.2f} | {v.std():.2f} | {v.min():+.2f} | {v.max():+.2f} |")
o += [f"\n- gpt-oss is the **most-commented family in {lead}/{nrun} runs** ({100*lead/nrun:.0f}%).",
f"- Magistral is the **least-commented in {last}/{nrun} runs** ({100*last/nrun:.0f}%).",
f"- gpt-oss over-representation rho = comment-share / fair-share: **mean {np.mean(rho):.2f}, median {np.median(rho):.2f}**.",
"\n## Variance decomposition (per-agent raw in-degree, in-sample R^2)\n",
"| block | R^2 |", "|---|---:|"]
for k in ["persona_only","model_only","oss_only","run_only","persona+model","persona+run","full"]:
o.append(f"| {k.replace('_',' ')} | {vd[k]:.3f} |")
o.append(f"| **Delta R^2 model** (full - persona+run) | **{dmodel:+.3f}** |")
o.append(f"\nn agents = {len(agents)}. Compare dyad Delta R^2_model +.044, triad +.071.")
OUTMD.write_text("\n".join(o)+"\n"); print("\n".join(o)); print("wrote", OUTMD)
|