Skip to content

ak.moment applies weights incorrectly for n≥2 (weights exponentiated) #3722

@T90REAL

Description

@T90REAL

Version of Awkward Array

2.8.10

Description and code to reproduce

Calling ak.moment with a weight array produces inflated results starting at n=2. The documentation states the formula. For weighted data this should evaluate to Σ weight * x**n / Σ weight, matching the usual definition of raw moments and aligning with the implementation of ak.var. However, the current implementation computes (x * weight) ** n before the reduction, effectively raising each weight to the nth power. This produces incorrect results whenever weights differ from 1.

import awkward as ak

data = ak.Array([1.0, 2.0])
weight = ak.Array([2.0, 3.0])

# Expected second raw moment: (2*1.0**2 + 3*2.0**2) / (2 + 3) = 14 / 5 = 2.8
expected = (2 * 1.0**2 + 3 * 2.0**2) / (2 + 3)

observed = ak.moment(data, 2, weight=weight, axis=None)

print("expected", expected)
print("observed", observed)
expected 2.8
observed 8.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThe problem described is something that must be fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions