Download htr/evaluation/bootstrap.py from spboucher04/htr-tiny: direct link, hf CLI and curl.
- Browser
- Download file 1.79 kB
-
https://huggingface.co/spboucher04/htr-tiny/resolve/main/htr/evaluation/bootstrap.py
- Command line
-
hf download hf://spboucher04/htr-tiny/htr/evaluation/bootstrap.py
-
curl -L -o bootstrap.py https://huggingface.co/spboucher04/htr-tiny/resolve/main/htr/evaluation/bootstrap.py
1.79 kB
| """Paired bootstrap confidence intervals over evaluation items (clustered by document to respect dependence).""" | |
| from __future__ import annotations | |
| import numpy as np | |
| def bootstrap_ci(values: list[float], groups: list[str] | None = None, n: int = 2000, seed: int = 0, alpha: float = 0.05) -> dict[str, float]: | |
| v = np.asarray(values, dtype=float) | |
| rng = np.random.default_rng(seed) | |
| if groups is None: | |
| groups = list(range(len(v))) | |
| g = np.asarray(groups) | |
| uniq = np.unique(g) | |
| idx_by = {u: np.where(g == u)[0] for u in uniq} | |
| means = np.empty(n) | |
| for i in range(n): | |
| pick = rng.choice(uniq, size=len(uniq), replace=True) | |
| sel = np.concatenate([idx_by[u] for u in pick]) | |
| means[i] = v[sel].mean() | |
| return {"mean": float(v.mean()), "ci_low": float(np.quantile(means, alpha / 2)), "ci_high": float(np.quantile(means, 1 - alpha / 2)), "n_items": int(len(v)), "n_groups": int(len(uniq))} | |
| def paired_bootstrap_diff(a: list[float], b: list[float], groups: list[str] | None = None, n: int = 2000, seed: int = 0) -> dict[str, float]: | |
| a, b = np.asarray(a, float), np.asarray(b, float) | |
| d = a - b | |
| r = bootstrap_ci(list(d), groups, n, seed) | |
| r["p_value_two_sided"] = float(2 * min((d.mean() <= 0) + 0.0, 1.0)) # placeholder replaced below | |
| rng = np.random.default_rng(seed) | |
| g = np.asarray(groups if groups is not None else list(range(len(d)))) | |
| uniq = np.unique(g) | |
| idx_by = {u: np.where(g == u)[0] for u in uniq} | |
| cnt = 0 | |
| for _ in range(n): | |
| pick = rng.choice(uniq, size=len(uniq), replace=True) | |
| sel = np.concatenate([idx_by[u] for u in pick]) | |
| if d[sel].mean() <= 0: | |
| cnt += 1 | |
| p = cnt / n | |
| r["p_value_two_sided"] = float(min(1.0, 2 * min(p, 1 - p))) | |
| return r | |