microsoft/qlib · error · ValueError

Model hasn't been trained yet

Error message

Model hasn't been trained yet

What it means

Thrown by HFMLGBModel.hf_signal_test when self.model is None, meaning the high-frequency LightGBM booster was never trained. The signal test predicts on the 'test' segment and computes precision/alpha metrics, which requires a fitted model.

Source

Thrown at qlib/contrib/model/highfreq_gdbt_model.py:62

            up_pre.append(up_precision)
            down_pre.append(down_precision)
            up_alpha_ll.append(up_alpha)
            down_alpha_ll.append(down_alpha)

        return (
            np.array(up_pre).mean(),
            np.array(down_pre).mean(),
            np.array(up_alpha_ll).mean(),
            np.array(down_alpha_ll).mean(),
        )

    def hf_signal_test(self, dataset: DatasetH, threhold=0.2):
        """
        Test the signal in high frequency test set
        """
        if self.model is None:
            raise ValueError("Model hasn't been trained yet")
        df_test = dataset.prepare("test", col_set=["feature", "label"], data_key=DataHandlerLP.DK_I)
        df_test.dropna(inplace=True)
        x_test, y_test = df_test["feature"], df_test["label"]
        # Convert label into alpha
        y_test[y_test.columns[0]] = y_test[y_test.columns[0]] - y_test[y_test.columns[0]].mean(level=0)

        res = pd.Series(self.model.predict(x_test.values), index=x_test.index)
        y_test["pred"] = res

        up_p, down_p, up_a, down_a = self._cal_signal_metrics(y_test, threhold, 1 - threhold)
        print("===============================")
        print("High frequency signal test")
        print("===============================")
        print("Test set precision: ")
        print("Positive precision: {}, Negative precision: {}".format(up_p, down_p))
        print("Test Alpha Average in test set: ")
        print("Positive average alpha: {}, Negative average alpha: {}".format(up_a, down_a))

View on GitHub (pinned to 79633dd950)

Solutions

  1. Call model.fit(dataset) successfully before hf_signal_test(dataset)
  2. Fix the underlying fit failure (commonly empty data or label config issues in _prepare_data)

Example fix

# before
model = HFMLGBModel()
model.hf_signal_test(dataset)  # ValueError

# after
model.fit(dataset)
model.hf_signal_test(dataset, threhold=0.2)
Defensive patterns

Strategy: validation

Validate before calling

assert model.model is not None, "HFMLGBModel must be fitted before hf_signal_test()"

Prevention

When it happens

Trigger: Calling hf_signal_test(dataset) on a fresh HFMLGBModel or after fit failed; testing before training in a high-frequency experiment script.

Common situations: Experiment scripts that run signal evaluation unconditionally; fit failures (empty data / multi-label from _prepare_data) being caught and skipped before the test step.

Related errors


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