DistanceLoss#

class mlquantify.losses.DistanceLoss(distance='hellinger', normalize=True)[source]#

Generic distance-based loss between two probability distributions.

Computes a symmetric distance measure between a mixture distribution and a target distribution. Both arrays are optionally normalized to valid probability vectors before the distance is evaluated.

Parameters:
distance{‘hellinger’, ‘topsoe’, ‘probsymm’, ‘sqEuclidean’, ‘euclidean’},

default=’hellinger’

The distance measure to apply.

normalizebool, default=True

If True, both mixture and target are passed through normalize_distribution before comparison.

Attributes:
distancestr

Name of the chosen distance measure.

normalizebool

Whether inputs are normalized before computing the distance.

Examples

>>> from mlquantify.losses import get_loss
>>> loss = get_loss("hellinger")
>>> loss([0.4, 0.6], [0.5, 0.5])
0.07612...
__call__(mixture, target)[source]#

Compute the distance between two distributions.

Parameters:
mixturearray-like of shape (n_classes,)

Estimated prevalence or mixture vector.

targetarray-like of shape (n_classes,)

Reference (true) prevalence or target vector.

Returns:
lossfloat

Scalar distance value.

Raises:
ValueError

If mixture and target have different shapes, or if self.distance is not a recognised distance name.

Examples

>>> from mlquantify.losses import get_loss
>>> loss = get_loss("topsoe")
>>> round(loss([0.3, 0.7], [0.5, 0.5]), 4)
0.0528