from _api_doc_utils import *Ridge
L2-regularized least squares with optional CV
1 Where it fits
Group: Regression
Ridge solves
\[ \min_{\alpha,\beta} \sum_i (y_i - \alpha - x_i'\beta)^2 + \lambda \|\beta\|_2^2. \]
A scalar penalty gives one ridge fit. A penalty grid with cv selects a penalty by cross-validation, stores the coefficient path, and refits on the full sample.
2 Python API
Constructor: cm.Ridge
The main methods mirror OLS: fit, fit_weighted, predict, summary, and bootstrap. summary() includes the selected penalty and, for grid fits, cross-validation diagnostics and coefficient paths.
print(inspect.signature(cm.Ridge))(penalty=None, cv=5)
cls = cm.Ridge
display(HTML(html_table(["Public method"], public_methods(cls))))| Public method |
|---|
bootstrap(self, /, n_bootstrap, seed=None) |
fit(self, /, x, y) |
fit_weighted(self, /, x, y, sample_weight) |
predict(self, /, x) |
summary(self, /, vcov='hc1', lags=None, clusters=None) |
3 Minimal example
rng = np.random.default_rng(2)
x = rng.normal(size=(240, 5))
y = 0.3 + x @ np.array([1.0, -0.8, 0.0, 0.25, 0.1]) + rng.normal(scale=0.6, size=240)
model = cm.Ridge(penalty=np.array([0.0, 0.05, 0.2, 1.0]), cv=4)
model.fit(x, y)
print(model.summary()["penalty"])
print(model.summary()["coef"])
print(model.predict(x[:3]))1.0
[ 0.96303432 -0.80048681 -0.1007246 0.29078042 0.11061705]
[0.38294603 1.55660946 1.29480827]
4 summary() contract
The table below is generated by fitting the live class in this repository and then inspecting summary(). Shapes are shown because most values are plain NumPy arrays or scalars.
rng = np.random.default_rng(102)
x = rng.normal(size=(90, 4)); y = 0.3 + x @ np.array([1, -.5, .2, 0]) + rng.normal(size=90)
model = cm.Ridge(penalty=np.array([0.0, 0.1, 1.0]), cv=3); model.fit(x, y)
summary = model.summary()
display(HTML(html_table(["summary() key", "shape"], summary_shape_rows(summary))))| summary() key | shape |
|---|---|
intercept |
() |
coef |
(4,) |
intercept_se |
() |
coef_se |
(4,) |
penalty |
() |
penalties |
(3,) |
vcov_type |
() |
best_penalty_index |
() |
cv_mse |
(3,) |
intercept_path |
(3,) |
coef_path |
(4, 3) |