Use Geary's C normality variance in spatial_autocorr (fixes #1183)#1197
Open
gaoflow wants to merge 4 commits into
Open
Use Geary's C normality variance in spatial_autocorr (fixes #1183)#1197gaoflow wants to merge 4 commits into
gaoflow wants to merge 4 commits into
Conversation
) `_analytic_pval` reused the Moran's I sampling variance under normality for both `mode="moran"` and `mode="geary"`, swapping only `E[score]`. Geary's C has a different normality variance (Cliff & Ord 1981; pysal/esda `Geary`), so the analytic `pval_norm` for `mode="geary"` was miscalibrated. Branch on the statistic and apply `Var[C] = ((2*S1 + S2)(n-1) - 4*S0^2) / (2*(n+1)*S0^2)` for Geary's C, leaving Moran's I unchanged. Add a regression test asserting the emitted `var_norm` matches the closed-form variance of the chosen statistic.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1197 +/- ##
=======================================
Coverage 76.55% 76.55%
=======================================
Files 63 63
Lines 9067 9069 +2
Branches 1521 1522 +1
=======================================
+ Hits 6941 6943 +2
Misses 1541 1541
Partials 585 585
🚀 New features to boost your workflow:
|
Member
|
Thanks a lot! I will merge this as soon as I get approval of someone else as well |
timtreis
approved these changes
Jun 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
squidpy.gr.spatial_autocorrshares a single_analytic_pvalimplementation betweenmode="moran"andmode="geary", swapping onlyE[score]. The variance it applies is the Moran's I normality variance (Cliff & Ord 1981); Geary's C has a different normality variance, so the analytic p-value (pval_norm/var_norm) formode="geary"was miscalibrated.Fixes #1183.
Fix
In
_analytic_pval, branch on the statistic and use the Geary's C normality variance (matchingpysal/esda'sGeary):Moran's I is left exactly as before.
Verification
Reproduced with row-standardised 6-NN weights (the same setup as the issue):
var_norm(Geary)Geary.VC_norm1.4583e-031.7014e-031.7014e-031.7014e-03Computing the Geary closed-form from squidpy's own row-standardised weight moments reproduces the post-fix
var_normexactly (relative diff0.0), and it clearly differs from the old Moran value — confirming the previous code applied the wrong formula. The reporter's 100k-trial null study shows the analytic p-value going from badly non-uniform (KS p ≈1e-108) to well-calibrated.Tests
Added
test_spatial_autocorr_var_norm_formula(parametrised overmoran/geary) asserting the emittedvar_normmatches the closed-form variance of the chosen statistic. It passes with the fix and fails on the Geary case without it. All existingspatial_autocorrtests continue to pass; Moran's I output is unchanged.