From 72a8dd272145bc81f2ef07b99d1861ff9a0c975f Mon Sep 17 00:00:00 2001 From: Sven Geboers Date: Sun, 29 Mar 2026 23:17:33 +0200 Subject: [PATCH] Fix RNG re-seeding per party and vectorize bootstrap loop Move rng initialization before the party loop so each party gets a unique segment of the random stream instead of identical sequences. Replace Python bootstrap loop with vectorized numpy indexing. --- analysis/political_axis.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/analysis/political_axis.py b/analysis/political_axis.py index 4117bf3..3b62608 100644 --- a/analysis/political_axis.py +++ b/analysis/political_axis.py @@ -649,6 +649,7 @@ def compute_party_bootstrap_cis( lo_pct = alpha / 2.0 hi_pct = 100.0 - lo_pct + rng = np.random.default_rng(seed) result: Dict[str, Dict] = {} for party, vectors in party_vectors.items(): @@ -670,12 +671,8 @@ def compute_party_bootstrap_cis( } continue - rng = np.random.default_rng(seed) - boot_centroids = np.empty((n_boot, mat.shape[1])) - - for b in range(n_boot): - idx = rng.integers(0, n_mps, size=n_mps) - boot_centroids[b] = mat[idx].mean(axis=0) + idx = rng.integers(0, n_mps, size=(n_boot, n_mps)) + boot_centroids = mat[idx].mean(axis=1) # (n_boot, dim) ci_lower = np.percentile(boot_centroids, lo_pct, axis=0) ci_upper = np.percentile(boot_centroids, hi_pct, axis=0)