Mahalanobis distance spec (with one-time covariance setup)
Source:R/dist_mahal.R
distance_mahalanobis.RdReturns a distance spec that estimates the sample covariance from the
training matrix X in setup(X), then evaluates
$$d(X_i, x_0) = \sqrt{(X_i - x_0)^\top \hat\Sigma^{-1}(X_i - x_0)}$$
in eval() / eval_batch(). Requires at least two covariates.
Value
A distance spec (list with name, setup, eval, eval_batch)
for use with cate_dnn(), weights_dnn(), or register_distance().
The string alias "mahalanobis" is registered at package load.
Details
The covariance inverse is computed once in setup() and reused for all
evaluations via the ctx argument, following the same convention as
distance_l2_scaled(). When the sample covariance is (near-)singular a
pseudoinverse via SVD is used automatically; no external packages are
required.
Particularly useful for DGPs with correlated covariates (e.g.\
"dgp5_correlated") where L2 or diagonal-scaled L2 distances fail to
adapt to the off-diagonal covariance structure.