The estimator contract
Every scikit-learn model follows one contract, and that contract is why models can be swapped, chained and tuned. __init__ only stores the parameters it receives, under the same names, with no checks and no computation: that is what lets get_params, set_params and clone work. fit(X, y) learns from the data, stores what it learned in attributes that end with an underscore (coef_, intercept_, n_features_in_) and returns self, so calls can be chained. predict(X) first calls check_is_fitted, which raises NotFittedError if fit has not run, then validates X with check_array and applies what was learned. Inherit from BaseEstimator for the parameter handling and from RegressorMixin for a free score method (put the mixin first).
The model in this lab is ridge regression. With centred data it solves (XᵀX+αI) w=Xᵀy, and the intercept is ȳ-x̄·w. A larger α shrinks the coefficients towards zero, which is a good habit when the features are noisy lagged returns. Because your class follows the contract, make_pipeline(StandardScaler(), ShrinkageRegressor()), cross_val_score and GridSearchCV accept it, and clone(model) gives a fresh unfitted copy. That last point matters in a walk-forward evaluation: fit a clone on each training window, never the model itself, or the information of one fold leaks into the next. The score for a return forecast is the Spearman rank correlation between predictions and outcomes on the test rows, the information coefficient.
- Parameters in, learned attributes out:
__init__stores,fitlearns into names ending with an underscore and returnsself. - Check before you predict:
check_is_fittedandcheck_arraygive the standard errors that pipelines and users expect. - Fit a clone: in a walk-forward loop,
clone(model).fit(...)keeps folds independent.
Lab · what's graded
- Lagged features10 pts
- Ridge from the normal equations15 pts
- The constructor10 pts
- Fit and predict20 pts
- Working with Pipeline and GridSearchCV10 pts
- A walk-forward rank correlation15 pts
- Does the past of SPY predict its next day?20 pts