EnergyLoss#

class mlquantify.losses.EnergyLoss[source]#

Energy-distance loss for distribution matching.

Computes the quadratic form

\[\mathcal{L}(\hat{p}) = \hat{p}^\top (2\,q - M\,\hat{p})\]

where \(\hat{p}\) is the estimated prevalence vector, \(q\) is a pre-computed cross-term vector, and \(M\) is the pairwise energy-distance matrix between training classes. Minimising this objective is equivalent to minimising the energy distance between the mixture distribution and the test distribution.

Examples

>>> import numpy as np
>>> from mlquantify.losses import get_loss
>>> loss = get_loss("energy")
>>> p = np.array([0.4, 0.6])
>>> q = np.array([0.3, 0.7])
>>> M = np.eye(2)
>>> loss(p, q, M)
0.04
__call__(prevalence, q, M)[source]#

Compute the energy-distance loss.

Parameters:
prevalencearray-like of shape (n_classes,)

Estimated class prevalence vector.

qarray-like of shape (n_classes,)

Cross-term vector equal to the mean energy distance between each training class and the test sample.

Marray-like of shape (n_classes, n_classes)

Pairwise energy-distance matrix between training classes.

Returns:
lossfloat

Scalar energy-distance loss value.

Examples

>>> import numpy as np
>>> from mlquantify.losses import get_loss
>>> loss = get_loss("energy_distance")
>>> p = np.array([0.5, 0.5])
>>> q = np.array([0.4, 0.6])
>>> M = np.array([[0.0, 1.0], [1.0, 0.0]])
>>> loss(p, q, M)
0.5