Skip to content

Fix WMA weighted_mean NaN handling#2263

Open
Whning0513 wants to merge 1 commit into
microsoft:mainfrom
Whning0513:fix-wma-nan-1993
Open

Fix WMA weighted_mean NaN handling#2263
Whning0513 wants to merge 1 commit into
microsoft:mainfrom
Whning0513:fix-wma-nan-1993

Conversation

@Whning0513

Copy link
Copy Markdown

Fix WMA weighted_mean NaN handling

The weighted_mean function in WMA._load_internal uses np.nanmean(w * x),
which produces incorrect results when NaN values are present in the input.
The problem is that np.nanmean renormalizes the product (w * x) by counting
how many non-NaN products there are, but it doesn't renormalize the weights
themselves — so the weight denominator still sums over all elements including
the NaN positions, leading to a biased result.

For example, with x = [1, NaN, 3] and w = [1/6, 2/6, 3/6]:
  Old: np.nanmean(w * x) = (1/6 + 9/6) / 2 = 0.833
  Correct weighted avg: (1*1/4 + 3*3/4) = 2.5

The fix filters out NaN positions from both x and w before computing the
weighted sum, then renormalizes w so it sums to 1 over the valid elements.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant