# MIT License, Bryan Cheng, 2026. Check (D-037): the raw allele with V = I has unit Frobenius norm and its inner products # with centred adapter updates reproduce the raw genotype g = sqrt(lam) u. import json, os, sys import numpy as np import torch sys.path.insert(0, ".") from src.lorapop.adapters import load_module_factors from src.lorapop.genotype import allele plan = json.load(open("results/interventions/plan_qwen05_raw.json")) G = plan["geno_dir"] loci = torch.load(os.path.join(G, plan["loci_file"])) names = json.load(open(os.path.join(G, "adapters.json")))["names"] d = plan["directions"][0] key, l = d["module"], d["l"] f = [load_module_factors(os.path.join(plan["adapters_dir"], n), {key}).get(key) for n in names] q = next(v[0].shape[1] for v in f if v is not None) D = allele(f, loci[key]["U"][:, l], loci[key]["lam"][l], torch.eye(q), device="cpu").double() # streaming: = - mean_i (no dense list kept) raw = np.array([0.0 if v is None else float(((v[1].double() @ v[0].double()) * D).sum()) for v in f]) proj = raw - raw.mean() g = loci[key]["g"][:, l].double().numpy() print(d["id"], "module", key, "q", q, "||D||_F", float(D.norm()), "corr(proj, g)", np.corrcoef(proj, g)[0, 1], "max|proj-g|/sd", np.abs(proj - g).max() / g.std(), "sd g", g.std())