microsoft/qlib · error · NotImplementedError

Please implement the `sum` method

Error message

Please implement the `sum` method

What it means

BaseSingleMetric.sum() is the abstract reduction that should return the total of the metric values across stocks (as a float). The base class raises NotImplementedError; SingleMetric implements the concrete NaN-aware sum. Calling .sum() on a bare BaseSingleMetric raises this error.

Source

Thrown at qlib/backtest/high_performance_ds.py:261

        raise NotImplementedError(f"Please implement the `__mul__` method")

    def __truediv__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:
        raise NotImplementedError(f"Please implement the `__truediv__` method")

    def __eq__(self, other: object) -> BaseSingleMetric:
        raise NotImplementedError(f"Please implement the `__eq__` method")

    def __gt__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:
        raise NotImplementedError(f"Please implement the `__gt__` method")

    def __lt__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:
        raise NotImplementedError(f"Please implement the `__lt__` method")

    def __len__(self) -> int:
        raise NotImplementedError(f"Please implement the `__len__` method")

    def sum(self) -> float:
        raise NotImplementedError(f"Please implement the `sum` method")

    def mean(self) -> float:
        raise NotImplementedError(f"Please implement the `mean` method")

    def count(self) -> int:
        """Return the count of the single metric, NaN is not included."""

        raise NotImplementedError(f"Please implement the `count` method")

    def abs(self) -> BaseSingleMetric:
        raise NotImplementedError(f"Please implement the `abs` method")

    @property
    def empty(self) -> bool:
        """If metric is empty, return True."""

        raise NotImplementedError(f"Please implement the `empty` method")

View on GitHub (pinned to 79633dd950)

Solutions

  1. Use SingleMetric.sum(), which sums the underlying values while handling NaN
  2. Implement sum(self) in your subclass (e.g. return float(self.storage.sum()) if pandas-backed)
  3. For mixed pipelines, dispatch on hasattr(type(m), 'sum') is not enough — check the method is not the base stub

Example fix

# before
total = BaseSingleMetric(values).sum()
# after
from qlib.backtest.high_performance_ds import SingleMetric
total = SingleMetric(values).sum()
Defensive patterns

Strategy: type-guard

Validate before calling

from qlib.backtest.high_performance_ds import BaseSingleMetric
assert type(m).sum is not BaseSingleMetric.sum, "object does not implement sum()"

Type guard

def supports_sum(m) -> bool:
    from qlib.backtest.high_performance_ds import BaseSingleMetric
    return isinstance(m, BaseSingleMetric) and type(m).sum is not BaseSingleMetric.sum

Prevention

When it happens

Trigger: m.sum() where m is a raw BaseSingleMetric; portfolio-level aggregation of per-stock metrics (total market value, total profit); subclasses implementing mean but not sum; code using sum(metric) which routes through __radd__/__add__ instead of .sum() and fails differently.

Common situations: Building performance reports (total return, total exposure); summary statistics in custom evaluators; incomplete custom metric backends.

Related errors


AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15). Data as JSON: /api/errors/8ef7dc5cee9add32. Report an issue: GitHub.