Download code/pocket_math/stats.py from Palaestra/pocket-math-ops: direct link, hf CLI and curl.
- Browser
- Download file 3.46 kB
-
https://huggingface.co/datasets/Palaestra/pocket-math-ops/resolve/main/code/pocket_math/stats.py
- Command line
-
hf download hf://datasets/Palaestra/pocket-math-ops/code/pocket_math/stats.py
-
curl -L -o stats.py https://huggingface.co/datasets/Palaestra/pocket-math-ops/resolve/main/code/pocket_math/stats.py
3.46 kB
| from __future__ import annotations | |
| import math | |
| from dataclasses import dataclass | |
| import numpy as np | |
| Results = dict[str, list[bool]] | |
| # Acklam's rational approximation of the inverse normal CDF, relative error about 1e-9. | |
| _A = (-3.969683028665376e01, 2.209460984245205e02, -2.759285104469687e02, | |
| 1.383577518672690e02, -3.066479806614716e01, 2.506628277459239e00) | |
| _B = (-5.447609879822406e01, 1.615858368580409e02, -1.556989798598866e02, | |
| 6.680131188771972e01, -1.328068155288572e01) | |
| _C = (-7.784894002430293e-03, -3.223964580411365e-01, -2.400758277161838e00, | |
| -2.549732539343734e00, 4.374664141464968e00, 2.938163982698783e00) | |
| _D = (7.784695709041462e-03, 3.224671290700398e-01, 2.445134137142996e00, 3.754408661907416e00) | |
| _P_LOW = 0.02425 | |
| def pass_at_1(r: Results) -> float: | |
| if not r: | |
| raise ValueError("no problems") | |
| return float(np.mean([np.mean(v) for v in r.values()])) | |
| def pass_at_k(n: int, c: int, k: int) -> float: | |
| if not 0 <= c <= n or not 1 <= k <= n: | |
| raise ValueError(f"need 0 <= c <= n and 1 <= k <= n, got n={n}, c={c}, k={k}") | |
| if n - c < k: | |
| return 1.0 | |
| return 1.0 - math.comb(n - c, k) / math.comb(n, k) | |
| def normal_quantile(p: float) -> float: | |
| if not 0.0 < p < 1.0: | |
| raise ValueError("p must be in (0, 1)") | |
| if p > 1 - _P_LOW: | |
| return -normal_quantile(1 - p) | |
| if p < _P_LOW: | |
| q = math.sqrt(-2 * math.log(p)) | |
| num = ((((_C[0] * q + _C[1]) * q + _C[2]) * q + _C[3]) * q + _C[4]) * q + _C[5] | |
| return num / ((((_D[0] * q + _D[1]) * q + _D[2]) * q + _D[3]) * q + 1) | |
| q = p - 0.5 | |
| r = q * q | |
| num = (((((_A[0] * r + _A[1]) * r + _A[2]) * r + _A[3]) * r + _A[4]) * r + _A[5]) * q | |
| return num / (((((_B[0] * r + _B[1]) * r + _B[2]) * r + _B[3]) * r + _B[4]) * r + 1) | |
| class Comparison: | |
| diff: float | |
| lo: float | |
| hi: float | |
| p_value: float | |
| n_problems: int | |
| def _paired_diffs(a: Results, b: Results) -> np.ndarray: | |
| uids = sorted(a.keys() & b.keys()) | |
| if not uids: | |
| raise ValueError("no shared problems") | |
| return np.array([np.mean(a[u]) - np.mean(b[u]) for u in uids]) | |
| def paired_bootstrap( | |
| a: Results, b: Results, n_boot: int = 10000, seed: int = 0, alpha: float = 0.05 | |
| ) -> Comparison: | |
| d = _paired_diffs(a, b) | |
| n = len(d) | |
| rng = np.random.default_rng(seed) | |
| boots = np.empty(n_boot) | |
| chunk = max(1, 2_000_000 // n) | |
| for start in range(0, n_boot, chunk): | |
| size = min(chunk, n_boot - start) | |
| boots[start : start + size] = d[rng.integers(0, n, size=(size, n))].mean(axis=1) | |
| lo, hi = np.quantile(boots, [alpha / 2, 1 - alpha / 2]) | |
| le = int((boots <= 0).sum()) | |
| ge = int((boots >= 0).sum()) | |
| p_value = min(1.0, 2 * (min(le, ge) + 1) / (n_boot + 1)) | |
| return Comparison(float(d.mean()), float(lo), float(hi), p_value, n) | |
| def mde(a: Results, b: Results | None = None, alpha: float = 0.05, power: float = 0.8) -> float: | |
| if b is None: | |
| per_problem = np.array([np.mean(v) for v in a.values()]) | |
| scale = math.sqrt(2) | |
| else: | |
| per_problem = _paired_diffs(a, b) | |
| scale = 1.0 | |
| n = len(per_problem) | |
| if n < 2: | |
| raise ValueError("need at least 2 problems") | |
| se = scale * float(per_problem.std(ddof=1)) / math.sqrt(n) | |
| return (normal_quantile(1 - alpha / 2) + normal_quantile(power)) * se | |