"""EJ2 · funciones de la implementación PRINCIPAL (20_exceso). La segunda (30_impl2) NO importa esto.""" import numpy as np, math NB = 120 # bins de 0,5 puntos en [0, 60 %) MASAS = [round(0.05 * j, 2) for j in range(1, 12)] # 5 %..55 % TOL = 0.00005 def bins(lo, hi): """índices de bin de [lo, hi) en puntos de %""" return list(range(int(round(lo * 2)), int(round(hi * 2)))) Z = set(bins(9.5, 13) + bins(19, 26) + bins(49, 56)) def config(c): if c == 20: return bins(19, 20), bins(20, 26), bins(12, 19) + bins(26, 34) if c == 10: return bins(9.5, 10), bins(10, 13), bins(5, 9.5) + bins(13, 18) if c == 50: return bins(49, 50), bins(50, 56), bins(40, 49) + bins(56, 60) E, H = bins(c - 1, c), bins(c, c + 1) F = [k for k in bins(c - 7, c + 7) if k not in E and k not in H and k not in Z] return E, H, F UMBRALES = [20, 10, 50, 15, 25, 30, 35, 40] def categorias(s): """categoría por licitación: 0..119 bin; 120+j masa puntual j; 140 s<0; 141 s>=0,6; 142 nulo""" s = np.asarray(s, dtype=float); cat = np.full(len(s), 142, dtype=np.int64) ok = ~np.isnan(s) cat[ok & (s < 0)] = 140; cat[ok & (s >= 0.6)] = 141 dentro = ok & (s >= 0) & (s < 0.6) cat[dentro] = np.floor(s[dentro] * 200 + 1e-9).astype(np.int64) for j, m in enumerate(MASAS): cat[ok & (np.abs(s - m) <= TOL)] = 120 + j return cat def recuento(cat): return np.bincount(cat, minlength=143) def ajuste(cnt, c, grado=2, F=None): """Poisson log-polinómico por IRLS en los bins F; devuelve la curva ajustada en los 120 bins (o None).""" E, H, F0 = config(c); F = F0 if F is None else F y = cnt[F].astype(float) if y.sum() < 50: return None mid = (np.arange(NB) * 0.5 + 0.25 - c) / 10.0 X = np.vander(mid, grado + 1, increasing=True) XF = X[F] b = np.linalg.lstsq(XF, np.log(y + 0.5), rcond=None)[0] for _ in range(100): eta = XF @ b; mu = np.exp(eta) zw = eta + (y - mu) / mu WX = XF * mu[:, None] bn = np.linalg.solve(XF.T @ WX, WX.T @ zw) if np.max(np.abs(bn - b)) < 1e-10: b = bn; break b = bn else: return None return np.exp(X @ b) def ajuste_bu1(cnt, c): """sensibilidad estilo BU1: recta por MCO en [c-6, c-1) (solo por debajo), extrapolada""" F = bins(c - 6, c - 1); y = cnt[F].astype(float) if y.sum() < 50: return None mid = np.arange(NB) * 0.5 + 0.25 A = np.vstack([np.ones(len(F)), mid[F]]).T b = np.linalg.lstsq(A, y, rcond=None)[0] return b[0] + b[1] * mid def medidas(cnt, c, curva): E, H, F = config(c) if curva is None: return None O, Eh = cnt[E].sum(), curva[E].sum() r = {'O_E': int(O), 'Ehat_E': float(Eh), 'X': float(O / Eh) if Eh > 0 else float('nan'), 'B': float(O - Eh)} if c in (10, 20, 50): OH, EH = cnt[H].sum(), curva[H].sum() r.update({'O_H': int(OH), 'Ehat_H': float(EH), 'R_H': float(OH / EH), 'D': float(EH - OH), 'balance_D_B': float((EH - OH) / (O - Eh)) if O - Eh > 0 else float('nan')}) return r def garwood(O, a=0.05): """IC exacto de Poisson para la media, por bisección sobre la CDF""" def cdf(k, lam): if k < 0: return 0.0 t = math.exp(-lam); s = t for i in range(1, k + 1): t *= lam / i; s += t return s def bis(f, lo, hi): for _ in range(200): m = (lo + hi) / 2 if f(m): hi = m else: lo = m return (lo + hi) / 2 lo = 0.0 if O == 0 else bis(lambda l: 1 - cdf(O - 1, l) >= a / 2, 0.0, O + 10 * math.sqrt(O) + 10) hi = bis(lambda l: cdf(O, l) <= a / 2, 0.0, O + 20 * math.sqrt(O) + 20) return lo, hi