spectralbrain.statistics.bayesian#
Bayesian statistical models for spectral morphometry.
Six models with a scikit-learn-like API: .fit(), .predict(),
.score(), .summary(). All models delegate MCMC sampling
to the backends (PyMC NUTS, nutpie, or NumPyro).
Models#
HorseshoeRegression — sparse regression for feature selection.
BayesianGroupComparison — BEST (Kruschke 2013) with HDI + ROPE.
HierarchicalLinearModel — multi-site random effects.
GaussianProcessNormative — GP age-trajectory normative.
BayesianSpatialModel — GMRF vertex-wise spatial prior.
BayesianConnectome — hierarchical connectome comparison.
Examples
>>> model = HorseshoeRegression()
>>> model.fit(descriptors, clinical_scores)
>>> model.summary()
>>> predictions = model.predict(new_descriptors)
>>> model.score() # LOO-CV
Dependencies#
PyMC, ArviZ (optional, lazy-imported).
Classes
|
Hierarchical Bayesian model for connectome comparison. |
|
Bayesian Estimation Supersedes the T-test (BEST). |
Abstract base for all SpectralBrain Bayesian models. |
|
|
Vertex-wise Bayesian model with spatial GMRF prior. |
|
GP-based normative model for age trajectories. |
|
Multi-site hierarchical linear model with random effects. |
|
Sparse Bayesian regression with horseshoe prior. |
- class spectralbrain.statistics.bayesian.BayesianConnectome(shrinkage=1.0)[source]#
Bases:
BayesianModelHierarchical Bayesian model for connectome comparison.
Models each entry of the geometric connectome matrix with a hierarchical prior, testing whether connection strengths differ between groups while sharing information across edges.
- Parameters:
shrinkage (float) – Hierarchical shrinkage strength.
Examples
>>> model = BayesianConnectome() >>> model.fit(connectomes_patients, connectomes_controls) >>> diff = model.edge_difference_posterior()
- edge_difference_posterior()[source]#
Posterior mean of per-edge group difference.
- Returns:
ndarray, shape (n_edges,)
- Return type:
- class spectralbrain.statistics.bayesian.BayesianGroupComparison(rope=(-0.1, 0.1))[source]#
Bases:
BayesianModelBayesian Estimation Supersedes the T-test (BEST).
Estimates the full posterior distribution of group means, standard deviations, effect size, and their differences. Reports HDI (Highest Density Interval) and probability of the difference exceeding a ROPE.
- Parameters:
rope (tuple of float) – Region Of Practical Equivalence. Default (-0.1, 0.1) in units of the pooled standard deviation.
Examples
>>> model = BayesianGroupComparison(rope=(-0.1, 0.1)) >>> model.fit(group_a_values, group_b_values) >>> model.summary() >>> model.effect_size_posterior()
- effect_size_posterior()[source]#
Posterior samples of Cohen’s d.
- Returns:
ndarray, shape (n_samples,)
- Return type:
- fit(group_a, group_b, **kwargs)[source]#
Fit BEST model.
- Parameters:
group_a (ndarray, shape (n_a,) and (n_b,))
group_b (ndarray, shape (n_a,) and (n_b,))
- Return type:
- class spectralbrain.statistics.bayesian.BayesianModel[source]#
Bases:
ABCAbstract base for all SpectralBrain Bayesian models.
Subclasses implement
_build_model()to construct a PyMC model, and optionally override_build_posterior_predictive()for custom prediction logic.- trace_#
Posterior samples (populated after
.fit()).- Type:
arviz.InferenceData or None
- model_#
The PyMC model object.
- Type:
pymc.Model or None
- fit(X, y, *, sampler='auto', draws=2000, tune=1000, chains=4, cores=4, target_accept=0.95, seed=42, **kwargs)[source]#
Fit the model via MCMC sampling.
- Parameters:
X (ndarray, shape (n, d)) – Feature matrix.
y (ndarray, shape (n,)) – Target variable.
sampler (str) –
"auto"tries nutpie → numpyro → nuts.draws (int) – MCMC configuration.
tune (int) – MCMC configuration.
chains (int) – MCMC configuration.
cores (int) – MCMC configuration.
target_accept (float)
seed (int)
**kwargs – Extra arguments passed to the sampler.
- Returns:
self
- Return type:
- class spectralbrain.statistics.bayesian.BayesianSpatialModel(spatial_strength=10.0)[source]#
Bases:
BayesianModelVertex-wise Bayesian model with spatial GMRF prior.
Places a Gaussian Markov Random Field prior on the vertex-level effects, so neighbouring vertices share information. This is Bayesian spatial smoothing — more principled than Gaussian kernel pre-smoothing.
- Parameters:
spatial_strength (float) – Precision of the GMRF prior (higher = more spatial smoothing).
Examples
>>> model = BayesianSpatialModel(spatial_strength=10.0) >>> model.fit(group_labels, vertex_descriptors, ... adjacency=mesh_adjacency)
- fit(group_labels, vertex_data, *, adjacency, **kwargs)[source]#
Fit spatial model.
- Parameters:
group_labels (ndarray, shape (S,)) – Group assignment (0/1) per subject.
vertex_data (ndarray, shape (S, N)) – Per-subject vertex-wise descriptor values.
adjacency (sparse matrix, shape (N, N)) – Mesh or kNN adjacency.
- class spectralbrain.statistics.bayesian.GaussianProcessNormative(kernel='matern52', lengthscale_prior=10.0)[source]#
Bases:
BayesianModelGP-based normative model for age trajectories.
Fits a Gaussian Process over age (or any continuous covariate) to model the normative distribution of a spectral descriptor. Individual deviations are computed as z-scores from the posterior predictive.
- Parameters:
Examples
>>> gp = GaussianProcessNormative(kernel="matern52") >>> gp.fit(ages_controls[:, None], descriptor_controls) >>> z_patient = gp.deviation(age_patient, descriptor_patient)
- class spectralbrain.statistics.bayesian.HierarchicalLinearModel(random_effects='intercept')[source]#
Bases:
BayesianModelMulti-site hierarchical linear model with random effects.
Models spectral descriptors with fixed effects (group, age, sex) and random intercepts/slopes per site, handling batch effects within the model rather than post-hoc harmonisation.
y ~ α + β·X + u_site + ε
- Parameters:
random_effects (str) –
"intercept"— random intercept per site."slope"— random intercept + slope per site.
Examples
>>> model = HierarchicalLinearModel(random_effects="intercept") >>> model.fit(X, y, site_labels=sites)
- class spectralbrain.statistics.bayesian.HorseshoeRegression(tau_prior=0.1)[source]#
Bases:
BayesianModelSparse Bayesian regression with horseshoe prior.
The horseshoe prior (Carvalho, Polson & Scott 2009) provides aggressive shrinkage of irrelevant coefficients toward zero while leaving large effects unshrunk — ideal for selecting which of 20+ spectral descriptors predict a clinical outcome.
- Parameters:
tau_prior (float) – Global shrinkage scale (smaller = more sparse). Rule of thumb: p_eff / (d - p_eff) / sqrt(n), where p_eff is expected number of relevant features.
Examples
>>> model = HorseshoeRegression(tau_prior=0.1) >>> model.fit(descriptors, clinical_scores) >>> model.summary() >>> model.predict(new_descriptors)