microsoft/qlib · error · NotImplementedError

currently `oas` can only support `const_var` as target

Error message

currently `oas` can only support `const_var` as target

What it means

ShrinkRiskModel (qlib/model/riskmodel/shrink.py:84) only implements the Oracle Approximating Shrinkage ('oas') estimator against the constant-variance diagonal target. If alpha='oas' is combined with any target other than 'const_var' (e.g. 'const_corr', 'single_factor', or a custom ndarray), __init__ raises NotImplementedError immediately at construction time.

Source

Thrown at qlib/model/riskmodel/shrink.py:84

        elif isinstance(alpha, (float, np.floating)):
            assert 0 <= alpha <= 1, "alpha should be between [0, 1]"
        else:
            raise TypeError("invalid argument type for `alpha`")
        self.alpha = alpha

        # target
        if isinstance(target, str):
            assert target in [
                self.TGT_CONST_VAR,
                self.TGT_CONST_CORR,
                self.TGT_SINGLE_FACTOR,
            ], f"shrinking target `{target} is not supported"
        elif isinstance(target, np.ndarray):
            pass
        else:
            raise TypeError("invalid argument type for `target`")
        if alpha == self.SHR_OAS and target != self.TGT_CONST_VAR:
            raise NotImplementedError("currently `oas` can only support `const_var` as target")
        self.target = target

    def _predict(self, X: np.ndarray) -> np.ndarray:
        # sample covariance
        S = super()._predict(X)

        # shrinking target
        F = self._get_shrink_target(X, S)

        # get shrinking parameter
        alpha = self._get_shrink_param(X, S, F)

        # shrink covariance
        if alpha > 0:
            S *= 1 - alpha
            F *= alpha
            S += F

View on GitHub (pinned to 79633dd950)

Solutions

  1. Keep target='const_var' when using alpha='oas'
  2. Switch to alpha='lw' (Ledoit-Wolf) if you need 'const_corr' or 'single_factor' targets
  3. Use a fixed float alpha with any supported target instead of the oas estimator

Example fix

# before
model = ShrinkRiskModel(alpha='oas', target='const_corr')  # NotImplementedError

# after
model = ShrinkRiskModel(alpha='oas', target='const_var')
# or
model = ShrinkRiskModel(alpha='lw', target='const_corr')
Defensive patterns

Strategy: validation

Validate before calling

if alpha == 'oas':
    assert target == 'const_var', "alpha='oas' requires target='const_var'"

Try / catch

try:
    model = ShrinkRiskModel(alpha=alpha, target=target)
except NotImplementedError as e:
    raise ValueError("pair 'oas' only with 'const_var', or use 'lw'") from e

Prevention

When it happens

Trigger: Constructing ShrinkRiskModel(alpha='oas', target='const_corr'); ShrinkRiskModel(alpha='oas', target=my_ndarray); any config pairing oas with const_var single-factor targets.

Common situations: Reusing a shrinkage config and switching alpha from 'lw' to 'oas' while keeping target='const_corr'; assuming all alpha/target combinations are supported because they appear in docs as separate options.

Related errors


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