SPB Git

spb/wp28_uqo

Public
2.1 KB · 43 lines python
1"""Finite-m prediction of the mean scaled gap in the exponential boundary study.2E[sqrt(N)(theta^(q) - theta_hat)] ~ -sqrt(N) E[e0^(2^q)] / theta0^(2^q - 1)  (q >= 1),  with e0 = pilot - theta_hat,3E e0 = b_m (exact finite-m bias of J_r), Var e0 ~ Var(R^J_k)/K  (R_N term negligible); Var(R^J_k) by exact formula (r = 1, 2) or4by a seeded simulation of one block (r >= 3). Gaussian moments are used for E e0^4 (q = 2)."""5import sys, pathlib, numpy as np6sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1] / "src"))7from pamle.jackknife import vandermonde_weights, DEFAULT_SPLITS8910def exact_bias(r, m, theta0=1.0):11    s = DEFAULT_SPLITS[r]; w = vandermonde_weights(s)12    return theta0 * sum(wj * sj / (m - sj) for wj, sj in zip(w, s))131415def var_RJ(r, m, theta0=1.0, reps=400000, seed=7):16    if (r == 1 and m <= 2) or (r == 2 and m <= 4) or (r >= 3 and m // DEFAULT_SPLITS[r][-1] <= 2):17        return float('nan')          # variance of the local remainder does not exist18    if r == 1:19        return theta0 ** 2 * ((3 * m + 2) / (m * (m - 1) * (m - 2)) - 1 / (m - 1) ** 2)20    if r == 2:21        varJ = theta0 ** 2 * m ** 2 * (m ** 2 - 6 * m + 17) / ((m - 4) * (m - 2) ** 2 * (m - 1) ** 2)22        covJU = theta0 ** 2 * (2 / (m - 1) - 1 / (m - 2))      # Cov(J2, U) with U = theta0 - theta0^2 Xbar23        return varJ + theta0 ** 2 / m - 2 * covJU24    s = DEFAULT_SPLITS[r]; w = vandermonde_weights(s); smax = s[-1]25    rng = np.random.default_rng(seed)26    parts = rng.gamma(shape=m // smax, scale=1.0 / theta0, size=(reps, smax))27    J = np.zeros(reps)28    for wj, sj in zip(w, s):29        sub = parts.reshape(reps, sj, smax // sj).sum(axis=2); J += wj * ((m / sj) / sub).mean(axis=1)30    U = theta0 - theta0 ** 2 * parts.sum(axis=1) / m31    return float(np.var(J - U, ddof=1))323334def predicted_mean_gap(r, q, m, K, N, theta0=1.0):35    b = exact_bias(r, m, theta0); v = var_RJ(r, m, theta0) / K36    if q == 0:37        return np.sqrt(N) * b38    if q == 1:39        return -np.sqrt(N) * (b ** 2 + v) / theta040    if q == 2:41        return -np.sqrt(N) * (b ** 4 + 6 * b ** 2 * v + 3 * v ** 2) / theta0 ** 342    raise ValueError(q)43
Files · 1ae646dc989aa524829d12cef2c3022d9e30261f
Menu

Theme