Skip to content

speedup: vectorise Monte Carlo permutation test in fisher_test_cci#12

Merged
jsnagai merged 3 commits into
mainfrom
copilot/speedup-montecarlo-simulation
Apr 2, 2026
Merged

speedup: vectorise Monte Carlo permutation test in fisher_test_cci#12
jsnagai merged 3 commits into
mainfrom
copilot/speedup-montecarlo-simulation

Conversation

Copilot AI commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

The Monte Carlo permutation loop inside fisher_test_cci called fisher_exact 1 000 times per cell pair via a plain Python for loop, with np.random.seed(42) reset on every iteration (making all permutations across rows identical). perm_p_value was computed but never stored.

Changes

  • Bug fix – RNG seed reset in loop: replaced np.random.seed(42) inside the inner loop with a single np.random.default_rng(42) created once per function call.
  • Vectorised permutation generation: all B permutations are now produced in one NumPy call instead of B sequential calls:
    permuted = rng.permuted(np.tile(flat, (B, 1)), axis=1).reshape(B, 2, 2)
  • Deduplicated fisher_exact calls: 4 values yield at most 4! = 24 unique 2×2 arrangements; np.unique(..., return_inverse=True) reduces ~1 000 calls per row to ≤ 24.
  • Vectorised p-value counting: replaced the manual count += 1 loop with (perm_pvals <= p_value + _PVAL_TOL).sum().
  • perm_p_value now stored: added as a perm_p_value column in the output stats DataFrame.
  • Eliminated code duplication: extracted a _pairwise_stats helper shared by both branches of fisher_test_cci.

@jsnagai jsnagai marked this pull request as ready for review April 1, 2026 14:49
@jsnagai jsnagai merged commit 44f99fb into main Apr 2, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants