quantlab.portfolio.base.ExpectedReturnModel#
- class quantlab.portfolio.base.ExpectedReturnModel(config)[source]#
Bases:
_Configured,ABCBase class of every expected return model: one prediction column to the
mua mean-variance rule maximises.A mean-variance rule (
MeanVarianceOptimizer) holds one asexpected_returnand readsexpected_return_label’s prediction, each candidate’s volatility on the label’s span and the candidates; the model decides how they become the expected return (ADR 0046). It works on[bars, symbols]arrays, so the same model serves the per-bar rule (one row) and the approximate backtest. Shipped models live inquantlab.portfolio.predefined.expected_return.Subclass it, set
config_clsto a frozen dataclass of its parameters, setneeds_sigmaand implementexpected_returns; overridebindto refuse a label it cannot read.- Parameters:
- configdataclass instance
An instance of
config_cls.
Examples
>>> from quantlab.portfolio.config import GrinoldExpectedReturnConfig >>> from quantlab.portfolio.predefined.expected_return import GrinoldExpectedReturn >>> model = GrinoldExpectedReturn(GrinoldExpectedReturnConfig(ic=0.05)) >>> isinstance(model, ExpectedReturnModel), model.needs_sigma (True, True)
- bind(spec)[source]#
Check the expected-return label’s spec before any data is read; the default accepts any.
- Parameters:
- specLabelSpec
The spec of the rule’s
expected_return_label.
- Raises:
- ValueError
If the model cannot read the label (
RawExpectedReturn: a scale other than"raw").
Examples
>>> model.bind(LabelSpec(name="ret_5", scale="standardized", delay=1, span=5)) is None True
- abstractmethod expected_returns(predictions, sigma, candidates)[source]#
Return the expected return of every symbol at every bar.
- Parameters:
- predictionsnp.ndarray
[bars, symbols]: the expected-return label’s prediction, NaN where there is none.- sigmanp.ndarray or None
[bars, symbols]: each symbol’s volatility on the label’s span (the covariance’s diagonal, orvolatility_label’s prediction);Nonewhenneeds_sigmais False.- candidatesnp.ndarray
[bars, symbols]booleans: the symbols whose prediction setsmuat the bar (tradable, covered, with a finite prediction).
- Returns:
- np.ndarray
[bars, symbols]:muon the candidates, 0.0 elsewhere.
Examples
>>> model.expected_returns( ... np.array([[1.0, 3.0]]), np.array([[0.01, 0.01]]), np.ones((1, 2), dtype=bool) ... ).round(4) array([[-0.0004, 0.0004]])
- needs_sigma = True#
Whether
expected_returnsreadssigma; a rule prepares none for a model that does not.