Skip to content

feat(linear): add ridge regression - #11

Open
BenjaminLucier wants to merge 1 commit into
mainfrom
feat/ridge
Open

BenjaminLucier wants to merge 1 commit into
mainfrom
feat/ridge

Conversation

@BenjaminLucier

Copy link
Copy Markdown
Owner

Least squares on nearly collinear columns picks large coefficients that
cancel out, and a little noise in the targets swings them wildly. Ridge
regression adds a penalty alpha * ||coef||² that keeps them small and
stable, and is the first penalty on LinearRegressor

.

Add the Ridge penalty and RidgeRegression = LinearRegressor:
new() with alpha 1 (scikit-learn's default), with_alpha, alpha(). It is
solved by the same SVD as LinearRegression: both fits now share one
private least_squares(data, intercept, alpha), and each kept singular
value s contributes 1 / (s + alpha / s) in place of 1 / s, so alpha = 0
is least squares to the bit. The intercept is never penalised. alpha
must be finite and non-negative, and finite in the data's float type
(InvalidParameter).

Tests match scikit-learn's Ridge(solver="svd") to 1e-12 on tall,
strongly penalised, weighted, no-intercept, duplicated-column and wide
data, and check alpha = 0, monotone shrinkage, the unpenalised intercept
and invalid alphas. The linear example and the book's linear page gain a
ridge section; README and AGENTS.md follow.

🤖 Generated with Claude Code

Least squares on nearly collinear columns picks large coefficients that
cancel out, and a little noise in the targets swings them wildly. Ridge
regression adds a penalty alpha * ||coef||² that keeps them small and
stable, and is the first penalty on LinearRegressor<P>.

Add the Ridge penalty and RidgeRegression = LinearRegressor<Ridge>:
new() with alpha 1 (scikit-learn's default), with_alpha, alpha(). It is
solved by the same SVD as LinearRegression: both fits now share one
private least_squares(data, intercept, alpha), and each kept singular
value s contributes 1 / (s + alpha / s) in place of 1 / s, so alpha = 0
is least squares to the bit. The intercept is never penalised. alpha
must be finite and non-negative, and finite in the data's float type
(InvalidParameter).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant