microsoft/qlib · error · TypeError
invalid argument type for `target`
Error message
invalid argument type for `target`
What it means
ShrinkRiskModel.__init__ (qlib/model/riskmodel/shrink.py:82) validates the `target` argument (the shrinking target matrix F): it must be a string in {'const_var', 'const_corr', 'single_factor'} or an np.ndarray supplied directly. Anything else (None, list, pd.DataFrame, int) raises TypeError('invalid argument type for `target`'). Note lists and DataFrames are not auto-converted.
Source
Thrown at qlib/model/riskmodel/shrink.py:82
if isinstance(alpha, str):
assert alpha in [self.SHR_LW, self.SHR_OAS], f"shrinking method `{alpha}` is not supported"
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 *= alphaView on GitHub (pinned to 79633dd950)
Solutions
- Use one of the built-in target strings: 'const_var', 'const_corr', or 'single_factor'
- Convert custom targets to np.ndarray first: target=df.to_numpy() or np.asarray(list_target)
- Ensure the config value for target is a string; remove null values
Example fix
# before model = ShrinkRiskModel(target=cov_df) # pd.DataFrame -> TypeError # after model = ShrinkRiskModel(target=cov_df.to_numpy()) # or model = ShrinkRiskModel(target='const_corr')
Defensive patterns
Strategy: validation
Validate before calling
import numpy as np
assert isinstance(target, str) and target in ('const_var', 'const_corr', 'single_factor') or isinstance(target, np.ndarray), f'invalid target: {target!r}' Type guard
def is_valid_target(t) -> bool:
return (isinstance(t, str) and t in ('const_var', 'const_corr', 'single_factor')) or isinstance(t, np.ndarray) Try / catch
try:
model = ShrinkRiskModel(target=target)
except TypeError as e:
raise ValueError('target must be const_var/const_corr/single_factor or np.ndarray') from e Prevention
- Convert DataFrames/lists to np.ndarray before passing custom targets
- Keep target as one of the documented string constants in configs
When it happens
Trigger: Constructing ShrinkRiskModel(target=None); passing target as a Python list or pandas DataFrame instead of np.ndarray; passing an int/float thinking it scales the target.
Common situations: Supplying a custom shrinkage target computed as a DataFrame from a prior covariance step; configs omitting target or setting it to null; assuming any array-like is accepted.
Related errors
- invalid argument type for `alpha`
- currently `oas` can only support `const_var` as target
- This type of `limit_threshold` is not supported
- stock data from resam_ts_data must be a number, pd.Series or
- {freq} is not supported in NumpyQuote
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/4853653916a6d5fa.
Report an issue: GitHub.