sqfa.distances

Distances between Symmetric Positive Definite matrices.

Functions

affine_invariant(A, B)

Compute the affine invariant distance between SPD matrices.

affine_invariant_sq(A, B)

Compute the squared affine invariant distance between SPD matrices.

bhattacharyya(statistics_A, statistics_B)

Compute the Bhattacharyya distance between two Gaussian distributions.

fisher_rao_lower_bound(statistics_A, ...)

Compute the Calvo & Oller lower bound of the Fisher-Rao squared distance between Gaussians.

fisher_rao_lower_bound_sq(statistics_A, ...)

Compute the Calvo & Oller lower bound of the Fisher-Rao squared distance between Gaussians.

fisher_rao_same_cov(statistics_A, statistics_B)

Compute the exact Fisher-Rao distance between two Gaussian distributions with the same covariance matrix.

hellinger(statistics_A, statistics_B)

Compute the Hellinger distance between two Gaussian distributions.

jeffreys(statistics_A, statistics_B)

Compute the Jeffreys divergence between Gaussian distributions.

log_euclidean(A, B)

Compute the log-Euclidean distance between SPD matrices.

log_euclidean_sq(A, B)

Compute the squared log-Euclidean distance between SPD matrices.

mahalanobis(statistics_A, statistics_B)

Compute the Mahalanobis distance between Gaussian distributions, using the mean covariance matrix for each pair.

mahalanobis_sq(statistics_A, statistics_B)

Compute the squared Mahalanobis distance between Gaussian distributions, using the mean covariance matrix for each pair.

wasserstein(statistics_A, statistics_B)

Compute the Wasserstein distance between Gaussian distributions.

wasserstein_sq(statistics_A, statistics_B)

Compute the squared Wasserstein distance between Gaussian distributions.

sqfa.distances.affine_invariant(A, B)

Compute the affine invariant distance between SPD matrices. A small epsilon is added inside the square root to avoid gradient instabilities.

Parameters:
  • A (torch.Tensor) – Shape (n_batch_A, n_dim, n_dim), the first SPD matrix.

  • B (torch.Tensor) – Shape (n_batch_B, n_dim, n_dim), the second SPD matrix.

Returns:

distance – Shape (n_batch_A, n_batch_B), the affine invariant distance.

Return type:

torch.Tensor

sqfa.distances.affine_invariant_sq(A, B)

Compute the squared affine invariant distance between SPD matrices.

Parameters:
  • A (torch.Tensor) – Shape (n_batch_A, n_dim, n_dim), the first SPD matrix.

  • B (torch.Tensor) – Shape (n_batch_B, n_dim, n_dim), the second SPD matrix.

Returns:

distance_squared – Shape (n_batch_A, n_batch_B), the squared affine invariant distance.

Return type:

torch.Tensor

sqfa.distances.bhattacharyya(statistics_A, statistics_B)

Compute the Bhattacharyya distance between two Gaussian distributions.

Parameters:
  • statistics_A (dict) –

    Dictionary containing the means and covariances of the first Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

  • statistics_B (dict) –

    Dictionary containing the means and covariances of the second Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

Returns:

distance – Shape (n_classes, n_classes), the lower bound of the Fisher-Rao distance.

Return type:

torch.Tensor

sqfa.distances.fisher_rao_lower_bound(statistics_A, statistics_B)

Compute the Calvo & Oller lower bound of the Fisher-Rao squared distance between Gaussians.

Parameters:
  • statistics_A (dict) –

    Dictionary containing the means and covariances of the first Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

  • statistics_B (dict) –

    Dictionary containing the means and covariances of the second Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

Returns:

distance – Shape (n_classes, n_classes), the lower bound of the Fisher-Rao distance.

Return type:

torch.Tensor

sqfa.distances.fisher_rao_lower_bound_sq(statistics_A, statistics_B)

Compute the Calvo & Oller lower bound of the Fisher-Rao squared distance between Gaussians.

Parameters:
  • statistics_A (dict) –

    Dictionary containing the means and covariances of the first Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

  • statistics_B (dict) –

    Dictionary containing the means and covariances of the second Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

Returns:

distance_squared – Shape (n_classes, n_classes), the lower bound of the Fisher-Rao squared distance.

Return type:

torch.Tensor

sqfa.distances.fisher_rao_same_cov(statistics_A, statistics_B)

Compute the exact Fisher-Rao distance between two Gaussian distributions with the same covariance matrix. The covariance matrix is taken as the mean of the two covariance matrices.

