microsoft/qlib · error · TypeError
invalid argument type for `alpha`
Error message
invalid argument type for `alpha`
What it means
ShrinkRiskModel.__init__ (qlib/model/riskmodel/shrink.py:69) validates the `alpha` argument: it must be either the string 'lw' or 'oas' (Ledoit-Wolf / Oracle Approximating Shrinkage estimators) or a float in [0, 1]. Any other type (int out of range, None, list, dict) raises TypeError('invalid argument type for `alpha`'). Note plain Python int is also rejected since only float/np.floating are accepted.
Source
Thrown at qlib/model/riskmodel/shrink.py:69
TGT_CONST_CORR = "const_corr"
TGT_SINGLE_FACTOR = "single_factor"
def __init__(self, alpha: Union[str, float] = 0.0, target: Union[str, np.ndarray] = "const_var", **kwargs):
"""
Args:
alpha (str or float): shrinking parameter or estimator (`lw`/`oas`)
target (str or np.ndarray): shrinking target (`const_var`/`const_corr`/`single_factor`)
kwargs: see `RiskModel` for more information
"""
super().__init__(**kwargs)
# alpha
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:View on GitHub (pinned to 79633dd950)
Solutions
- Set alpha to 'lw' or 'oas' to use a built-in estimator
- Set alpha to a float between 0 and 1 inclusive, e.g. alpha=0.1 (write 0.1, not an int)
- Check your YAML/JSON config for null/integer alpha values and coerce them to float or estimator strings
Example fix
# before model = ShrinkRiskModel(alpha=None) # TypeError # after model = ShrinkRiskModel(alpha='lw') # or fixed float model = ShrinkRiskModel(alpha=0.1)
Defensive patterns
Strategy: validation
Validate before calling
import numbers
assert alpha in ('lw', 'oas') or (isinstance(alpha, (float, np.floating)) and 0 <= alpha <= 1), f'invalid alpha: {alpha!r}' Type guard
def is_valid_alpha(alpha) -> bool:
return alpha in ('lw', 'oas') or (isinstance(alpha, (float, np.floating)) and 0.0 <= alpha <= 1.0) Try / catch
try:
model = ShrinkRiskModel(alpha=alpha)
except TypeError as e:
raise ValueError(f"alpha must be 'lw'/'oas' or float in [0,1], got {alpha!r}") from e Prevention
- In YAML configs write alpha as 0.1 (float) or an estimator string, never null/int
- Validate risk-model hyperparameters at config load time
When it happens
Trigger: Constructing ShrinkRiskModel(alpha=None), alpha=1 (int), alpha=[0.1, 0.2], or a misspelled estimator string; passing alpha as an unsupported estimator name like 'james_stein' (that hits the assert instead).
Common situations: Config files with alpha: null or integer alpha (e.g. alpha: 1); users assuming alpha accepts any sklearn covariance estimator name; copy-pasting configs between risk model classes with different alpha semantics.
Related errors
- invalid argument type for `target`
- 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/31094f44ffc7418a.
Report an issue: GitHub.