{"record":{"id":"24470d140d44a933","repo":"microsoft/qlib","slug":"please-implement-the-mean-method","errorCode":null,"errorMessage":"Please implement the `mean` method","messagePattern":"Please implement the `mean` method","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"qlib/backtest/high_performance_ds.py","lineNumber":264,"sourceCode":"        raise NotImplementedError(f\"Please implement the `__truediv__` method\")\n\n    def __eq__(self, other: object) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__eq__` method\")\n\n    def __gt__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__gt__` method\")\n\n    def __lt__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__lt__` method\")\n\n    def __len__(self) -> int:\n        raise NotImplementedError(f\"Please implement the `__len__` method\")\n\n    def sum(self) -> float:\n        raise NotImplementedError(f\"Please implement the `sum` method\")\n\n    def mean(self) -> float:\n        raise NotImplementedError(f\"Please implement the `mean` method\")\n\n    def count(self) -> int:\n        \"\"\"Return the count of the single metric, NaN is not included.\"\"\"\n\n        raise NotImplementedError(f\"Please implement the `count` method\")\n\n    def abs(self) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `abs` method\")\n\n    @property\n    def empty(self) -> bool:\n        \"\"\"If metric is empty, return True.\"\"\"\n\n        raise NotImplementedError(f\"Please implement the `empty` method\")\n\n    def add(self, other: BaseSingleMetric, fill_value: float = None) -> BaseSingleMetric:\n        \"\"\"Replace np.nan with fill_value in two metrics and add them.\"\"\"\n","sourceCodeStart":246,"sourceCodeEnd":282,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/backtest/high_performance_ds.py#L246-L282","documentation":"BaseSingleMetric.mean() is the abstract per-stock average reduction, returning a float. The base class raises NotImplementedError; SingleMetric implements the NaN-aware mean. Calling .mean() on a bare BaseSingleMetric instance raises this error.","triggerScenarios":"m.mean() where m is a raw BaseSingleMetric; cross-sectional statistics (average score, average weight) in evaluators; subclasses implementing sum but not mean.","commonSituations":"Reporting average per-stock metrics; factor evaluation code computing cross-sectional means; custom metric classes with partial reduction implementations.","solutions":["Use SingleMetric.mean()","Implement mean(self) in your subclass (NaN-aware, e.g. float(self.storage.mean()) if pandas-backed)","Reuse the base-class pattern: often mean can be implemented as self.sum() / self.count() once both are concrete"],"exampleFix":"# before\navg = BaseSingleMetric(values).mean()\n# after\nfrom qlib.backtest.high_performance_ds import SingleMetric\navg = SingleMetric(values).mean()","handlingStrategy":"type-guard","validationCode":"from qlib.backtest.high_performance_ds import BaseSingleMetric\nassert type(m).mean is not BaseSingleMetric.mean, \"object does not implement mean()\"","typeGuard":"def supports_mean(m) -> bool:\n    from qlib.backtest.high_performance_ds import BaseSingleMetric\n    return isinstance(m, BaseSingleMetric) and type(m).mean is not BaseSingleMetric.mean","tryCatchPattern":null,"preventionTips":["Use SingleMetric.mean() for cross-sectional averages","Remember mean is NaN-aware: pair it with count() to know the denominator","Implement all abstract reductions when subclassing, not just the one you need now"],"tags":["python","qlib","abstract-method","aggregation","metrics"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}