Bayesian LM tests against classical spreg¶
Cross-implementation check for the Lagrange-Multiplier specification tests.
neighbayes evaluates the LM statistic at every posterior draw and reports the
posterior mean; spreg computes the classical point statistic from an OLS fit.
On data simulated under \(H_0\) with weakly-informative priors the two should tell
the same story, and where they diverge the reason should be understood rather
than assumed.
This is evidence about the implementation, not a guide to using it — for that see How to run Bayesian LM specification tests.
import warnings
import libpysal
import numpy as np
import pandas as pd
from spreg import OLS as SpregOLS
from spreg import LMtests as SpregLMtests
from neighbayes.diagnostics.lmtests import (
bayesian_lm_error_test,
bayesian_lm_lag_test,
bayesian_lm_sdm_joint_test,
bayesian_lm_slx_error_joint_test,
bayesian_lm_wx_sem_test,
bayesian_lm_wx_test,
bayesian_robust_lm_error_sdem_test,
bayesian_robust_lm_error_test,
bayesian_robust_lm_lag_sdm_test,
bayesian_robust_lm_lag_test,
bayesian_robust_lm_wx_test,
)
from neighbayes.models import OLS, SAR, SEM, SLX
warnings.filterwarnings("ignore", category=FutureWarning)
warnings.filterwarnings("ignore", category=UserWarning)
import geopandas as gpd
from libpysal.graph import Graph
from libpysal.weights import Rook
# Generate data under H0: no spatial dependence
np.random.seed(42)
# Load Columbus dataset for spatial weights
columbus_path = libpysal.examples.get_path("columbus.shp")
gdf = gpd.read_file(columbus_path)
# Create a Graph (modern libpysal API) for neighbayes models
# Row-standardize the graph so spatial models work correctly
g = Graph.build_contiguity(gdf, rook=True).transform("r")
n = g.n
# Legacy W for spreg comparison
w_spreg = Rook.from_shapefile(columbus_path)
w_spreg.transform = "r"
# Get sparse and dense W matrices
W_sparse = g.sparse.tocsr().astype(np.float64)
W_dense = np.array(W_sparse.todense())
# Design matrix
k = 3
X = np.column_stack([np.ones(n), np.random.normal(size=(n, k - 1))])
beta_true = np.array([1.0, 2.0, -1.5])
y = X @ beta_true + np.random.normal(scale=1.0, size=n)
print(f"W shape: {W_sparse.shape}, nnz: {W_sparse.nnz}")
W shape: (49, 49), nnz: 200
# Fit OLS model (null for joint tests)
ols_model = OLS(y=y, X=X, W=g)
ols_model.fit(draws=5000, tune=5000, chains=4, random_seed=42)
# Fit SAR model (null for LM-WX and robust LM-WX)
sar_model = SAR(y=y, X=X, W=g)
sar_model.fit(draws=5000, tune=5000, chains=4, random_seed=42)
# Fit SLX model (null for robust LM-Lag-SDM and robust LM-Error-SDEM)
slx_model = SLX(y=y, X=X, W=g)
slx_model.fit(draws=5000, tune=5000, chains=4, random_seed=42)
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (4 chains in 2 jobs)
NUTS: [beta, sigma2]
Sampling 4 chains for 5_000 tune and 5_000 draw iterations (20_000 + 20_000 draws total) took 13 seconds.
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (4 chains in 2 jobs)
NUTS: [beta, sigma2]
Sampling 4 chains for 5_000 tune and 5_000 draw iterations (20_000 + 20_000 draws total) took 14 seconds.
Gibbs sampling (sar): 4 chains for 5,000 tune and 5,000 draw iterations (4 x 10,000 = 40,000 draws total)
Sampling took 2s (19,536 draws/s)
<xarray.DataTree>
Group: /
├── Group: /posterior
│ Dimensions: (chain: 4, draw: 5000, coefficient: 5)
│ Coordinates:
│ * chain (chain) int64 32B 0 1 2 3
│ * draw (draw) int64 40kB 0 1 2 3 4 5 ... 4994 4995 4996 4997 4998 4999
│ * coefficient (coefficient) <U4 80B 'x0' 'x1' 'x2' 'W*x1' 'W*x2'
│ Data variables:
│ beta (chain, draw, coefficient) float64 800kB 1.111 2.242 ... 0.1962
│ sigma2 (chain, draw) float64 160kB 1.597 1.393 1.32 ... 1.29 1.257
│ sigma (chain, draw) float64 160kB 1.264 1.18 1.149 ... 1.136 1.121
│ Attributes:
│ created_at: 2026-11-09T16:19:44.973854+00:00
│ creation_library: ArviZ
│ creation_library_version: 1.3.1
│ creation_library_language: Python
│ inference_library: pymc
│ inference_library_version: 6.4.0
│ sample_dims: ['chain', 'draw']
│ sampling_time: 14.444284915924072
│ tuning_steps: 5000
├── Group: /sample_stats
│ Dimensions: (chain: 4, draw: 5000)
│ Coordinates:
│ * chain (chain) int64 32B 0 1 2 3
│ * draw (draw) int64 40kB 0 1 2 3 4 ... 4996 4997 4998 4999
│ Data variables: (12/18)
│ process_time_diff (chain, draw) float64 160kB 0.001085 ... 0.0001891
│ energy_error (chain, draw) float64 160kB 0.3811 ... -0.179
│ largest_eigval (chain, draw) float64 160kB nan nan nan ... nan nan
│ energy (chain, draw) float64 160kB 91.67 90.34 ... 87.35
│ perf_counter_diff (chain, draw) float64 160kB 0.001085 ... 0.0001892
│ lp (chain, draw) float64 160kB -88.98 -87.89 ... -86.2
│ ... ...
│ tree_depth (chain, draw) int64 160kB 4 3 3 3 3 3 ... 4 3 4 2 3 2
│ step_size (chain, draw) float64 160kB 0.5279 0.5279 ... 0.571
│ n_steps (chain, draw) float64 160kB 15.0 7.0 7.0 ... 7.0 3.0
│ max_energy_error (chain, draw) float64 160kB 0.9167 -0.2973 ... -0.179
│ diverging (chain, draw) bool 20kB False False ... False False
│ divergences (chain, draw) int64 160kB 0 0 0 0 0 0 ... 0 0 0 0 0 0
│ Attributes:
│ created_at: 2026-11-09T16:19:44.999969+00:00
│ creation_library: ArviZ
│ creation_library_version: 1.3.1
│ creation_library_language: Python
│ inference_library: pymc
│ inference_library_version: 6.4.0
│ sample_dims: ['chain', 'draw']
│ sampling_time: 14.444284915924072
│ tuning_steps: 5000
└── Group: /observed_data
Dimensions: (obs_dim_0: 49)
Coordinates:
* obs_dim_0 (obs_dim_0) int64 392B 0 1 2 3 4 5 6 7 ... 42 43 44 45 46 47 48
Data variables:
obs (obs_dim_0) float64 392B 2.206 -0.2238 -0.5325 ... 3.193 -0.03629
Attributes:
created_at: 2026-11-09T16:19:45.003660+00:00
creation_library: ArviZ
creation_library_version: 1.3.1
creation_library_language: Python
inference_library: pymc
inference_library_version: 6.4.0
sample_dims: []Bayesian LM statistics¶
Evaluated at the null model each test requires.
# Fit the SEM null required by LM-WX-SEM (Bayesian analogue of Koley–Bera 2024).
sem_model = SEM(y=y, X=X, W=g)
sem_model.fit(draws=2500, tune=2500, chains=2, random_seed=42)
# Non-robust Bayesian LM tests evaluated at the appropriate null model.
non_robust_results = pd.DataFrame(
[
bayesian_lm_lag_test(ols_model).to_series(),
bayesian_lm_error_test(ols_model).to_series(),
bayesian_lm_wx_test(sar_model).to_series(),
bayesian_lm_wx_sem_test(sem_model).to_series(),
bayesian_lm_sdm_joint_test(ols_model).to_series(),
bayesian_lm_slx_error_joint_test(ols_model).to_series(),
],
index=[
"LM-Lag",
"LM-Error",
"LM-WX",
"LM-WX-SEM",
"LM-SDM Joint",
"LM-SLX-Error Joint",
],
)
non_robust_results
Gibbs sampling (sem): 2 chains for 2,500 tune and 2,500 draw iterations (2 x 5,000 = 10,000 draws total)
Sampling took 2s (5,360 draws/s)
| lm_samples | statistic | mean | median | credible_interval | bayes_pvalue | test_type | df | n_draws | k_wx | |
|---|---|---|---|---|---|---|---|---|---|---|
| LM-Lag | [0.589248, 0.787757, 0.565717, 0.886587, 0.481... | 0.646624 | 0.735179 | 0.683626 | (0.272821, 1.508105) | 0.421322 | bayesian_lm_lag | 1 | 20000 | NaN |
| LM-Error | [0.680438, 1.007145, 0.653904, 0.245509, 0.482... | 0.871849 | 0.763176 | 0.685345 | (0.012954, 2.030314) | 0.350444 | bayesian_lm_error | 1 | 20000 | NaN |
| LM-WX | [1.074226, 1.002423, 1.169347, 0.889062, 0.927... | 0.809567 | 0.914688 | 0.848448 | (0.324785, 1.886486) | 0.667121 | bayesian_lm_wx | 2 | 20000 | 2.0 |
| LM-WX-SEM | [0.378774, 0.390767, 0.366606, 0.56892, 0.6279... | 0.261173 | 0.412887 | 0.294577 | (0.026641, 1.550952) | 0.877581 | bayesian_lm_wx_sem | 2 | 5000 | 2.0 |
| LM-SDM Joint | [1.260838, 1.689576, 1.197356, 1.849351, 1.053... | 1.367613 | 1.554619 | 1.449644 | (0.593721, 3.14178) | 0.713144 | bayesian_lm_sdm_joint | 3 | 20000 | 2.0 |
| LM-SLX-Error Joint | [1.156957, 1.650866, 1.089115, 0.880268, 0.909... | 1.367613 | 1.323366 | 1.201304 | (0.35573, 3.015725) | 0.713144 | bayesian_lm_slx_error_joint | 3 | 20000 | 2.0 |
# Robust Bayesian LM tests (Neyman orthogonal score)
robust_results = pd.DataFrame(
[
bayesian_robust_lm_lag_test(ols_model).to_series(),
bayesian_robust_lm_error_test(ols_model).to_series(),
bayesian_robust_lm_lag_sdm_test(slx_model).to_series(),
bayesian_robust_lm_wx_test(sar_model).to_series(),
bayesian_robust_lm_error_sdem_test(slx_model).to_series(),
],
index=[
"Robust LM-Lag",
"Robust LM-Error",
"Robust LM-Lag-SDM",
"Robust LM-WX",
"Robust LM-Error-SDEM",
],
)
robust_results
| lm_samples | statistic | mean | median | credible_interval | bayes_pvalue | test_type | df | n_draws | k_wx | |
|---|---|---|---|---|---|---|---|---|---|---|
| Robust LM-Lag | [0.072691, 0.069424, 0.069586, 0.690528, 0.085... | 0.046219 | 0.253013 | 0.107955 | (0.004055, 1.44902) | 0.829778 | bayesian_robust_lm_lag | 1 | 20000 | NaN |
| Robust LM-Error | [0.163881, 0.288812, 0.157773, 0.04945, 0.0870... | 0.271444 | 0.281010 | 0.183404 | (0.000746, 1.103859) | 0.602365 | bayesian_robust_lm_error | 1 | 20000 | NaN |
| Robust LM-Lag-SDM | [3.95244, 35.651953, 1442.885937, 1133.867281,... | 1.901533 | 113.260850 | 33.307467 | (0.087517, 719.588249) | 0.167907 | bayesian_robust_lm_lag_sdm | 1 | 20000 | 2.0 |
| Robust LM-WX | [71.871622, 58.808362, 19.686134, 0.488989, 0.... | 0.406496 | 28.802283 | 5.970204 | (0.333868, 203.094819) | 0.816076 | bayesian_robust_lm_wx | 2 | 20000 | 2.0 |
| Robust LM-Error-SDEM | [3.816173, 35.318175, 1443.350458, 1133.212866... | 1.792893 | 112.988795 | 33.024422 | (0.085791, 719.541654) | 0.180574 | bayesian_robust_lm_error_sdem | 1 | 20000 | 2.0 |
Classical spreg statistics on the same data¶
# Classical spreg LM tests
ols_spreg = SpregOLS(y, X)
lm_spreg = SpregLMtests(ols_spreg, w_spreg)
spreg_results = {
"LM-Lag": lm_spreg.lml,
"LM-Error": lm_spreg.lme,
"LM-WX": lm_spreg.lmwx,
"LM-SDM Joint": lm_spreg.lmspdurbin,
"LM-SLX-Error Joint": lm_spreg.lmslxerr,
"Robust LM-Lag": lm_spreg.rlml,
"Robust LM-Error": lm_spreg.rlme,
"Robust LM-Lag-SDM": lm_spreg.rlmdurlag,
"Robust LM-WX": lm_spreg.rlmwx,
}
all_results = pd.concat([non_robust_results, robust_results])
comparison_rows = []
for bname in all_results.index:
row = all_results.loc[bname]
if bname in spreg_results:
s_stat, s_pval = spreg_results[bname]
else:
s_stat, s_pval = np.nan, np.nan
comparison_rows.append(
{
"bayes_mean": row["mean"],
"spreg_stat": s_stat,
"bayes_pvalue": row["bayes_pvalue"],
"spreg_pvalue": s_pval,
}
)
comparison_df = pd.DataFrame(comparison_rows, index=all_results.index)
comparison_df
/home/runner/micromamba/envs/test/lib/python3.14/site-packages/spreg/diagnostics.py:620: ComplexWarning: Casting complex values to real discards the imaginary part
ci_result = sqrt(max_eigval / min_eigval)
| bayes_mean | spreg_stat | bayes_pvalue | spreg_pvalue | |
|---|---|---|---|---|
| LM-Lag | 0.735179 | 0.886074 | 0.421322 | 0.346543 |
| LM-Error | 0.763176 | 1.341625 | 0.350444 | 0.246748 |
| LM-WX | 0.914688 | 0.616118 | 0.667121 | 0.734872 |
| LM-WX-SEM | 0.412887 | NaN | 0.877581 | NaN |
| LM-SDM Joint | 1.554619 | 1.957743 | 0.713144 | 0.581224 |
| LM-SLX-Error Joint | 1.323366 | 1.957743 | 0.713144 | 0.581224 |
| Robust LM-Lag | 0.253013 | 0.057811 | 0.829778 | 0.809990 |
| Robust LM-Error | 0.281010 | 0.513362 | 0.602365 | 0.473687 |
| Robust LM-Lag-SDM | 113.260850 | 1.341625 | 0.167907 | 0.246748 |
| Robust LM-WX | 28.802283 | 1.071669 | 0.816076 | 0.585181 |
| Robust LM-Error-SDEM | 112.988795 | NaN | 0.180574 | NaN |
The divergence in the error and WX tests is expected because the Bayesian LM statistics use the information matrix (not \(E[gg']\)), which gives different variance estimates than the classical approach. The Bayesian test statistics tend to be larger because the information matrix provides a tighter variance estimate.