{"record":{"id":"361ed540c89cd1b9","repo":"microsoft/qlib","slug":"please-implement-the-abs-method","errorCode":null,"errorMessage":"Please implement the `abs` method","messagePattern":"Please implement the `abs` method","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"qlib/backtest/high_performance_ds.py","lineNumber":272,"sourceCode":"    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\n        raise NotImplementedError(f\"Please implement the `add` method\")\n\n    def replace(self, replace_dict: dict) -> BaseSingleMetric:\n        \"\"\"Replace the value of metric according to replace_dict.\"\"\"\n\n        raise NotImplementedError(f\"Please implement the `replace` method\")\n\n    def apply(self, func: Callable) -> BaseSingleMetric:","sourceCodeStart":254,"sourceCodeEnd":290,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/backtest/high_performance_ds.py#L254-L290","documentation":"BaseSingleMetric.abs() is the abstract element-wise absolute value, returning a new metric of the same type. The base class raises NotImplementedError; SingleMetric implements it. Calling .abs() on a bare BaseSingleMetric raises this error.","triggerScenarios":"m.abs() where m is a raw BaseSingleMetric; risk metrics like total absolute exposure or sum(|weight|); subclasses implementing arithmetic operators but not abs.","commonSituations":"L1-norm style exposure checks; converting signed PnL to magnitude metrics for reports; custom backends with partial method coverage.","solutions":["Use SingleMetric.abs()","Implement abs(self) in your subclass returning type(self)(self.storage.abs())","For quick checks without a subclass, take absolute values on the underlying Series and re-wrap in SingleMetric"],"exampleFix":"# before\nexposure = BaseSingleMetric(weights).abs().sum()\n# after\nfrom qlib.backtest.high_performance_ds import SingleMetric\nexposure = SingleMetric(weights).abs().sum()","handlingStrategy":"type-guard","validationCode":"from qlib.backtest.high_performance_ds import BaseSingleMetric\nassert type(m).abs is not BaseSingleMetric.abs, \"object does not implement abs()\"","typeGuard":"def supports_abs(m) -> bool:\n    from qlib.backtest.high_performance_ds import BaseSingleMetric\n    return isinstance(m, BaseSingleMetric) and type(m).abs is not BaseSingleMetric.abs","tryCatchPattern":null,"preventionTips":["Use SingleMetric.abs() for element-wise magnitude","abs() must return a new metric of the same type, not modify in place","Implement abs() when building risk/exposure metrics in subclasses"],"tags":["python","qlib","abstract-method","math","metrics"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}