Date: 2026-10-25. Scope: what is needed to port PySAL faithfully to Elixir + Rust.

1. PySAL ecosystem

Meta-package PySAL 26.7 (PyPI).

GroupPackageVersionspatex relevance
liblibpysal (weights, Graph)4.15.1M0-M1
exploreesda2.10.0M2-M3
exploregiddy2.3.9M8
explorepointpats2.6.0M6
exploreinequality-M8
exploresegregation, spaghetti, momepy, access-out of scope
modelspreg1.9.1M7
modelmgwr2.2.1M9
modelspopt0.8.0M9
modelspglm, spint, spml, tobler-out of scope
vizmapclassify2.11.0M4
vizsplot-out of scope

2. Graph model

Source: libpysal/graph/base.py; W to Graph migration guide. Graph replaces the legacy W.

  • Storage: adjacency Series indexed by (focal, neighbor), sorted by focal order. Every node appears at least once; islands are zero-weight self-loops.
  • Constructors: from_arrays, from_adjacency, from_sparse, from_dicts.
  • Builders: build_contiguity (rook / queen / by_perimeter), build_knn, build_kernel, build_distance_band, build_block_contiguity, build_fuzzy_contiguity, build_triangulation (delaunay, gabriel, relative_neighborhood, voronoi).
  • Transforms:
CodeNameRule
Rrow-standardizew / row sum (isolates stay 0)
Ddouble-standardizew / global sum
Bbinarynonzero becomes 1
Vvariance-stabilizingrow-normalize by sqrt(sum w^2), then scale by n / sum(all)

3. Algorithm details for a faithful port

3.1 Contiguity hashing

Source: libpysal/graph/_contiguity.py (_vertex_set_intersection).

  • Queen: hash map from coordinate tuple to the set of geometries containing it.
  • Rook: hash map from sorted vertex-pair edge to the set of geometries.
  • Every set with size >= 2 becomes a clique of neighbors.
  • Multipolygon phantom edges (between parts of the same geometry) are skipped.
  • Rust: hash exact f64 bit patterns (no tolerance), matching PySAL's exact-coordinate behavior.

3.2 Global Moran's I

Source: esda/moran.py. With z = y - mean(y), weights W, S0/S1/S2 the usual weight sums:

  • I = n / S0 * sum_i z_i (Wz)_i / sum z^2
  • E[I] = -1 / (n - 1)
  • Normality: VI_norm = (n^2 S1 - n S2 + 3 S0^2) / ((n-1)(n+1) S0^2) - E[I]^2
  • Randomization, with k = m4 / m2^2:
    • A = n ((n^2 - 3n + 3) S1 - n S2 + 3 S0^2)
    • B = k ((n^2 - n) S1 - 2 n S2 + 6 S0^2)
    • VI_rand = (A - B) / ((n-1)(n-2)(n-3) S0^2) - E[I]^2
  • Analytic p-value: normal survival function if z > 0, else CDF; doubled when two_tailed.
  • Permutations (default 999): EI_sim = mean of sims; seI_sim = std with ddof=0; z_sim = (I - EI_sim) / seI_sim.
  • p_sim = (larger + 1) / (P + 1), larger = count of sims >= observed, flipped to P - larger when that is smaller than larger.
  • Default transform: "r" (row-standardized).

3.3 Local Moran

Sources: esda/moran.py, esda/crand.py.

  • z standardized with ddof=0; I_i = (n - 1) z_i (Wz)_i / sum z^2.
  • Quadrants: 1 = HH, 2 = LH, 3 = LL, 4 = HL (geoda_quads swaps 2 and 3).
  • Conditional randomization: hold z_i fixed, draw card(i) of the other n - 1 values without replacement, recompute the local statistic.
  • Islands: lag 0, I = 0.

3.4 Pseudo p-value alternatives

Source: esda/significance.py.

AlternativeRule
:directed (default)folded: tail chosen by sign of the statistic relative to simulated mean
:two_sidedmin tail x 2, capped at 1
:greaterupper tail
:lesserlower tail

All use the (count + 1) / (P + 1) convention.

4. Rust crate choices

Versions from crates.io as of 2026-10.

NeedCrateVersionRationale
NIF bindingrustler0.39.0Same as GeoFrame; Mix package rustler ~> 0.38, with rustler_precompiled 0.10.1 for the precompiled release (M10)
Geometrygeo + wkb0.33.1WKB polygon parsing for contiguity; geometry types and predicates
Spatial indexrstar0.13.0R-tree for distance band / kNN fallback
Spatial indexkiddo6.3.0Fast kd-tree kNN
Triangulationspade-Delaunay (Gabriel / RNG derived from it)
Linear algebrafaer0.24.4Pure Rust dense and sparse; no system BLAS or LAPACK, so precompiled NIFs stay portable. Preferred over ndarray-linalg (needs system BLAS/LAPACK)
Linear algebra (alt.)nalgebra0.35.0Considered; weaker sparse and large-matrix performance
Sparse (alt.)sprs0.11.6Considered; CSR is simple enough to own
RNGrand + rand_chacha0.10.3 / 0.10.0ChaCha with seed_from_u64; reproducible across platforms and versions
Parallelismrayon1.12.0Permutation loops; per-site / per-chunk derived streams give thread-count-independent results
Distributionsstatrs0.19.1Normal CDF/SF, t, chi-square, F
Optimizationargmin0.11.0Brent for ML rho (or hand-rolled Brent to avoid the dependency)
Graph algorithmspetgraph0.8.3Connected components, MST (SKATER)

Crates marked "-" had no version pinned when this synthesis was written.

5. Elixir ecosystem gap

  • Hex has ex_rstar (spatial index) and an image-only moransi; no ESDA, weights, or spatial regression library.
  • Rust: wbspatialstats, mermin-stats, scirs2-spatial exist but are unvetted and not a PySAL-grade ESDA suite.
  • Conclusion: spatex fills a real gap, and the pure-Rust spatex_core can serve the wider Rust community.

6. Determinism and parity stance

  • Same seed gives identical results across runs and thread counts.
  • Bit-for-bit equality with numpy's RNG is not a goal.
  • Deterministic statistics must match PySAL to 1e-9; permutation p-values are compared statistically.