Skip to content

BUG: mean padding with np.pad may lead to different prepended and appended values #11216

Closed

Description

@adeak

The bug was posted on the mailing list. It boils down to the fact that np.mean with mean padding recomputes the mean from the half-padded array for the append step. Mathematically this would be OK, but in case of finite precision and near-zero means the near-zero prepended values may bias the recomputed mean in a way that prepended and appended values are different. Stripped-down example:

>>> import numpy as np
>>> a = np.array([-1, 2, -1]) + np.array([0, 1e-12, 0], dtype=np.float64)
>>> a = np.pad(a, (1, 1), 'mean')
>>> print(a)
[ 3.33362967e-13 -1.00000000e+00  2.00000000e+00 -1.00000000e+00
  3.33399974e-13]

As you can see, the first and the last values (both of which should be the mean) differ on the order of the machine epsilon. The prepended value is the true mean of the original array, the appended value is the mean where the prepended value is also taken into account. It was decided on the mailing list that this is a bug.

Activity

  1. hmaarrfk commented on Sep 16, 2018

    @hmaarrfk
    Contributor

    @eric-wieser can you chime in on whether or not this is a bug. When developing the "single copy" pad function, I was perplexed as to why the code was written in such a way that made this possible. This would help me write a few tests for: #11358 (comment)

  2. adeak commented on Sep 16, 2018

    @adeak
    ContributorAuthor

    @hmaarrfk if you read the last link in my bug report you'll get your answer.

  3. hmaarrfk commented on Sep 16, 2018

    @hmaarrfk
    Contributor

    of course...

  4. hmaarrfk commented on Sep 16, 2018

    @hmaarrfk
    Contributor

    Force pushing really messes with github. @eric-wieser beat me to the punch by 10 hours:

    xref #11961

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