Skip to content

Clarify the intended behavior of the l1 metric #2008

Closed

Description

@SahilKumar75

Bug form information

sbi version: 0.27.0
Python version: 3.13.4
Device: CPU
Inference method: Not method specific
Rounds: Not applicable

1. Problem

sbi.utils.metrics.l1() is named after the L1 norm, more commonly known as Manhattan distance. Conventionally, Manhattan distance between two points is the sum of the absolute differences of their coordinates.

The current implementation, however, computes the mean of the absolute differences rather than their sum. The function's docstring is consistent with the code, since it explicitly states that the result is averaged, so there is no internal contradiction between the implementation and its documentation. The conflict is external: the function's name promises standard Manhattan distance, but its behavior delivers mean absolute difference instead.

This mismatch means the function's name and its actual behavior disagree, which can lead callers to assume standard Manhattan distance when the function actually returns something else.

2. Minimal example

import torch
from sbi.utils.metrics import l1

x = torch.tensor([[0.0, 0.0]])
y = torch.tensor([[1.0, 0.0]])

print(l1(x, y))

This currently returns 0.5, whereas the true Manhattan distance between these two points is 1.0.

The averaging behavior also introduces a dimensionality artifact: adding an identical, nondiffering coordinate to both points changes the reported distance, even though no actual difference was introduced.

x1 = torch.tensor([[0.0]])
y1 = torch.tensor([[1.0]])

x2 = torch.tensor([[0.0, 0.0]])
y2 = torch.tensor([[1.0, 0.0]])

print(l1(x1, y1))
print(l1(x2, y2))

The single coordinate difference of 1.0 produces a different result depending on how many identical, nondiffering dimensions are appended. This is not how Manhattan distance should behave, since appending a dimension with no difference should leave the distance unchanged rather than shrink it.

3. Why it matters

A function named l1 sets a reasonable expectation among callers that it returns the sum of absolute differences, matching the standard definition of the L1 norm. Any caller relying on that assumption will get results that differ from the conventional L1 definition, since the averaged output is close in shape but different in scale, and that scale shifts with dimensionality.

At the same time, this project uses l1() in ABC (Approximate Bayesian Computation) distance thresholding, and possibly elsewhere. Any existing code, saved thresholds, or calibrated tolerances built around the current averaged output would be affected by a direct change to summation. This makes the fix less straightforward than a typical bug fix, since it is also a potential breaking change to numerical behavior that downstream users may be depending on, even unknowingly.

4. Proposed solution

My preferred solution is to make l1() compute the conventional Manhattan sum and update its documentation and tests. Since this changes numerical scaling for existing users, I would like maintainer guidance on whether the change should be direct, documented as a compatibility change, introduced gradually, or provided under a separate function.

If a direct change is agreed on, the concrete steps would be:

  1. Change l1() to compute the sum of absolute differences rather than the mean.
  2. Update the docstring to describe Manhattan distance accurately, removing any reference to averaging.
  3. Add a regression test that verifies appending identical coordinates to both inputs does not change the computed distance.
  4. If maintainers consider this a breaking change, add release notes or compatibility documentation flagging the behavior change and its version.

5. Alternatives to ask maintainers about

Before deciding whether to merge the existing change, it's worth confirming which policy maintainers prefer, since this affects backward compatibility:

  1. Change l1() directly to compute the conventional summed Manhattan distance.
  2. Preserve the current function as is, but document it clearly as a mean absolute difference metric rather than Manhattan distance.
  3. Add a separate function for true Manhattan distance, leaving l1() untouched.
  4. Introduce the change gradually, through a deprecation warning or compatibility period before the behavior switches.

6. Testing plan

The most important regression case is confirming that appending identical coordinates to both inputs does not change the computed Manhattan distance, since this is the artifact of the current mean based implementation. This should be tested alongside the existing ordinary reduction cases (single and multiple dimensions, batched tensors, a nondefault reduction axis, and identical inputs returning zero) to confirm the fix does not break standard behavior.

7. Related work

PR #1998 already implements a change from mean to sum. However, that change is still pending agreement from maintainers on how to handle backward compatibility with existing ABC thresholds and other dependent code. This issue should not be considered resolved until maintainers have approved both the target behavior and the compatibility approach.

Activity

  1. SahilKumar75 commented on Sep 10, 2026

    @SahilKumar75
    ContributorAuthor

    @janfb, could you please confirm which compatibility approach you prefer for l1? I can update PR #1998 as soon as the intended behavior is agreed.

  2. janfb commented on Sep 10, 2026

    @janfb
    Contributor

    @janfb, could you please confirm which compatibility approach you prefer for l1? I can update PR #1998 as soon as the intended behavior is agreed.

    Thanks for the issue and plan, I will look into this next week.

  3. janfb commented on Sep 21, 2026

    @janfb
    Contributor

    Good catch, I agree with the approach taken in the PR, just change it to the correct L1 definition without averaging. And clear documentation of this change in the docs and changelog. I will comment in the PR.

  4. janfb commented on Sep 21, 2026

    @janfb
    Contributor

    closed with #1998

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions