API reference¶
The public API of bayespecon is everything reachable from the
top-level bayespecon namespace (model classes, samplers, DGPs,
diagnostics, graph helpers). Internal subpackages are prefixed with
an underscore (_logdet, _ops, _backends, _jax_dispatch,
_numba_dispatch); they are documented here for reference but their
APIs are not covered by the package’s stability guarantees and may
change without deprecation.
Base Classes¶
|
Base class for Bayesian spatial regression models. |
|
Base class for static spatial panel models with FE transforms. |
Cross-Sectional Spatial Models¶
|
Bayesian ordinary least squares cross-sectional regression. |
|
Bayesian Spatial Autoregressive (Spatial Lag) model. |
|
Bayesian Spatial Error Model. |
|
Bayesian SLX (Spatial Lag X) model. |
|
Bayesian Spatial Durbin Model. |
|
Bayesian Spatial Durbin Error Model. |
Panel Spatial Models (Fixed Effects)¶
|
Bayesian pooled and fixed-effects linear panel regression. |
|
Bayesian spatial-lag panel regression. |
|
Bayesian spatial-error panel regression. |
|
Bayesian spatial Durbin panel regression. |
|
Bayesian spatial Durbin error panel regression. |
|
Bayesian SLX panel regression. |
Panel Spatial Models (Random Effects)¶
|
Bayesian random effects panel regression (non-spatial). |
|
Bayesian spatial lag panel model with unit random effects. |
|
Bayesian spatial error panel model with unit random effects. |
|
Bayesian spatial Durbin error panel model with unit random effects. |
Dynamic Panel Spatial Models¶
|
|
|
|
|
|
|
|
|
|
|
|
|
Non-Linear Spatial Models¶
|
Bayesian spatial probit with regional random effects. |
|
Bayesian spatial autoregressive Tobit model. |
|
Bayesian spatial error Tobit model. |
|
Bayesian spatial Durbin Tobit model. |
|
Bayesian SAR model with a Negative Binomial likelihood. |
|
Bayesian structural-form SAR-NB with Pólya–Gamma Gibbs sampler. |
Panel Spatial Models (Tobit)¶
|
|
|
Flow Models¶
|
Abstract base class for Bayesian spatial flow regression models. |
|
Non-spatial Bayesian OD-flow gravity model (independence baseline). |
|
Non-spatial Bayesian OD-flow Poisson gravity model (count baseline). |
|
Bayesian SAR flow model with three free spatial autoregressive parameters. |
|
Bayesian separable SAR flow model with ρ_w = −ρ_d · ρ_o. |
|
Bayesian SAR flow model with a Poisson observation distribution. |
|
Bayesian separable SAR flow model with a Poisson observation distribution. |
|
Aspatial OD-flow Negative Binomial gravity baseline. |
|
Bayesian SAR flow model with NB2 observation noise. |
|
Separable SAR flow model with NB2 observation noise. |
|
Bayesian structural-form SAR-NB flow model with Pólya–Gamma Gibbs sampler. |
|
Bayesian separable SAR-NB flow model with Pólya–Gamma Gibbs sampler. |
|
Bayesian spatial-error flow model with three free spatial parameters. |
|
Bayesian separable spatial-error flow model with \(\lambda_w = -\lambda_d \lambda_o\). |
Panel Flow Models¶
|
Abstract base class for balanced panel spatial flow models. |
|
Non-spatial Bayesian OD-flow gravity model for balanced panel data. |
|
Non-spatial Bayesian OD-flow Poisson gravity model for balanced panel data. |
|
Panel spatial-lag origin-destination flow model with unrestricted dependence. |
|
Panel separable spatial-lag flow model with \(\rho_w = -\rho_d \rho_o\). |
|
Panel Poisson spatial-lag flow model with unrestricted dependence. |
|
Panel separable Poisson spatial-lag flow model. |
|
Aspatial panel OD-flow NB2 gravity baseline. |
|
Panel NB2 SAR flow model with unrestricted dependence parameters. |
|
Panel separable NB2 SAR flow model. |
|
Panel spatial-error flow model with three free spatial parameters. |
|
Panel separable spatial-error flow model with \(\lambda_w = -\lambda_d \lambda_o\). |
Bayesian Diagnostics¶
|
Container for Bayesian LM test results. |
|
Bayesian LM test for omitted spatial lag (SAR) model. |
|
Bayesian LM test for omitted spatial error (SEM) model. |
|
Bayesian LM test for WX coefficients (H₀: γ = 0 | SAR). |
|
Bayesian joint LM test for SDM (H₀: ρ = 0 AND γ = 0 | OLS). |
Bayesian joint LM test for SDEM (H₀: λ = 0 AND γ = 0 | OLS). |
|
Bayesian LM-Error test from a SAR posterior (H₀: λ = 0 | SAR). |
|
|
Bayesian LM-Error test from an SDM posterior (H₀: λ = 0 | SDM). |
|
Bayesian LM-Lag test from an SDEM posterior (H₀: ρ = 0 | SDEM). |
|
Bayesian LM test for WX coefficients in SEM (H₀: γ = 0 | SEM). |
|
Bayesian robust LM-Lag test (H₀: ρ = 0, robust to local λ). |
Bayesian robust LM-Error test (H₀: λ = 0, robust to local ρ). |
|
Bayesian robust LM-Lag test in SDM context (H₀: ρ = 0, robust to γ). |
|
|
Bayesian robust LM-WX test (H₀: γ = 0, robust to ρ). |
Bayesian robust LM-Error test in SDEM context (H₀: λ = 0, robust to γ). |
|
Bayesian robust LM-Error test in SAR context (H₀: λ = 0 | SAR). |
|
Bayesian robust LM-Error test in SDM context (H₀: λ = 0 | SDM). |
|
Bayesian robust LM-Lag test in SDEM context (H₀: ρ = 0 | SDEM). |
|
Bayesian robust LM-Lag test in SEM context (H₀: ρ = 0 | SEM). |
|
Bayesian robust LM-WX test in SEM context (H₀: γ = 0 | SEM). |
Panel Bayesian LM Tests¶
|
Bayesian panel LM test for omitted spatial lag (H₀: ρ = 0). |
|
Bayesian panel LM test for omitted spatial error (H₀: λ = 0). |
Bayesian panel robust LM-Lag test (H₀: ρ = 0, robust to λ). |
|
Bayesian panel robust LM-Error test (H₀: λ = 0, robust to ρ). |
|
|
Bayesian panel LM test for WX coefficients (H₀: γ = 0). |
Bayesian panel joint LM test for SDM (H₀: ρ = 0 AND γ = 0). |
|
Bayesian panel joint LM test for SDEM (H₀: λ = 0 AND γ = 0). |
|
Panel LM-Error test from an SDM panel posterior (H₀: λ = 0 | SDM). |
|
Panel LM-Lag test from an SDEM panel posterior (H₀: ρ = 0 | SDEM). |
|
Bayesian panel LM test for WX coefficients in SEM (H₀: γ = 0 | SEM). |
|
Bayesian panel robust LM-Lag in SDM context (H₀: ρ = 0 | SLX panel). |
|
Bayesian panel robust LM-WX (H₀: γ = 0 | SAR panel, robust to ρ). |
|
Bayesian panel robust LM-Error in SDEM context (H₀: λ = 0 | SLX panel). |
Flow Bayesian LM Tests¶
|
Bayesian LM test for an omitted destination-side spatial lag. |
|
Bayesian LM test for an omitted origin-side spatial lag (\(H_0\colon \rho_o = 0\)). |
Bayesian LM test for an omitted network spatial lag (\(H_0\colon \rho_w = 0\)). |
|
|
Bayesian LM (WX-style) test for the intra block in an OLSFlow null. |
|
Joint Bayesian LM test for the SARFlow filter (\(H_0\colon \rho_d = \rho_o = \rho_w = 0\)). |
|
Robust Bayesian LM test for \(\rho_d\) adjusting for \((\rho_o, \rho_w)\) nuisance via the Neyman-orthogonal score (Bera and Yoon [1993], Anselin et al. [1996], Doğan et al. [2021]). |
|
Robust Bayesian LM test for \(\rho_o\) adjusting for \((\rho_d, \rho_w)\) nuisance. |
Robust Bayesian LM test for \(\rho_w\) adjusting for \((\rho_d, \rho_o)\) nuisance. |
|
Panel analogue of |
|
Panel analogue of |
|
Panel analogue of |
|
Panel analogue of |
|
Panel analogue of |
Diagnostic Test Suites¶
Pre-bundled collections of LM tests used by model.spatial_diagnostics()
and the decision-tree renderers.
|
Named, immutable registry of Bayesian LM specification tests. |
|
Return the |
Named, immutable registry of Bayesian LM specification tests. |
|
Named, immutable registry of Bayesian LM specification tests. |
|
Named, immutable registry of Bayesian LM specification tests. |
|
Named, immutable registry of Bayesian LM specification tests. |
|
Named, immutable registry of Bayesian LM specification tests. |
|
Named, immutable registry of Bayesian LM specification tests. |
|
Named, immutable registry of Bayesian LM specification tests. |
|
Named, immutable registry of Bayesian LM specification tests. |
|
Named, immutable registry of Bayesian LM specification tests. |
|
Named, immutable registry of Bayesian LM specification tests. |
|
Named, immutable registry of Bayesian LM specification tests. |
|
Named, immutable registry of Bayesian LM specification tests. |
|
Named, immutable registry of Bayesian LM specification tests. |
|
Named, immutable registry of Bayesian LM specification tests. |
|
Named, immutable registry of Bayesian LM specification tests. |
|
Named, immutable registry of Bayesian LM specification tests. |
|
Named, immutable registry of Bayesian LM specification tests. |
|
Named, immutable registry of Bayesian LM specification tests. |
|
Named, immutable registry of Bayesian LM specification tests. |
|
Named, immutable registry of Bayesian LM specification tests. |
Bayesian Model Comparison¶
|
Compare a collection of fitted Bayesian models. |
|
Compute all pairwise Bayes factors for a set of Bayesian models. |
|
Convert BIC values to Bayes factors via the BIC-approximation method. |
|
Compile a PyMC model's log-posterior into a callable for bridge sampling. |
|
Compute posterior model probabilities from marginal likelihoods. |
MCMC Efficiency¶
|
Summary of MCMC sampling-efficiency checks for a spatial model. |
|
Run MCMC adequacy checks on a fitted Bayesian spatial model. |
Spatial Cross-Validation¶
|
Result of |
|
Spatial block cross-validation for a fitted Bayesian spatial model. |
Data Generating Processes¶
Note
The cross-sectional and (scalar) panel DGP simulators accept W
(Graph/sparse/dense) and gdf inputs. You may provide both
together; in that case W is used for simulation and is checked
against gdf for dimensional compatibility (a ValueError is
raised when they do not describe the same number of spatial units).
The flow DGPs below take G (libpysal Graph), gdf, n, and
knn_k instead. All four are optional: when none is supplied the
DGP synthesises a point grid via
synth_point_geodataframe() and builds a
row-standardised KNN graph automatically.
|
Simulate data from a non-spatial OLS DGP |
|
Simulate data from SAR DGP |
|
Simulate data from SEM DGP |
|
Simulate data from SLX DGP |
|
Simulate data from SDM DGP |
|
Simulate data from SDEM DGP |
|
Simulate data from a SAR-NB2 DGP. |
|
Simulate SpatialProbit-style binary outcome data. |
|
Simulate left-censored SAR Tobit data. |
|
Simulate left-censored SEM Tobit data. |
|
Simulate left-censored SDM Tobit data. |
|
Simulate pooled data compatible with OLSPanelFE model assumptions. |
|
Simulate SAR panel data in time-first stacking order. |
|
Simulate SEM panel data in time-first stacking order. |
|
Simulate SDM panel FE data. |
|
Simulate SDEM panel FE data. |
|
Simulate SLX panel FE data. |
|
Simulate OLS random-effects panel style data. |
|
Simulate SAR random-effects panel style data. |
|
Simulate SEM random-effects panel style data. |
|
Simulate dynamic non-spatial panel FE data. |
|
Simulate dynamic restricted SDM panel FE data. |
|
Simulate dynamic unrestricted SDM panel FE data. |
|
Simulate dynamic SAR panel FE data. |
|
Simulate dynamic SEM panel FE data. |
|
Simulate dynamic SDEM panel FE data. |
|
Simulate dynamic SLX panel FE data. |
|
Simulate left-censored panel SAR FE data. |
|
Simulate left-censored panel SEM FE data. |
Flow Data Generating Processes¶
Each flow DGP accepts n, G, gdf, and knn_k as optional
arguments and (unless gamma_dist=0.0) appends a log_distance
column log(1 + d_{ij}) with default coefficient gamma_dist=-0.5.
The Gaussian flow DGPs (generate_flow_data,
generate_panel_flow_data and their separable variants) default to
distribution="lognormal", returning strictly-positive flows
y = exp(eta) where eta is the latent SAR-filtered linear
predictor (also exposed in the result dict as "eta_vec" /
"eta"). Pass distribution="normal" to recover the legacy
Gaussian-on-y behaviour. The Gaussian-likelihood flow models in
bayespecon.models.flow operate on the latent scale, so fit on
np.log(y) to recover the SAR parameters. The Poisson and Negative
Binomial DGPs are unchanged.
|
Simulate flow data from a SAR flow model. |
|
Simulate flow data from a separable SAR flow model. |
|
Generate synthetic origin-destination flow count data for a Poisson spatial autoregressive flow model. |
|
Simulate Poisson flow count data from a separable SAR flow model. |
|
Generate synthetic O-D flow counts from an NB2 SAR flow DGP. |
|
NB2 flow DGP with separability constraint |
|
Simulate flow data from a spatial-error (SEM) flow model. |
|
Simulate SEM flow data with the separability constraint \(\lambda_w = -\lambda_d \lambda_o\). |
|
Simulate panel flow data from a SAR flow model with unit effects. |
|
Simulate panel flow data from a separable SAR flow model. |
|
Simulate panel Poisson flow data from a spatial autoregressive model. |
Simulate panel Poisson flow data from a separable SAR flow model. |
|
|
Generate panel NB2 flow counts from a spatial autoregressive DGP. |
Panel NB2 flow DGP with separability constraint |
|
|
Simulate panel SEM flow data with O-D-pair random effects. |
|
Panel SEM flow DGP with separability constraint \(\lambda_w = -\lambda_d \lambda_o\). |
Graph Utilities¶
|
Combined design matrix for an O-D flow regression. |
|
Build a flow regression design matrix from regional attribute data. |
|
Build a flow design matrix with different numbers of dest/origin variables. |
|
Build a flow design matrix with separate destination and origin data. |
Build all three N×N flow weight matrices from a single Graph. |
|
Build the N×N destination weight matrix \(W_d = I_n \otimes W\). |
|
Build the N×N origin weight matrix \(W_o = W \otimes I_n\). |
|
Build the N×N network weight matrix \(W_w = W \otimes W\). |
Gibbs Samplers¶
Block-Gibbs samplers for Gaussian spatial models. These bypass NUTS
entirely and exploit conditional conjugacy for faster sampling. All
four symbols are re-exported from the top-level bayespecon
namespace.
|
Base class for Gaussian spatial Gibbs sampler configuration and execution. |
|
Gibbs sampler for SAR/SDM Gaussian models. |
|
Gibbs sampler for SEM/SDEM Gaussian models. |
|
Prior hyperparameters for Gaussian spatial Gibbs. |
Configuration¶
|
Enable persistent on-disk compile caches for JAX and PyTensor. |
Internal Modules¶
The following subpackages are private (underscore-prefixed). They back the public model classes and samplers and may change without deprecation; they are documented here for reference and for users writing custom extensions.
Custom PyTensor Ops (bayespecon._ops)¶
Differentiable sparse and Kronecker-structured linear solves with adjoint-method gradients, used inside the flow-model PyMC graphs.
|
Differentiable sparse solve \(\eta = A(\rho)^{-1} b\). |
|
Differentiable sparse solve \(H = A(\rho)^{-1} B\) for matrix RHS. |
|
Differentiable Kronecker-factored solve for separable Poisson flow models. |
Kronecker-factored solve for separable panel Poisson flow models. |
|
|
Solve \((L_o \otimes L_d)\,\eta = b\) via two \(n \times n\) sparse solves. |
|
Solve \((L_o \otimes L_d)\,H = B\) for a matrix RHS via batched two-step solve. |
Log-Determinant Methods (bayespecon._logdet)¶
Positive-only methods (sparse_spline, grid_mc) auto-restrict
the rho/lambda support to [1e-5, 1.0] when the prior or method
default would otherwise admit negative values. Explicit
rho_min/rho_max overrides still raise.
Method resolution and bound handling¶
|
Canonical names for the log-determinant approximation methods. |
|
Resolved logdet method and rho interval used for approximation. |
|
Validate |
|
Resolve method-specific rho bounds from defaults, priors, or overrides. |
Factories¶
|
Return a function (rho) -> pytensor log|I - rho*W| expression. |
|
Return a pure-numpy |
|
Return a vectorized numpy |
|
Return a JAX-native function (rho) -> log|I - rho*W|. |
|
Return a shared cached |
Clear the shared cache of precomputed PyTensor logdet callables. |
PyTensor kernels¶
|
Eigenvalue-based log|I - rho*W|. |
|
Exact log|I - rho*W| as a pytensor expression. |
|
Evaluate Chebyshev approximation of log|I - rho*W| symbolically. |
|
Cubic spline interpolation of log|I - rho*W|. |
|
Evaluate Barry-Pace trace polynomial approximation of log|I - rho*W| symbolically. |
JAX kernels¶
|
Evaluate Chebyshev approximation of log|I - rho*W| in JAX. |
|
Evaluate trace-polynomial approximation of log|I - rho*W| in JAX. |
Grid / polynomial primitives¶
|
Compute Monte Carlo log-determinant approximation (Barry and Kelley Pace [1999]). |
|
Compute Chebyshev approximation of log|I - rho*W| ([Pace and LeSage, 2004]). |
|
Compute a spline-interpolated log-determinant grid. |
Flow log-determinants¶
|
Differentiable PyTensor log-determinant for the flow SAR model. |
|
Vectorised numpy port of |
|
Estimate tr(W^k) for k=1..miter via Barry-Pace stochastic traces. |
|
Pre-compute logdet data for separable flow models and return a logdet callable. |
|
Pre-compute numeric logdet data for separable flow models. |
Stochastic trace estimation¶
The XTrace (Epperly–Tropp–Webber 2024) and Hutch++ (Meyer et al. 2021)
estimators are implemented natively in NumPy + SciPy sparse and used
inside the Chebyshev log-determinant path. Selection is via the
trace_estimator= keyword on each model.
Return |
|
|
Estimate tr(W^k) for k=1..order via variance-reduced stochastic trace estimation. |
|
Estimate tr(W^k) for the Chebyshev MC path via traceax. |