Skip to content

Strange behaviour of np.average with negative weights #9825

@apbard

Description

@apbard

The current implementation of np.average accepts weights and the only check performed is that the sum of weights is not zero.
This is due to the fact the the result is normalised by the sum of weights.

This however leads to the following strange behaviour when dealing with negative weights:

In [1]: import numpy as np

In [2]: np.average([3,5], weights=[1, -0.99])
Out[2]: -194.99999999999986

In [3]: np.average([3,5], weights=[1, -1.01])
Out[3]: 204.9999999999998

In [4]: np.average([3,5], weights=[0, -1])
Out[4]: 5.0

In [5]: np.average([3,5], weights=[0, +1])
Out[5]: 5.0

wouldn't be better to normalise by the absolute sum or the sum of absolute values? In this way we would get:

# Case absolute sum
In [4]: average([3,5], weights=[0, -1])
Out[4]: -5.0

In [5]: average([3,5], weights=[0, +1])
Out[5]: 5.0

In [6]: average([3,5], weights=[1, -0.99])
Out[6]: -194.99999999999986

In [7]: average([3,5], weights=[1, -1.01])
Out[7]: -204.9999999999998

or

# Case sum of absolute values
In [4]: average([3,5], weights=[0, -1])
Out[4]: -5.0

In [5]: average([3,5], weights=[0, +1])
Out[5]: 5.0

In [6]: average([3,5], weights=[1, -0.99])
Out[6]: -0.97989949748743732

In [7]: average([3,5], weights=[1, -1.01])
Out[7]: -1.0199004975124377

Finally, is there a realistic use case where negative weights are needed?

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