Supported Models¶
Model suite¶
The package organizes models along three column dimensions — likelihood (linear / non-linear), temporal structure (cross-section / panel), and outcome structure (single / flow). Each cell lists the spatial structures implemented for that combination.
Linear · Cross-section |
Linear · Panel |
Non-linear · Cross-section |
Non-linear · Panel |
|
|---|---|---|---|---|
Single |
Aspatial, SLX, SAR, SEM, SDM, SDEM |
Aspatial, SLX, SAR, SEM, SDM, SDEM |
Aspatial, SAR, SEM, SDM |
SAR, SEM |
Flow |
Aspatial, SAR, SEM |
Aspatial, SAR, SEM |
Aspatial, SAR |
Aspatial, SAR |
Panel models come in fixed-effects and random-effects variants, and the linear panel family additionally has dynamic (lagged-dependent-variable) forms. Every spatial flow model has a separable counterpart that pins \(\rho_w = -\rho_d \rho_o\); the non-linear flow cell covers both negative binomial and Poisson observation models. The sections below list every class individually.
Cross Sectional Models¶
OLS¶
SLX¶
SAR¶
SEM¶
SDM¶
SDEM¶
Panel Models¶
OLS panel¶
SAR panel¶
SEM panel¶
SDM panel¶
SDEM panel¶
SLX panel¶
OLS panel (Random Effects)¶
SAR panel (Random Effects)¶
SEM panel (Random Effects)¶
SDEM panel (Random Effects)¶
Dynamic Panel Models¶
OLSPanelDynamic (Dynamic Linear Model)¶
SDMRPanelDynamic (Dynamic Restricted Spatial Durbin)¶
SDMUPanelDynamic (Dynamic Unrestricted Spatial Durbin)¶
SARPanelDynamic (Dynamic SAR)¶
SEMPanelDynamic (Dynamic SEM)¶
SDEMPanelDynamic (Dynamic SDEM)¶
SLXPanelDynamic (Dynamic SLX)¶
Non-Linear Models¶
SARProbit¶
Note the \(a\) term: this is a region-random-effects specification, in which
\(\rho\) acts on region-level latent utilities and observations are nested within
regions via region_ids. It is not the standard spatial probit of the LeSage
toolbox. For spatial binary outcomes prefer the Pólya–Gamma logit classes
(SARLogit, SARLogitStructural, SEMLogit), which are conjugate and have a
Gibbs sampler.
Tobit (SAR Tobit)¶
Tobit (SEM Tobit)¶
Tobit (SDM Tobit)¶
Panel Tobit (SAR)¶
Panel Tobit (SEM)¶
SARNegBin (Reduced Form)¶
No latent \(\sigma\) — spatial dependence enters only through the mean propagator. Supports both NUTS and Pólya–Gamma Gibbs sampling.
SARNegBinStructural (Structural Form)¶
Includes latent \(\sigma\) — structural form with explicit noise. Gibbs sampling only (PG augmentation).
SARZINB¶
Zero-inflated negative binomial with spatial lag on the log-mean.
Logit¶
Non-spatial logistic regression baseline.
NegBin¶
Non-spatial negative binomial baseline.
SARLogit (Reduced Form)¶
Spatial lag on the latent log-odds, written with the multiplier applied to the mean. Pólya–Gamma Gibbs sampler only — no NUTS path.
SARLogitStructural (Structural Form)¶
The same model as SARLogit, parameterised without inverting \((I - \rho W)\).
Pólya–Gamma Gibbs sampler only.
SEMLogit¶
Spatial error on the latent log-odds. The logit link fixes \(\sigma^2 = 1\), so it does not appear in the posterior. Pólya–Gamma Gibbs sampler only.
Flow Models¶
Vectorize the origin-destination flow matrix to \(y \in \mathbb{R}^{N}\) with \(N = n^2\), and define destination, origin, and network weight matrices as \(W_d\), \(W_o\), and \(W_w\).
OLSFlow¶
NegBinFlow¶
SARFlow¶
SARFlowSeparable¶
SARNegBinFlow¶
SARNegBinFlowSeparable¶
SARPoissonFlow¶
No dispersion parameter. Sampled by auxiliary-mixture Gibbs (Frühwirth-Schnatter & Wagner 2006) rather than Pólya–Gamma, which admits no exact Poisson representation.
SARPoissonFlowSeparable¶
The recommended Poisson flow model — the separable restriction removes the weakly-identified \(\rho\) ridge of the unrestricted variant.
SEMFlow¶
SEMFlowSeparable¶
Panel Flow Models¶
Stack the flow models above across \(T\) periods in time-first order. The NB panel variants currently operate in pooled mode.
OLSFlowPanel¶
NegBinFlowPanel¶
SARFlowPanel¶
SARFlowSeparablePanel¶
SARNegBinFlowPanel¶
SARNegBinFlowSeparablePanel¶
SEMFlowPanel¶
SEMFlowSeparablePanel¶
Specification Tests¶
Lagrange-Multiplier tests for choosing a spatial specification. Each statistic
is evaluated at every posterior draw, giving a posterior distribution rather
than a point estimate. Call them directly from
neighbayes.diagnostics.lmtests, or through spatial_diagnostics() and
spatial_diagnostics_decision() on any fitted model.
Test |
\(H_0\) |
Alternative |
df |
Null model |
|---|---|---|---|---|
LM-Lag |
\(\rho = 0\) |
SAR |
1 |
OLS |
LM-Error |
\(\lambda = 0\) |
SEM |
1 |
OLS |
LM-WX |
\(\gamma = 0\) |
SLX |
\(k_{wx}\) |
SAR |
LM-SDM (joint) |
\(\rho = \gamma = 0\) |
SDM |
\(1 + k_{wx}\) |
OLS |
LM-SLX-Error (joint) |
\(\lambda = \gamma = 0\) |
SDEM |
\(1 + k_{wx}\) |
OLS |
LM-WX-SEM |
\(\gamma = 0\) in SEM |
SDEM |
\(k_{wx}\) |
SEM |
LM-Error-SDM |
\(\lambda = 0\) in SDM |
SDARAR |
1 |
SDM |
LM-Lag-SDEM |
\(\rho = 0\) in SDEM |
SDARAR |
1 |
SDEM |
Robust LM-Lag |
\(\rho = 0\), robust to \(\lambda\) |
SAR vs SEM |
1 |
OLS |
Robust LM-Error |
\(\lambda = 0\), robust to \(\rho\) |
SEM vs SAR |
1 |
OLS |
Robust LM-Lag-SDM |
\(\rho = 0\), robust to \(\gamma\) |
SDM |
1 |
SLX |
Robust LM-WX |
\(\gamma = 0\), robust to \(\rho\) |
SDM |
\(k_{wx}\) |
SAR |
Robust LM-Error-SDEM |
\(\lambda = 0\), robust to \(\gamma\) |
SDEM |
1 |
SLX |
The robust variants use the Neyman orthogonal score of Doğan, Taşpınar & Bera (2021), which removes the correlation between the test score and the nuisance score:
The same machinery extends to balanced panels — with a \(T\) multiplier on the
information matrix, under the bayesian_panel_lm_* prefix — and to
origin–destination flow models on Kronecker weights \(W_d\), \(W_o\), \(W_w\), under
bayesian_lm_flow_*.
Sources¶
Doğan, O., Taşpınar, S., Bera, A.K. (2021). “A Bayesian robust chi-squared test for testing simple hypotheses.” Journal of Econometrics, 222(2), 933–958.
Koley, M., Bera, A.K. (2024). “To Use, or Not to Use the Spatial Durbin Model? – That Is the Question.” Spatial Economic Analysis, 19(1), 30–56.
Bera, A.K., Yoon, M.J. (1993). “Specification testing with locally misspecified alternatives.” Econometric Theory, 9(4), 649–658.
Anselin, L., Bera, A.K., Florax, R., Yoon, M.J. (1996). “Simple diagnostic tests for spatial dependence.” Regional Science and Urban Economics, 26(1), 77–104.
LeSage, J.P., Pace, R.K. (2008). “Spatial Econometric Modeling of Origin–Destination Flows.” Journal of Regional Science, 48(5), 941–967.
Sampling Backends¶
Choosing a sampler¶
fit() takes sampler={"gibbs", "nuts", None}, and None — the default —
selects Gibbs whenever the model has a registered Gibbs sampler, and NUTS
otherwise. For SAR/SEM/SDM/SDEM, the count families, the ZINB model and the
Gaussian panel families, that means Gibbs unless you ask for something else.
NUTS is not universally available. The Pólya–Gamma logit classes
(SARLogit, SARLogitStructural, SEMLogit) and the auxiliary-mixture
Poisson flow classes build no PyMC graph at all, and fit(sampler="nuts")
raises NotImplementedError. Robust (Student-t) models are the mirror case:
no Gibbs sampler supports them, so robust=True requires NUTS.
target_accept is NUTS-only and raises TypeError if passed with Gibbs.
Execution backends¶
For any Gibbs sampler, gibbs_backend selects the execution path:
Value |
Behaviour |
|---|---|
|
default — JAX when installed and supported by the family, else NumPy |
|
the sweep JIT-compiled into one XLA kernel; chains vectorised under |
|
pure NumPy/SciPy; chains as separate processes via |
Both backends implement the same sampler and target the same posterior.
Gibbs Sampler (Gaussian models)¶
Gaussian cross-sectional models (SAR, SEM, SDM, SDEM) and the Gaussian panel families exploit conditional conjugacy with a 3-block strategy:
Block |
Full conditional |
Update |
|---|---|---|
β | ρ, σ², y |
Normal |
Direct draw (conjugate) |
σ² | β, ρ, y |
Inverse-Gamma |
Direct draw (conjugate) |
ρ/λ | β, σ², y |
1-D non-conjugate |
Adaptive slice sampling |
SAR and SDM update the spatial parameter with β and σ² integrated out (a collapsed conditional); SEM and SDEM update it conditional on them.
model = SAR(y=y, X=X, W=W)
idata = model.fit(draws=2000, tune=1000, chains=4) # Gibbs, by default
The family accepts two options beyond the shared fit() arguments:
slice_width (initial slice interval for ρ/λ) and chain_method (JAX
backend chain mapping). See the
Gibbs sampler how-to for details.
Gibbs Sampler (SAR Negative Binomial)¶
SARNegBin (reduced form) supports a Pólya–Gamma Gibbs sampler via sampler="gibbs" (the default):
model = SARNegBin(y=y_int, X=X, W=W)
idata = model.fit(draws=2000, tune=1000, chains=4)
The reduced form has no latent σ² — spatial dependence enters only through the mean propagator \((I - \rho W)^{-1}\):
Block |
Full conditional |
Update |
|---|---|---|
ω | β, ρ, α, y |
Pólya–Gamma |
Direct draw (conjugate augmentation) |
β | ρ, ω, y |
Normal |
Direct draw (conjugate, via \(\tilde{X} = (I-\rho W)^{-1}X\)) |
ρ | ω, y |
1-D non-conjugate |
Adaptive slice sampling (β marginalised) |
α | y, η |
1-D non-conjugate |
Slice sampling on log(α) |
SARNegBinStructural (structural form) adds latent η and σ² blocks via a separate Gibbs sampler in neighbayes.samplers.negbin.
Gibbs Sampler (NB flow models)¶
NB flow models (SARNegBinFlow, SARNegBinFlowSeparable, NegBinFlow) support a Pólya–Gamma Gibbs sampler via sampler="gibbs":
model = SARNegBinFlow(y_int, X, G) # positional: (y, X, W)
idata = model.fit(sampler="gibbs", draws=2000, tune=1000, chains=4)
The sampler uses a reduced-form Pólya–Gamma augmentation strategy with no σ² parameter — spatial dependence enters only through the mean propagator \(A^{-1}\):
Block |
Full conditional |
Update |
|---|---|---|
ω | β, α, y |
Pólya–Gamma |
Direct draw (conjugate augmentation) |
β | ρ, ω, y |
Normal |
Direct draw (conjugate, via \(\tilde{X} = A^{-1}X\)) |
ρ | ω, y |
1-D non-conjugate |
Adaptive slice sampling (β marginalised) |
α | y, η |
1-D non-conjugate |
Slice sampling on log(α) |
For the unrestricted model (SARNegBinFlow), each ρ parameter (ρ_d, ρ_o, ρ_w) is updated via independent 1-D slice sampling with β marginalised out. For the separable model (SARNegBinFlowSeparable), ρ_w = −ρ_d·ρ_o is deterministic and only ρ_d and ρ_o are sampled. The aspatial NegBinFlow omits the ρ block entirely.
Log-Determinant Methods¶
The spatial Jacobian \(\log|I - \rho W|\) is evaluated at every MCMC draw, and is
the term that makes large problems expensive. logdet_method is set on the
model constructor, not on fit(), and both samplers honour it. Leaving it
at None auto-selects by size, by whether \(W\) is symmetric, and by how much
fill-in a sparse factorization would incur.
Auto-selection¶
\(n\) |
\(W\) |
Chosen |
Why |
|---|---|---|---|
≤ 500 |
any |
|
one \(O(n^3)\) eigendecomposition, then \(O(n)\) per ρ — exact and cheap at this size |
≤ 60000 |
symmetric |
|
sparse Cholesky at adaptively-chosen AAA support points; exact, root-exponential convergence |
≤ 60000 |
non-symmetric |
|
the same rational scheme over sparse LU (KLU), for directed graphs — k-nearest-neighbour, travel time, migration |
> 60000 |
any |
|
stochastic Chebyshev expansion; no factorization, at the cost of stochastic error |
Fill-in guard. Size alone does not predict factorization cost — a dense or
hub-dominated graph blows up under Cholesky regardless of \(n\). Before
committing to an exact path the selector estimates
\(\mathrm{nnz}(W^2)/\mathrm{nnz}(W)\) in \(O(\mathrm{nnz})\); if that exceeds 20 it
warns and falls back to cheb_stochastic. A KNN-50 graph or a fully dense \(W\)
at moderate \(n\) takes that branch.
The full set¶
Method |
Exact |
Notes |
|---|---|---|
|
✅ |
full eigendecomposition; the reference answer at small \(n\) |
|
✅ |
CHOLMOD factorizations at AAA support points; auto choice for symmetric \(W\) |
|
✅ |
AAA rational approximation over sparse LU; handles non-symmetric \(W\) |
|
✅ |
sparse Cholesky at Chebyshev nodes |
|
✅ |
sparse LU at Chebyshev nodes |
|
✅ |
deterministic Chebyshev from exact eigenvalues |
|
✅ |
JAX-native sparse CHOLMOD; requires |
|
≈ |
spline interpolation over a precomputed ρ grid |
|
✗ |
stochastic Chebyshev (Han et al. 2015); auto choice above the cutoff |
|
✗ |
Stochastic Lanczos Quadrature, D-symmetrised |
|
✗ |
truncated trace series; legacy, retained for the flow NUTS path |
Flow models take an additional value, "resolvent", which is their default for
the unrestricted three-ρ case: it samples via the resolvent-Kronecker gradient
rather than evaluating a scalar log-determinant.
Cutoffs are configurable through the environment:
NEIGHBAYES_LOGDET_EIGEN_MAX_N (default 500),
NEIGHBAYES_LOGDET_CHEB_MAX_N (default 60000), and
NEIGHBAYES_LOGDET_MAX_FILLIN_RATIO (default 20).
The constructor reports the valid names on a bad value, so the list above can be checked against any installation:
SAR(y=y, X=X, W=W, logdet_method="?") # ValueError lists every valid option