Skip to content

[MRG] Fix paired Gaussian distances for unequal batch sizes - #909

Open
DiadkoShmek wants to merge 3 commits into
PythonOT:masterfrom
DiadkoShmek:lezo3-gaussian-paired-batches
Open

DiadkoShmek wants to merge 3 commits into
PythonOT:masterfrom
DiadkoShmek:lezo3-gaussian-paired-batches

Conversation

@DiadkoShmek

Copy link
Copy Markdown

Types of changes

Bug fix.

Motivation and context / Related issue

ot.gaussian.bures_wasserstein_distance(..., paired=True) documents paired distances when the two batches have the same size, and crossed distances otherwise. The mean term checks tensor ranks rather than batch sizes, while the covariance term checks batch sizes. With two source Gaussians and one target, these terms silently broadcast to a 2×2 result instead of the required 2×1 matrix. With batches of size two and three, the function raises a broadcasting error.

Compare batch sizes for the mean term as well. The diagonal-Gaussian regression uses the independent closed form W2² = ||ms-mt||² + ||stds-stdt||², including equal-batch and explicit-cross controls, both log modes, float32/64, and input immutability. A release note records the corrected behavior.

This change was prepared with AI assistance.

How has this been tested (if it applies)

Exact source checkout at 0b6f8d506950be47f072a4cba439cd1836ba038d, full native installation including Cython/C++ extensions; CPU/NumPy only:

  • python -m pytest test/test_gaussian.py -k unequal_batches -q: unmodified implementation with the same regression: 12 failed, 28 passed; patched: 40 passed.
  • python -m pytest test/test_gaussian.py -q: 74 passed; one existing fixed-point barycenter nonconvergence warning.
  • Additional independent closed-form oracle under shared random orthogonal rotations: 200 cases passed, covering source/target batch sizes 1–5, float32/64, paired/crossed output and both log modes.
  • Ruff 0.5.2 lint and format checks on the changed Python files, and git diff --check: passed.

The complete repository test suite and optional Torch/JAX/TensorFlow/CuPy backends were not run.

PR checklist

  • I have read the CONTRIBUTING document.
  • The documentation is up-to-date with the changes I made (check build artifacts).
  • All tests passed, and additional code has been covered with new tests.
  • I have added the PR and Issue fix to the RELEASES.md file.

The existing API docstring already describes this fallback correctly. No documentation build was run. The test results and remaining backend coverage are listed above; the full repository-suite checkbox is intentionally unchecked.

@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.09%. Comparing base (4cb2575) to head (cc9aa65).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #909   +/-   ##
=======================================
  Coverage   97.09%   97.09%           
=======================================
  Files         130      130           
  Lines       27273    27300   +27     
=======================================
+ Hits        26481    26508   +27     
  Misses        792      792           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@clbonet clbonet left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fix, it seems good to me!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants