mahalanobis_distance#

bayesflow.diagnostics.metrics.mahalanobis_distance(observed_samples: ndarray, reference_samples: ndarray) → ndarray[source]#

Computes the mean Mahalanobis distance of the observed samples to the distribution of the reference samples.

Like maximum_mean_discrepancy(), it can be passed as comparison_fn to bootstrap_comparison() and summary_space_comparison(). The Mahalanobis distance in summary space follows [1]. Trailing dimensions are flattened. The covariance is inverted with the pseudo-inverse, so deviations along directions in which the reference samples do not vary (e.g., constant or collinear dimensions) do not contribute to the distance.

[1] Li, C., Vehtari, A., Bürkner, P.-C., Radev, S. T., Acerbi, L., & Schmitt, M. (2024). Amortized Bayesian workflow. arXiv:2409.04332.

Parameters:
observed_samplesnp.ndarray

Observed samples, shape (num_observed, …).

reference_samplesnp.ndarray

Reference samples used to estimate the mean and covariance, shape (num_reference, …).

Returns:
distancenp.ndarray

A scalar NumPy array containing the mean Mahalanobis distance (not squared) of the observed samples.

Raises:
ValueError

If the shapes of observed and reference samples do not match on dimensions besides the first one.

Examples

>>> distance_observed, distance_null = bf.diagnostics.summary_space_comparison(
...     observed_data, reference_data, estimator, comparison_fn=bf.diagnostics.mahalanobis_distance
... )