From 32de9dd9b79454bbcb34fbb78b00b401fc457696 Mon Sep 17 00:00:00 2001 From: Ayush Ojha Date: Fri, 30 Jan 2026 05:16:49 -0800 Subject: [PATCH] fix: correct WMA calculation to use nansum instead of nanmean (#1993) The weighted_mean function normalizes weights to sum to 1, so np.nanmean (which divides by count) produces incorrect results. Use np.nansum instead, consistent with the EMA implementation. --- qlib/data/ops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qlib/data/ops.py b/qlib/data/ops.py index d9a2ffbb3e3..671948d98cf 100644 --- a/qlib/data/ops.py +++ b/qlib/data/ops.py @@ -1337,7 +1337,7 @@ def _load_internal(self, instrument, start_index, end_index, *args): def weighted_mean(x): w = np.arange(len(x)) + 1 w = w / w.sum() - return np.nanmean(w * x) + return np.nansum(w * x) if self.N == 0: series = series.expanding(min_periods=1).apply(weighted_mean, raw=True)