Repository Public
1"""Reference values for Statistics/Resampling: bootstrap (scipy.stats.bootstrap fed with a known2index matrix), exact permutation tests (scipy.stats.permutation_test) and Monte Carlo output3summaries (numpy / scipy.stats)."""4import numpy as np5from scipy import stats67from gen_common import dump89out = {}10SEED = 202611211112# --- bootstrap -----------------------------------------------------------------------------13r = np.random.default_rng(7)14x = np.round(r.lognormal(1.0, 0.6, size=25), 4)15y = np.round(0.6 * x + r.normal(0, 1.0, size=25), 4)16B = 200017idx = np.random.default_rng(SEED).integers(0, len(x), (B, len(x))) # what scipy draws internally181920def manual(stat_on_idx, level):21 n = len(x)22 theta = stat_on_idx(np.arange(n))23 reps = np.array([stat_on_idx(i) for i in idx])24 alpha = (1 - level) / 225 pct = np.percentile(reps, [100 * alpha, 100 * (1 - alpha)])26 z0 = stats.norm.ppf(((reps < theta).sum() + (reps <= theta).sum()) / (2 * B))27 jack = np.array([stat_on_idx(np.delete(np.arange(n), i)) for i in range(n)])28 u = jack.mean() - jack29 a = (u ** 3).sum() / (6 * (u ** 2).sum() ** 1.5)30 za = stats.norm.ppf(alpha)31 a1 = stats.norm.cdf(z0 + (z0 + za) / (1 - a * (z0 + za)))32 a2 = stats.norm.cdf(z0 + (z0 - za) / (1 - a * (z0 - za)))33 bca = np.percentile(reps, [100 * a1, 100 * a2])34 return dict(estimate=theta, reps=reps, se=reps.std(ddof=1), bias=reps.mean() - theta, pct=pct, bca=bca, z0=z0, acc=a)353637cases = {}38single = {39 "mean": (lambda v, axis=-1: np.mean(v, axis=axis)),40 "sd": (lambda v, axis=-1: np.std(v, axis=axis, ddof=1)),41 "median": (lambda v, axis=-1: np.median(v, axis=axis)),42}43for name, f in single.items():44 for level in (0.95, 0.80):45 m = manual(lambda i: f(x[i]), level)46 for method in ("percentile", "BCa"):47 res = stats.bootstrap((x,), f, n_resamples=B, confidence_level=level, method=method, vectorized=True,48 rng=np.random.default_rng(SEED), batch=None)49 ref = m["pct"] if method == "percentile" else m["bca"]50 got = np.array([res.confidence_interval.low, res.confidence_interval.high])51 assert np.allclose(got, ref, rtol=1e-12), (name, level, method, got, ref)52 assert np.isclose(res.standard_error, m["se"], rtol=1e-12)53 cases[f"{name}_{level}"] = {"stat": name, "level": level, **{k: v for k, v in m.items() if k != "reps"},54 "reps_head": m["reps"][:10]}555657def corr(a, b, axis=-1):58 a = a - a.mean(axis=axis, keepdims=True)59 b = b - b.mean(axis=axis, keepdims=True)60 return (a * b).sum(axis=axis) / np.sqrt((a * a).sum(axis=axis) * (b * b).sum(axis=axis))616263m = manual(lambda i: corr(x[i], y[i]), 0.9)64for method in ("percentile", "BCa"):65 res = stats.bootstrap((x, y), corr, n_resamples=B, confidence_level=0.9, method=method, paired=True, vectorized=True,66 rng=np.random.default_rng(SEED), batch=None)67 ref = m["pct"] if method == "percentile" else m["bca"]68 assert np.allclose([res.confidence_interval.low, res.confidence_interval.high], ref, rtol=1e-12), (method,)69cases["pearson_0.9"] = {"stat": "pearson", "level": 0.9, **{k: v for k, v in m.items() if k != "reps"}, "reps_head": m["reps"][:10]}70out["bootstrap"] = {"x": x, "y": y, "indices": idx, "cases": cases}7172# --- permutation tests -----------------------------------------------------------------------73a = np.array([12.1, 9.8, 11.4, 15.2, 10.0])74b = np.array([8.7, 9.9, 7.4, 10.0, 6.5, 9.1])75perm = {"a": a, "b": b, "two": {}, "paired": {}}76alts = {"twoSided": "two-sided", "less": "less", "greater": "greater"}77for key, alt in alts.items():78 res = stats.permutation_test((a, b), lambda u, v, axis=-1: np.mean(u, axis=axis) - np.mean(v, axis=axis),79 permutation_type="independent", n_resamples=100000, alternative=alt, vectorized=True)80 perm["two"]["mean_" + key] = {"stat": res.statistic, "p": res.pvalue, "n": len(res.null_distribution),81 "null_sorted": np.sort(res.null_distribution)}82 res = stats.permutation_test((a, b), lambda u, v, axis=-1: np.median(u, axis=axis) - np.median(v, axis=axis),83 permutation_type="independent", n_resamples=100000, alternative=alt, vectorized=True)84 perm["two"]["median_" + key] = {"stat": res.statistic, "p": res.pvalue, "n": len(res.null_distribution)}85before = np.array([140.0, 152, 138, 160, 147, 155, 149, 151, 143])86after = np.array([135.0, 150, 139, 151, 140, 149, 150, 144, 138])87perm["before"], perm["after"] = before, after88for key, alt in alts.items():89 res = stats.permutation_test((before, after), lambda u, v, axis=-1: np.mean(u - v, axis=axis),90 permutation_type="samples", n_resamples=100000, alternative=alt, vectorized=True)91 perm["paired"]["mean_" + key] = {"stat": res.statistic, "p": res.pvalue, "n": len(res.null_distribution),92 "null_sorted": np.sort(res.null_distribution)}93# a larger problem where exact enumeration is the reference for the Monte Carlo approximation94g1 = np.round(np.random.default_rng(3).normal(0.0, 1, 10), 3)95g2 = np.round(np.random.default_rng(4).normal(0.9, 1, 10), 3)96res = stats.permutation_test((g1, g2), lambda u, v, axis=-1: np.mean(u, axis=axis) - np.mean(v, axis=axis),97 permutation_type="independent", n_resamples=10 ** 6, alternative="two-sided", vectorized=True)98perm["big"] = {"g1": g1, "g2": g2, "stat": res.statistic, "p": res.pvalue, "n": len(res.null_distribution)}99out["permutation"] = perm100101# --- Monte Carlo output summaries --------------------------------------------------------------102r = np.random.default_rng(99)103N = 3000104ia, ib, ic = r.normal(10, 2, N), r.uniform(0, 4, N), r.lognormal(0, 0.5, N)105yv = 3 * ia + ib ** 2 - 5 * ic + r.normal(0, 0.5, N)106probs = [0.01, 0.05, 0.10, 0.25, 0.50, 0.75, 0.90, 0.95, 0.99]107counts, edges = np.histogram(yv, bins=40)108points = 100109chk = sorted(set(max(2, int(np.floor(j * N / points + 0.5))) for j in range(1, points + 1)))110conv = [[n, yv[:n].mean(), yv[:n].std(ddof=1) / np.sqrt(n)] for n in chk]111rho = [stats.spearmanr(v, yv).statistic for v in (ia, ib, ic)]112out["montecarlo"] = {113 "inputs": [ia, ib, ic], "y": yv, "mean": yv.mean(), "sd": yv.std(ddof=1), "se": yv.std(ddof=1) / np.sqrt(N),114 "min": yv.min(), "max": yv.max(), "median": np.median(yv), "skew": stats.skew(yv), "kurt": stats.kurtosis(yv),115 "probs": probs, "percentiles": np.quantile(yv, probs), "hist_counts": counts, "hist_edges": edges,116 "convergence": conv, "spearman": rho, "contrib": (np.array(rho) ** 2 / (np.array(rho) ** 2).sum()),117 "input_spearman": stats.spearmanr(np.column_stack([ia, ib, ic])).statistic,118}119# Spearman ↔ Pearson relation for the bivariate normal used by the correlation step: ρs = (6/π)·asin(r/2)120out["montecarlo"]["spearman_to_pearson"] = [[rs, 2 * np.sin(np.pi * rs / 6)] for rs in (-0.9, -0.3, 0.0, 0.5, 0.8)]121122dump("resampling", out)123