"""Study: former workers waiting on pensions in frozen plans, plan year 2024 (PRD 5.145). Writes the frozen files. Reads the Labor Department's Form 5500 research files ("Latest" versions dated 2026-10-25) and nothing else: F_SCH_SB, F_5500 and F_5500_SF for form years 2023 and 2024. No Attovar database is needed. The rule (set 2026-11-21 for the weekly study draft, before any count; the draft's separate check matched it exactly): - Every Schedule SB row, joined on the acknowledgement ID to its main filing (Form 5500 or 5500-SF) in the same form-year files. A Schedule SB is filed by single-employer and multiple-employer defined benefit plans. - One filing per plan record (sponsor EIN on Schedule SB and plan number): the highest acknowledgement ID. - Final filings are dropped (FINAL_FILING_IND / SF_FINAL_FILING_IND = 1). - Frozen: the main filing's pension feature codes include 1I ("frozen plan"), the employer's own code. - Former workers waiting: Schedule SB line 3b, terminated vested participants (SB_TERM_PARTCP_CNT). Value owed to them: the line 3b funding target (SB_TERM_FNDNG_TGT_AMT), a present value. Active: line 3c (SB_ACT_PARTCP_CNT). - State: the sponsor's mailing state on the main filing (5500: SPONS_DFE_MAIL_US_STATE; SF: SF_SPONS_US_STATE). - Plan year 2024 is the 2024 form-year files; 2023 runs the same way for the trend. - plans.csv names only frozen plans with 100 or more total participants (Schedule SB line 3d count), so no small plan whose sponsor is a person is named; every count in summary.json covers all frozen plans. Usage: python -I tools/frozen_study.py ZIP_DIR OUT_DIR """ from __future__ import annotations import csv import glob import hashlib import io import json import os import shutil import statistics import sys import zipfile from collections import defaultdict PY = 2024 YEARS = (2023, 2024) NAMED = 100 # plans.csv: frozen plans with at least this many participants BIG = 1000 # "big" plans: at least this many former workers waiting TOP = 25 # lookup rows: the largest frozen plans by former workers waiting DATA_RELEASE = "dol-latest-2026-10-25" # the DOL "Latest" research files dated 2026-10-25 (hashes in summary.json) SB = {"former": "SB_TERM_PARTCP_CNT", "owed": "SB_TERM_FNDNG_TGT_AMT", "active": "SB_ACT_PARTCP_CNT", "participants": "SB_TOT_PARTCP_CNT"} def csv_rows(path: str): zf = zipfile.ZipFile(path) name = next(n for n in zf.namelist() if n.lower().endswith(".csv")) rd = csv.reader(io.TextIOWrapper(zf.open(name), encoding="latin-1", newline="")) head = [h.strip().upper() for h in next(rd)] for row in rd: yield dict(zip(head, row)) def num(x: str | None) -> float: try: return float(x) if x and x.strip() else 0.0 except ValueError: return 0.0 def sha256(path: str) -> str: h = hashlib.sha256() with open(path, "rb") as fh: for block in iter(lambda: fh.read(1 << 20), b""): h.update(block) return h.hexdigest() def mains(zdir: str, y: int) -> dict: """Acknowledgement ID -> (frozen, final, sponsor, plan name, state, form) for every main filing of form year Y.""" out = {} for r in csv_rows(os.path.join(zdir, f"F_5500_{y}_Latest.zip")): codes = (r.get("TYPE_PENSION_BNFT_CODE") or "").upper() out[r["ACK_ID"]] = ("1I" in codes, (r.get("FINAL_FILING_IND") or "").strip() == "1", (r.get("SPONSOR_DFE_NAME") or "").strip(), (r.get("PLAN_NAME") or "").strip(), (r.get("SPONS_DFE_MAIL_US_STATE") or "").strip(), "5500") for r in csv_rows(os.path.join(zdir, f"F_5500_SF_{y}_Latest.zip")): codes = (r.get("SF_TYPE_PENSION_BNFT_CODE") or "").upper() out[r["ACK_ID"]] = ("1I" in codes, (r.get("SF_FINAL_FILING_IND") or "").strip() == "1", (r.get("SF_SPONSOR_NAME") or "").strip(), (r.get("SF_PLAN_NAME") or "").strip(), (r.get("SF_SPONS_US_STATE") or "").strip(), "5500-SF") return out def load(zdir: str, y: int) -> list: """Plan records of form year Y that pass the rule up to the frozen test (non-final, joined), each with its facts.""" m = mains(zdir, y) best: dict = {} for r in csv_rows(os.path.join(zdir, f"F_SCH_SB_{y}_Latest.zip")): if r["ACK_ID"] not in m: continue k = (r["SB_EIN"].strip(), r["SB_PN"].strip()) if k not in best or r["ACK_ID"] > best[k]["ack"]: best[k] = {"ack": r["ACK_ID"], "ein": k[0], "pn": k[1], **{n: num(r.get(c)) for n, c in SB.items()}} out = [] for rec in best.values(): frozen, final, sponsor, plan, st, form = m[rec["ack"]] if final: continue rec.update(frozen=frozen, sponsor=sponsor, plan=plan, state=st, form=form) out.append(rec) return out def people(v: list, key: str = "former") -> int: return int(sum(r[key] for r in v)) def usd(v: list) -> float: return round(sum(r["owed"] for r in v), 2) def year_summary(v: list) -> dict: fz = [r for r in v if r["frozen"]] return {"plans": len(fz), "former": people(fz), "owed_usd": usd(fz), "all_plans": len(v), "all_former": people(v)} def summarize(by_year: dict, inputs: dict) -> dict: v = by_year[PY] fz = [r for r in v if r["frozen"]] more = [r for r in fz if r["former"] > r["active"]] big = [r for r in fz if r["former"] >= BIG] st_all: dict = defaultdict(int) for r in v: st_all[r["state"]] += int(r["former"]) states: dict = defaultdict(lambda: {"plans": 0, "former": 0, "owed_usd": 0.0}) for r in fz: s = states[r["state"]] s["plans"] += 1 s["former"] += int(r["former"]) s["owed_usd"] += r["owed"] by_state = sorted(({"state": k, "plans": s["plans"], "former": s["former"], "owed_usd": round(s["owed_usd"], 2), "all_former": st_all[k], "frozen_share": round(s["former"] / st_all[k], 4) if st_all[k] else None} for k, s in states.items()), key=lambda x: (-x["former"], x["state"])) trend = [{"plan_year": y, **year_summary(by_year[y])} for y in YEARS] t0, t1 = trend[0], trend[1] shares = [x for x in by_state if x["former"] >= 1000 and x["frozen_share"] is not None] return { "plan_year": PY, "data_release": DATA_RELEASE, "inputs": inputs, "rule": {"join": "Schedule SB to Form 5500 or 5500-SF on ACK_ID, same form year", "filing": "highest ACK_ID per Schedule SB EIN and plan number", "final": "dropped", "frozen": "pension feature code 1I", "former": "Schedule SB line 3b count", "owed": "Schedule SB line 3b funding target", "active": "Schedule SB line 3c count", "named_min_participants": NAMED}, "plans": len(fz), "former": people(fz), "owed_usd": usd(fz), "owed_per_former_usd": round(usd(fz) / people(fz)), "all_plans": len(v), "all_former": people(v), "frozen_share": round(people(fz) / people(v), 4), "former_outnumber_active": {"plans": len(more), "former": people(more)}, "no_active": sum(1 for r in fz if r["active"] == 0), "no_active_with_former": sum(1 for r in fz if r["active"] == 0 and r["former"] > 0), "big": {"min_former": BIG, "plans": len(big), "former": people(big), "share": round(people(big) / people(fz), 4)}, "median_former": statistics.median(r["former"] for r in fz), "named_plans": sum(1 for r in fz if r["participants"] >= NAMED), "forms": {f: sum(1 for r in fz if r["form"] == f) for f in ("5500", "5500-SF")}, "trend": trend, "change": {"former": t1["former"] - t0["former"], "pct": round((t1["former"] - t0["former"]) / t0["former"], 4), "plans": t1["plans"] - t0["plans"]}, "top_share_states": sorted(shares, key=lambda x: (-x["frozen_share"], x["state"]))[:5], "by_state": by_state, } PLAN_COLS = ("plan_id", "ein", "pn", "ack_id", "form", "sponsor", "plan_name", "state", "participants", "former_workers_waiting", "active_workers", "owed_to_former_usd", "former_outnumber_active") def _row(r: dict) -> dict: return {"plan_id": f"{r['ein']}-{r['pn']}", "ein": r["ein"], "pn": r["pn"], "ack_id": r["ack"], "form": r["form"], "sponsor": r["sponsor"], "plan_name": r["plan"], "state": r["state"], "participants": int(r["participants"]), "former_workers_waiting": int(r["former"]), "active_workers": int(r["active"]), "owed_to_former_usd": round(r["owed"], 2), "former_outnumber_active": int(r["former"] > r["active"])} def write(out: str, fz: list, summary: dict) -> None: os.makedirs(out, exist_ok=True) named = sorted((r for r in fz if r["participants"] >= NAMED), key=lambda r: (-r["former"], r["ein"], r["pn"])) with open(os.path.join(out, "plans.csv"), "w", newline="") as fh: w = csv.DictWriter(fh, fieldnames=PLAN_COLS, lineterminator="\n") w.writeheader() w.writerows(_row(r) for r in named) with open(os.path.join(out, "lookup.json"), "w") as fh: json.dump({"rows": [_row(r) for r in named[:TOP]]}, fh, indent=1) fh.write("\n") with open(os.path.join(out, "summary.json"), "w") as fh: json.dump(summary, fh, indent=1) fh.write("\n") shutil.copyfile(os.path.abspath(__file__), os.path.join(out, "frozen_study.py.txt")) def main(zdir: str, out: str) -> dict: inputs = {os.path.basename(z): sha256(z) for y in YEARS for z in sorted(glob.glob(os.path.join(zdir, f"F_*_{y}_Latest.zip"))) if os.path.basename(z).startswith(("F_SCH_SB_", "F_5500_"))} by_year = {y: load(zdir, y) for y in YEARS} s = summarize(by_year, inputs) write(out, [r for r in by_year[PY] if r["frozen"]], s) return s if __name__ == "__main__": if len(sys.argv) != 3: sys.exit(__doc__) s = main(sys.argv[1], sys.argv[2]) print(json.dumps({k: s[k] for k in ("plans", "former", "owed_usd", "all_former", "frozen_share", "former_outnumber_active", "no_active", "big", "median_former", "named_plans", "trend", "change", "top_share_states")}, indent=1)) print(json.dumps(s["by_state"][:8], indent=1))