"""CO1b · 20 · AUC emparejado (pares), bootstrap por expediente y por positivo, dejar-uno-fuera, veredicto (§5-6).""" import json, random import pyarrow.parquet as pq G = pq.read_table('/w/interno/10_grupos_b.parquet').to_pylist() SEED, NB = 20261022, 2000 def tramo(k, kmin): for lim, lab in ((5, f'{kmin}-5'), (10, '6-10'), (20, '11-20'), (50, '21-50')): if k <= lim: return lab return '>50' def evaluables(var, kmin): return [r for r in G if r['var'] == var and r['en_cg'] and r['k'] >= kmin and r['clase'] in ('pos', 'ctrl')] def unidades(rows, key, kmin): ctrl = {} for r in rows: if r['clase'] == 'ctrl': ctrl.setdefault((r['red'], tramo(r['k'], kmin)), []).append(r[key]) out, sin = [], [] for r in rows: if r['clase'] != 'pos': continue cs = ctrl.get((r['red'], tramo(r['k'], kmin)), []) if not cs: sin.append((r['red'], r['k'])); continue f = sum(1.0 if c < r[key] else (0.5 if c == r[key] else 0.0) for c in cs) / len(cs) out.append(dict(exp=r['exp'], cpv2=r['cpv2'], red=r['red'], grupo=r['grupo'], k=r['k'], tramo=tramo(r['k'], kmin), f=f, n_ctrl=len(cs), v=r[key])) return out, sin def auc(us): return sum(u['f'] for u in us) / len(us) if us else None def q(a): a = sorted(a); return (a[int(0.025 * len(a))], a[int(0.975 * len(a)) - 1]) def boot(us): if len(us) < 2: return {} rng = random.Random(SEED); exps = sorted({u['exp'] for u in us}); be = {e: [u for u in us if u['exp'] == e] for e in exps} a1, a2 = [], [] for _ in range(NB): a1.append(auc([u for e in (rng.choice(exps) for _ in exps) for u in be[e]])) a2.append(auc([rng.choice(us) for _ in us])) return dict(ic_expediente=q(a1), ic_positivo=q(a2)) def bloque(var, kmin, key, detalle=False): rows = evaluables(var, kmin); us, sin = unidades(rows, key, kmin) R = dict(auc=auc(us), n_pos=len(us), n_pos_sin_control=len(sin), n_pos_eval=sum(r['clase'] == 'pos' for r in rows), n_ctrl_eval=sum(r['clase'] == 'ctrl' for r in rows), n_exp=len({u['exp'] for u in us}), **boot(us)) if detalle: R['sin_control'] = sin R['por_expediente'] = {e: dict(auc=auc([u for u in us if u['exp'] == e]), n=sum(u['exp'] == e for u in us)) for e in sorted({u['exp'] for u in us})} R['por_red'] = {d: dict(auc=auc([u for u in us if u['red'] == d]), n=sum(u['red'] == d for u in us)) for d in sorted({u['red'] for u in us})} R['por_tramo'] = {t: dict(auc=auc([u for u in us if u['tramo'] == t]), n=sum(u['tramo'] == t for u in us)) for t in sorted({u['tramo'] for u in us})} R['dejar_mercado_fuera'] = {m: dict(auc=auc([u for u in us if u['cpv2'] != m]), n=sum(u['cpv2'] != m for u in us)) for m in sorted({u['cpv2'] for u in us})} R['dejar_expediente_fuera'] = {e: dict(auc=auc([u for u in us if u['exp'] != e]), n=sum(u['exp'] != e for u in us)) for e in sorted({u['exp'] for u in us})} R['positivos'] = [(u['red'], u['grupo'], u['k'], u['tramo'], round(u['f'], 4), u['n_ctrl']) for u in us] a = R['auc']; li, ls = R['ic_expediente'] if 'ic_expediente' in R else (None, None) lomo = min(v['auc'] for v in R['dejar_mercado_fuera'].values()); loeo = min(v['auc'] for v in R['dejar_expediente_fuera'].values()) if R['n_pos'] < 10: v = 'NO CONCLUYENTE (sin potencia)' elif li > 0.60 and lomo >= 0.55 and loeo >= 0.55: v = 'PASA' elif a >= 0.60: v = 'COTA' elif ls < 0.60: v = 'NO PASA' else: v = 'NO CONCLUYENTE' R['veredicto'] = dict(c_i=li > 0.60, c_ii=lomo >= 0.55, c_iii=loeo >= 0.55, lomo_min=lomo, loeo_min=loeo, li=li, ls=ls, veredicto=v) return R def regla80(var, kmin): """Regla del artículo: CG y EG ambas > percentil 80 de los controles de su red y tramo.""" rows = evaluables(var, kmin); ctrl = {} for r in rows: if r['clase'] == 'ctrl': ctrl.setdefault((r['red'], tramo(r['k'], kmin)), []).append(r) def p80(v): v = sorted(v); return v[int(0.8 * (len(v) - 1))] res = {'pos': [0, 0], 'ctrl': [0, 0]} for r in rows: cs = ctrl.get((r['red'], tramo(r['k'], kmin)), []) if not cs: continue mark = r['CG'] > p80([c['CG'] for c in cs]) and r['EG'] > p80([c['EG'] for c in cs]) res[r['clase']][0] += mark; res[r['clase']][1] += 1 return {c: dict(marcados=m, n=n, tasa=m / n if n else None) for c, (m, n) in res.items()} R = {} for var in ('A', 'B'): for kmin in (3, 5): for key in ('SWK', 'CG', 'EG', 'S1', 'C1', 'E1'): R[f'{var}_k{kmin}_{key}'] = bloque(var, kmin, key, detalle=(key in ('SWK', 'S1'))) R[f'{var}_k{kmin}_regla80'] = regla80(var, kmin) # descriptivos (sin nombres): tamaños y medidas medias por clase, A k>=3 import statistics as st d = {} for cl in ('pos', 'ctrl'): rr = [r for r in evaluables('A', 3) if r['clase'] == cl] d[cl] = dict(n=len(rr), k_mediana=st.median([r['k'] for r in rr]), **{f'{m}_mediana': st.median([r[m] for r in rr]) for m in ('CG', 'EG', 'SWK', 'C1', 'E1', 'S1')}) d[cl]['por_tramo'] = {t: sum(1 for r in rr if tramo(r['k'], 3) == t) for t in ('3-5', '6-10', '11-20', '21-50', '>50')} R['descriptivos_A_k3'] = d # CO1: positivos que quedan fuera de evaluables y por qué allpos = [r for r in G if r['var'] == 'A' and r['clase'] == 'pos'] R['positivos_CO1_A'] = dict(total=len(allpos), fuera_CG=sum(not r['en_cg'] for r in allpos), k_menor_3=sum(r['k'] < 3 for r in allpos)) allc = [r for r in G if r['var'] == 'A' and r['clase'] == 'ctrl'] R['controles_CO1_A'] = dict(total=len(allc), fuera_CG=sum(not r['en_cg'] for r in allc), k2=sum(r['k'] == 2 for r in allc), evaluables_k3=sum(r['en_cg'] and r['k'] >= 3 for r in allc), evaluables_k5=sum(r['en_cg'] and r['k'] >= 5 for r in allc)) json.dump(R, open('/w/interno/20_res.json', 'w'), indent=1, ensure_ascii=False) for k, v in R.items(): if isinstance(v, dict) and 'auc' in v: print(k, round(v['auc'], 3) if v['auc'] is not None else None, v['n_pos'], v.get('ic_expediente'), v.get('veredicto', {}).get('veredicto', '')) else: print(k, v if 'regla' in k or 'CO1' in k else '') print(json.dumps(R['A_k3_SWK'], indent=1, ensure_ascii=False)[:6000])