Parameters:
  • statistics_A (dict) –

    Dictionary containing the means and covariances of the first Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

  • statistics_B (dict) –

    Dictionary containing the means and covariances of the second Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

Returns:

distance – Shape (n_classes, n_classes), the Fisher-Rao distance.

Return type:

torch.Tensor

References

[1]
  1. Nielsen, “A Simple Approximation Method for the Fisher–Rao

Distance between Multivariate Normal Distributions” Entropy, vol. 25, no. 4, pp. 654, 2023.

sqfa.distances.hellinger(statistics_A, statistics_B)

Compute the Hellinger distance between two Gaussian distributions. An epsilon is added inside the square root to avoid gradient instabilities.

Parameters:
  • statistics_A (dict) –

    Dictionary containing the means and covariances of the first Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

  • statistics_B (dict) –

    Dictionary containing the means and covariances of the second Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

Returns:

distance – Shape (n_classes, n_classes), the lower bound of the Fisher-Rao distance.

Return type:

torch.Tensor

sqfa.distances.jeffreys(statistics_A, statistics_B)

Compute the Jeffreys divergence between Gaussian distributions.

Jeffreys divergence is the symmetric KL divergence: KL(A || B) + KL(B || A).

Parameters:
  • statistics_A (dict) –

    Dictionary containing the means and covariances of the first Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

  • statistics_B (dict) –

    Dictionary containing the means and covariances of the second Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

Returns:

divergence – Shape (n_classes, n_classes), the Jeffreys divergence.

Return type:

torch.Tensor

sqfa.distances.log_euclidean(A, B)

Compute the log-Euclidean distance between SPD matrices. A small epsilon is added inside the square root to avoid gradient instabilities.

Parameters:
  • A (torch.Tensor) – Shape (n_batch_A, n_dim, n_dim), the first SPD matrix.

  • B (torch.Tensor) – Shape (n_batch_B, n_dim, n_dim), the second SPD matrix.

Returns:

distance – Shape (n_batch_A, n_batch_B), the log-Euclidean distance.

Return type:

torch.Tensor

sqfa.distances.log_euclidean_sq(A, B)

Compute the squared log-Euclidean distance between SPD matrices.

Parameters:
  • A (torch.Tensor) – Shape (n_batch_A, n_dim, n_dim), the first SPD matrix.

  • B (torch.Tensor) – Shape (n_batch_B, n_dim, n_dim), the second SPD matrix.

Returns:

distance_squared – Shape (n_batch_A, n_batch_B), the squared log-Euclidean distance.

Return type:

torch.Tensor

sqfa.distances.mahalanobis(statistics_A, statistics_B)

Compute the Mahalanobis distance between Gaussian distributions, using the mean covariance matrix for each pair.

Parameters:
  • statistics_A (dict) –

    Dictionary containing the means and covariances of the first Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

  • statistics_B (dict) –

    Dictionary containing the means and covariances of the second Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

Returns:

distance – Shape (n_classes, n_classes), the Mahalanobis distance.

Return type:

torch.Tensor

sqfa.distances.mahalanobis_sq(statistics_A, statistics_B)

Compute the squared Mahalanobis distance between Gaussian distributions, using the mean covariance matrix for each pair.

Parameters:
  • statistics_A (dict) –

    Dictionary containing the means and covariances of the first Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

  • statistics_B (dict) –

    Dictionary containing the means and covariances of the second Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

Returns:

distance_squared – Shape (n_classes, n_classes), the squared Mahalanobis distance.

Return type:

torch.Tensor

sqfa.distances.wasserstein(statistics_A, statistics_B)

Compute the Wasserstein distance between Gaussian distributions. A small epsilon is added inside the square root to avoid gradient instabilities.

Parameters:
  • statistics_A (dict) –

    Dictionary containing the means and covariances of the first Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

  • statistics_B (dict) –

    Dictionary containing the means and covariances of the second Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

Returns:

distance – Shape (n_classes, n_classes), the Wasserstein distance.

Return type:

torch.Tensor

sqfa.distances.wasserstein_sq(statistics_A, statistics_B)

Compute the squared Wasserstein distance between Gaussian distributions.

Parameters:
  • statistics_A (dict) –

    Dictionary containing the means and covariances of the first Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

  • statistics_B (dict) –

    Dictionary containing the means and covariances of the second Gaussian distribution, with keys “means” and “covariances”. - The means are a torch.Tensor of shape (n_classes, n_filters) - The covariances are a torch.Tensor of

    shape (n_classes, n_filters, n_filters).

Returns:

distance_squared – Shape (n_classes, n_classes), the squared Wasserstein distance.

Return type:

torch.Tensor