quantlab.model.predefined.linear.LinearRegressor#

class quantlab.model.predefined.linear.LinearRegressor(config)[source]#

Bases: QlibLibraryHead

Qlib’s linear model on the rows of the cross-sectional rank target.

Features are clipped to ±3 and NaN becomes 0, as Qlib’s RobustZScoreNorm(clip_outlier=True) and Fillna leave them; the training target is CSRankNorm (training_target "cs_rank"). Hyperparameters, with Qlib’s defaults:

estimator ("ols")

"ols": min |y - Xw|^2; "ridge": plus alpha |w|^2; "lasso": |y - Xw|^2 / (2n) + alpha |w|_1 (scikit-learn’s scaling); "nnls": least squares with w >= 0.

alpha (0.0)

The penalty of ridge and lasso; must be 0 for the others.

fit_intercept (False)

Whether to fit an intercept: by centring X and y, and for nnls by a column of ones, constrained like the other coefficients, as in Qlib.

include_valid (False)

Whether the validation rows are fitted too.

Early stopping does not apply. The model is {"coef": [F], "intercept": float}.

Known difference from Qlib’s implementation: no RobustZScoreNorm is fitted on the training span (the features are expected standardised already); every estimator is solved from X'X rather than by scikit-learn on the rows, which gives the same coefficients up to floating point.

Parameters:
configModelConfig

Factors, one label, dates and hyperparameters.

Raises:
ValueError

At training, if the model has more than one label, estimator is unknown, or alpha is set for an estimator without a penalty.

Examples

>>> head = LinearRegressor(ModelConfig(
...     factors=[factor], labels=[label], model_save_dir="checkpoints",
...     factor_data_strategy="read", label_data_strategy="read",
...     train_start="2024-01-01", train_end="2024-05-31",
...     test_start="2024-06-01", test_end="2024-07-18",
... )).collect()
>>> head.train().name
'LinearRegressor_total.joblib'
DEFAULTS = {'alpha': 0.0, 'estimator': 'ols', 'fit_intercept': False, 'include_valid': False}#

The library’s parameters, Qlib’s benchmark values.

LIBRARY_DEFAULTS = {'early_stopping': False, 'early_stopping_patience': 50, 'training_target': 'cs_rank'}#

Defaults of the reserved keys this variant reads itself.