Live answer table v3: ZL rows (position, line) with empirical shrinkage per level in K header rows
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
# Live-Antwort-Tafel v3: Z-Zeilen wie v2 (gepoolt) plus ZL-Zeilen (Position, Linie), Format mit der Hauptsession abgestimmt (03.12.2026):
|
||||
# ZL <bb> <typ 0-3> <strasse 2|3> <bis> <pos o|i> <linie lead|cbet|barrel|-> <k Folds> <n>
|
||||
# K <stufe> <kappa> <zellen> <streuung> # Schrumpfung je Stufe, empirisch (Momente des Beta-Binomials um die Elternzelle), keine gesetzte Zahl im Leser
|
||||
# stufe pos : ZL (pos, -) um die Z-Zelle (typ, strasse, bis)
|
||||
# stufe linie : ZL (pos, linie) um ZL (pos, -)
|
||||
# Alte Leser (antwort_eichung.rs) ueberspringen unbekannte Zeilen (`_ => {}`) und lesen nur Z.
|
||||
# python3 antworttafel_v3.py <zellen-fein.json> <v2.txt> <aus.txt>
|
||||
import sys; sys.dont_write_bytecode = True
|
||||
import json, os
|
||||
from collections import defaultdict
|
||||
|
||||
POS = {"oop": "o", "ip": "i"}
|
||||
|
||||
|
||||
def kappa(gruppen):
|
||||
"""gruppen: Liste von (p_eltern, [(k, n), ...]). Momentenschaetzer der Intraklassen-Korrelation rho (Beta-Binomial), kappa = (1 - rho) / rho.
|
||||
Gibt (kappa, Zahl der Kinder, rho) zurueck; ohne Ueberstreuung kappa = inf (Kinder gleich Eltern)."""
|
||||
s = m = nm1 = 0.0
|
||||
for p, kinder in gruppen:
|
||||
if not 0 < p < 1:
|
||||
continue
|
||||
for k, n in kinder:
|
||||
if n < 2:
|
||||
continue
|
||||
s += (k - n * p) ** 2 / (n * p * (1 - p)); m += 1; nm1 += n - 1
|
||||
if m == 0 or nm1 == 0:
|
||||
return float("inf"), int(m), 0.0
|
||||
rho = (s - m) / nm1
|
||||
if rho <= 0:
|
||||
return float("inf"), int(m), rho
|
||||
return (1 - rho) / rho, int(m), rho
|
||||
|
||||
|
||||
def baue(fein_pfad, v2_pfad, aus):
|
||||
if os.path.exists(aus):
|
||||
sys.exit("gibt es schon (nie ueberschreiben): %s" % aus)
|
||||
fein = json.load(open(fein_pfad))
|
||||
v2 = [z.rstrip("\n") for z in open(v2_pfad)]
|
||||
zrate = {}
|
||||
for z in v2:
|
||||
w = z.split()
|
||||
if w[:1] == ["Z"]:
|
||||
zrate[(int(w[2]), int(w[3]), float(w[4]))] = int(w[5]) / int(w[6])
|
||||
pos_summe = defaultdict(lambda: [0, 0]); lin = {}
|
||||
for c in fein:
|
||||
sch = (c["typ"], c["strasse"], float(c["bis"]))
|
||||
p = POS[c["position"]]
|
||||
pos_summe[sch + (p,)][0] += c["fold"]; pos_summe[sch + (p,)][1] += c["n"]
|
||||
lin[sch + (p, c["linie"])] = (c["fold"], c["n"])
|
||||
g_pos = defaultdict(list)
|
||||
for key, (k, n) in pos_summe.items():
|
||||
if key[:3] in zrate:
|
||||
g_pos[zrate[key[:3]]].append((k, n))
|
||||
g_lin = defaultdict(list)
|
||||
for key, (k, n) in lin.items():
|
||||
kp, np_ = pos_summe[key[:4]]
|
||||
if np_ > 0:
|
||||
g_lin[kp / np_].append((k, n))
|
||||
k_pos = kappa(list(g_pos.items())); k_lin = kappa(list(g_lin.items()))
|
||||
zeilen = [v2[0].replace("Eichung v2", "Eichung v3") if v2 and v2[0].startswith("#") else "# Live-Antwort-Eichung v3",
|
||||
"# ZL <bb> <typ> <strasse> <bis> <pos o|i> <linie> <k> <n>; K <stufe> <kappa> <zellen> <rho>: Schrumpfung je Stufe empirisch (Beta-Binomial-Momente)",
|
||||
"K pos %s %d %.5f" % ("inf" if k_pos[0] == float("inf") else "%.1f" % k_pos[0], k_pos[1], k_pos[2]),
|
||||
"K linie %s %d %.5f" % ("inf" if k_lin[0] == float("inf") else "%.1f" % k_lin[0], k_lin[1], k_lin[2])]
|
||||
zeilen += [z for z in v2[1:] if z.strip() and not z.startswith("#")]
|
||||
for key in sorted(pos_summe):
|
||||
k, n = pos_summe[key]
|
||||
zeilen.append("ZL 0.02 %d %d %g %s - %d %d" % (key[0], key[1], key[2], key[3], k, n))
|
||||
for key in sorted(lin):
|
||||
k, n = lin[key]
|
||||
zeilen.append("ZL 0.02 %d %d %g %s %s %d %d" % (key[0], key[1], key[2], key[3], key[4], k, n))
|
||||
with open(aus + ".tmp", "w", encoding="utf-8") as f:
|
||||
f.write("\n".join(zeilen) + "\n")
|
||||
os.replace(aus + ".tmp", aus)
|
||||
print("K pos", k_pos, "K linie", k_lin, "ZL", sum(1 for z in zeilen if z.startswith("ZL")))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
baue(*sys.argv[1:4])
|
||||
Reference in New Issue
Block a user