"""NE1 · funciones comunes de la implementación PRINCIPAL (la segunda, 30_impl2, no importa esto).""" import numpy as np SEM, B = 20261022, 1000 NB = 30 XS = np.array([0.70 + (b + 0.5) * 0.01 for b in range(NB)]) def fit(M, nfit=25, grado=1, excl=None): """M: (R, 30) recuentos. Devuelve O (R,), E (R,). Ajuste en b < nfit (sin los b de excl), ventana b = 25..29.""" M = np.atleast_2d(np.asarray(M, float)) idx = [b for b in range(nfit) if not (excl and b in excl)] X = np.vander(XS[idx], grado + 1, increasing=True) # (k, g+1) P = np.linalg.pinv(X) # (g+1, k) coef = M[:, idx] @ P.T # (R, g+1) Xw = np.vander(XS[25:], grado + 1, increasing=True) # (5, g+1) E = np.clip(coef @ Xw.T, 0, None).sum(1) E = np.where(E < 1e-9, 0.0, E) O = M[:, 25:].sum(1) return O, E def xratio(O, E): with np.errstate(divide='ignore', invalid='ignore'): return np.where(E > 0, O / np.where(E > 0, E, 1), np.inf) def pesos(estratos, sem=SEM, Bn=B): """estratos: array de etiquetas por órgano (orden fijo). Bootstrap de órganos estratificado: (Bn, n) recuentos.""" rng = np.random.default_rng(sem); est = np.asarray(estratos); Wt = np.zeros((Bn, len(est))) for s in sorted(set(est.tolist())): ix = np.where(est == s)[0]; Wt[:, ix] = rng.multinomial(len(ix), np.full(len(ix), 1 / len(ix)), size=Bn) return Wt def ic(v): v = np.asarray(v, float); v = v[~np.isnan(v)] if len(v) == 0: return [float('nan'), float('nan')] return [float(np.percentile(v, 2.5)), float(np.percentile(v, 97.5))]