{"record":{"id":"149822032c03e148","repo":"microsoft/qlib","slug":"unknown-clip-method","errorCode":null,"errorMessage":"Unknown clip_method","messagePattern":"Unknown clip_method","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"qlib/contrib/meta/data_selection/utils.py","lineNumber":93,"sourceCode":"    clip_method: str\n        The clip method. Current available: \"clamp\", \"tanh\", and \"sigmoid\".\n    \"\"\"\n    if clip_weight is not None:\n        if clip_method == \"clamp\":\n            weights = torch.exp(preds)\n            weights = weights.clamp(1.0 / clip_weight, clip_weight)\n        elif clip_method == \"tanh\":\n            weights = torch.exp(torch.tanh(preds) * np.log(clip_weight))\n        elif clip_method == \"sigmoid\":\n            # intuitively assume its sum is 1\n            if clip_weight == 0.0:\n                weights = torch.ones_like(preds)\n            else:\n                sm = nn.Sigmoid()\n                weights = sm(preds) * clip_weight  # TODO: The clip_weight is useless here.\n                weights = weights / torch.sum(weights) * weights.numel()\n        else:\n            raise ValueError(\"Unknown clip_method\")\n    else:\n        weights = torch.exp(preds)\n    return weights\n\n\nclass SingleMetaBase(nn.Module):\n    def __init__(self, hist_n, clip_weight=None, clip_method=\"clamp\"):\n        # method can be tanh or clamp\n        super().__init__()\n        self.clip_weight = clip_weight\n        if clip_method in [\"tanh\", \"clamp\"]:\n            if self.clip_weight is not None and self.clip_weight < 1.0:\n                self.clip_weight = 1 / self.clip_weight\n        self.clip_method = clip_method\n\n    def is_enabled(self):\n        if self.clip_weight is None:\n            return True","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/contrib/meta/data_selection/utils.py#L75-L111","documentation":"preds_to_weight_with_clamp converts raw meta-model predictions into instrument weights, choosing a transform by clip_method: 'clamp' (hard clamp at 1/clip_weight..clip_weight), 'tanh' (exp(tanh(x)*log(clip_weight))), or 'sigmoid'. Any other string reaches the else and raises ValueError.","triggerScenarios":"Calling preds_to_weight_with_clamp(preds, clip_weight=w, clip_method='none'/'softplus'/etc.), or SingleMetaBase(..., clip_method=...) with an unsupported name.","commonSituations":"Assuming clip_weight=None disables clipping while passing an invalid clip_method anyway; typos ('Tanh', 'clamped'); newer code expecting a method name that this version does not have.","solutions":["Use one of \"clamp\", \"tanh\", \"sigmoid\".","If you want unclipped weights, pass clip_weight=None — the whole transform block is skipped and weights are exp(preds).","Validate clip_method at configuration load time so the error surfaces early with a clear message."],"exampleFix":"// before\nw = preds_to_weight_with_clamp(preds, clip_weight=3.0, clip_method=\"none\")\n\n// after\nw = preds_to_weight_with_clamp(preds, clip_weight=3.0, clip_method=\"clamp\")\n# unclipped: preds_to_weight_with_clamp(preds, clip_weight=None)","handlingStrategy":"validation","validationCode":"VALID = {\"clamp\", \"tanh\", \"sigmoid\"}\nif clip_method not in VALID and clip_weight is not None:\n    raise ValueError(f\"clip_method must be one of {VALID}, got {clip_method!r}\")\nw = preds_to_weight_with_clamp(preds, clip_weight, clip_method)","typeGuard":"def is_clip_method(m) -> bool:\n    return m in (\"clamp\", \"tanh\", \"sigmoid\")","tryCatchPattern":"try:\n    w = preds_to_weight_with_clamp(preds, clip_weight, clip_method)\nexcept ValueError as e:\n    if \"Unknown clip_method\" in str(e):\n        w = preds_to_weight_with_clamp(preds, clip_weight, \"tanh\")\n    else:\n        raise","preventionTips":["Pass clip_weight=None when clipping is not wanted — the method check is skipped entirely.","Validate clip_method wherever configs are loaded (SingleMetaBase and the helper both accept it).","Keep the valid set as a shared constant in your code."],"tags":["qlib","meta-learning","weights","validation"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}