Skip to content

fix: compute WMA as weighted sum#2250

Open
he-yufeng wants to merge 1 commit into
microsoft:mainfrom
he-yufeng:fix/wma-weighted-sum
Open

fix: compute WMA as weighted sum#2250
he-yufeng wants to merge 1 commit into
microsoft:mainfrom
he-yufeng:fix/wma-weighted-sum

Conversation

@he-yufeng

Copy link
Copy Markdown

Summary

Fixes #1993.

WMA already normalizes the rolling weights so they sum to 1, but then calls np.nanmean(w * x). That divides the weighted values by the number of non-NaN entries again. For example, [1, 2, 3] with a 3-day window should be (1 + 4 + 9) / 6 = 14/6, while the current implementation returns 14/18.

This changes the reducer to np.nansum(w * x), matching the normalized-weight definition and the behavior used by the exponential weighted reducer nearby.

Validation

python setup.py build_ext --inplace

$env:PYTHONPATH=(Resolve-Path .).Path; python -m pytest tests\ops\test_rolling_ops.py -q
# 1 passed

$env:PYTHONPATH=(Resolve-Path .).Path; python -m pytest tests\ops\test_special_ops.py -q
# 2 passed

python -m py_compile qlib\data\ops.py tests\ops\test_rolling_ops.py
python -m ruff check qlib\data\ops.py tests\ops\test_rolling_ops.py
# All checks passed

git diff --check

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.

WMA计算错误

1 participant