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.
main
Sven Geboers 1 month ago
parent cd8aeec997
commit 72a8dd2721
  1. 9
      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)

Loading…
Cancel
Save