neighbayes.diagnostics.spatial_kfold¶
-
neighbayes.diagnostics.spatial_kfold(model, *, splitter=
None, fold_ids=None, n_blocks=10, geometry=None, draws=400, tune=400, chains=2, random_seed=0, progressbar=True, verbose=False, **fit_kwargs)[source]¶ Spatial block cross-validation for a fitted Bayesian spatial model.
Refits the model on each training fold and evaluates the conditional Gaussian predictive density of the held-out fold under the full-data joint implied by the model.
- Parameters:¶
- model : SpatialModel¶
A model from
neighbayes.models. The model must have been constructed (its_X,_yand_W_sparsewill be used for prediction); it does not need to be fit, since fold-specific refits are performed internally.- splitter : sklearn-compatible splitter, optional¶
Any object exposing
split(X)that yields(train_idx, test_idx)pairs (sklearnBaseCrossValidatorprotocol). This is the recommended entry point for using geovalidate splitters such asHilbertKFold,CellStratifiedKFold,LeaveClusterOut, orBallKFold.geometry(when provided) is forwarded as theXargument tosplit; this suffices for geometry-aware geovalidate splitters. Mutually exclusive withfold_ids.- fold_ids : np.ndarray, optional¶
Integer fold assignment per observation, shape
(n,). When supplied,n_blocksandgeometryare ignored.- n_blocks : int, default 10¶
Number of spatial blocks for the KMeans fallback when neither
splitternorfold_idsis provided.- geometry : geopandas.GeoSeries, optional¶
Geometry used by the KMeans fallback to cluster centroids, and forwarded to
splitter.splitwhensplitteris supplied. Required for the KMeans fallback.- draws=
400¶ Forwarded to
SpatialModel.fit()for each per-fold refit. Defaults are deliberately modest to keep CV affordable.- tune=
400¶ Forwarded to
SpatialModel.fit()for each per-fold refit. Defaults are deliberately modest to keep CV affordable.- chains=
2¶ Forwarded to
SpatialModel.fit()for each per-fold refit. Defaults are deliberately modest to keep CV affordable.- random_seed=
0¶ Forwarded to
SpatialModel.fit()for each per-fold refit. Defaults are deliberately modest to keep CV affordable.- progressbar : bool, default True¶
If True, display a fold-level progress bar (via
tqdm) showing CV progress. Independent of any per-chain progress bar insideSpatialModel.fit(), which is always disabled.- verbose : bool, default False¶
If True, allow per-fold
fitcalls to print their usual sampler / compile messages to stdout/stderr. When False (the default) those messages — along with PyMC’sINFOlogger output and warnings — are suppressed so only the fold-level progress bar is visible.- **fit_kwargs¶
Extra keyword arguments forwarded to
SpatialModel.fit().
- Return type:¶
Notes
When
splitterproduces folds whose test sets do not form a disjoint partition of the data (e.g.LeaveBallOutwith an exclusion buffer, or any splitter where some observations are tested multiple times or not at all), the per-observation accounting used to estimateseis undefined. In that caseseis set tonan;elpd_per_foldandelpdremain valid.Notes
Computation is \(O(K \cdot G \cdot \text{nnz}(W))\) per fold plus the cost of refitting; spatial folds are typically a handful (e.g.
n_blocks=5–10). ForOLS/SLXthe predictive collapses to the standard independent Gaussian and the per-fold cost is \(O(G \cdot n_{\text{test}} \cdot k)\).