ridge — Fit ridge regression coefficients for one or more regularization parameters.
ridge(y,X,k) returns ridge regression coefficient estimates for response vector y, predictor matrix X, and one or more nonnegative ridge parameters k.
Syntax
B = ridge(y, X, k)
B = ridge(y, X, k, scaled)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
y | NumericArray | Yes | — | Response vector with one value per observation. |
X | NumericArray | Yes | — | Predictor matrix with observations in rows and predictors in columns. |
k | NumericArray | Yes | — | Nonnegative ridge parameters. |
scaled | Any | No | 1 | Scaling flag: 1 returns standardized coefficients, 0 restores original scale and intercept. |
Returns
| Name | Type | Description |
|---|---|---|
B | NumericArray | Coefficient estimates, one column per ridge parameter. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:ridge:InvalidArgument | Inputs, dimensions, ridge parameters, or scaling flag are malformed. | ridge: invalid argument |
RunMat:ridge:Numerical | The regularized normal equations cannot be solved numerically. | ridge: numerical solve failed |
RunMat:ridge:Internal | RunMat cannot allocate or construct ridge outputs. | ridge: internal error |
How ridge works
Xmust be an n-by-p real numeric matrix andymust be a vector with n elements.kaccepts a nonnegative finite scalar or vector. Each output column corresponds to onekvalue in input order.- Rows containing
NaNinXoryare omitted before fitting, matching MATLAB's missing-row behavior. - By default, RunMat centers
y, centers and standardizes predictors, and returns p standardized coefficients without an intercept. ridge(y,X,k,0)restores coefficients to the original predictor scale and returns a (p+1)-by-numel(k) matrix whose first row is the intercept.ridge(y,X,k,1)is the default scaled form and returns a p-by-numel(k) matrix.
Examples
Fit an original-scale ridge model
X = [0; 1; 2; 3];
y = [1; 3; 5; 7];
B = ridge(y, X, 0, 0)Expected output:
B is approximately [1; 2].Fit a ridge trace
B = ridge(y, X, [0 1 10])Expected output:
B has one column per ridge parameter and contains standardized coefficients.Using ridge with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how ridge changes the result.
Run a small ridge example, explain the result, then change one input and compare the output.
FAQ
Should I include a column of ones in X?⌄
No. Like MATLAB, ridge centers the data internally. Use scaled=0 when you need the intercept in the returned coefficients.
How does scaled change the output size?⌄
scaled=1 returns p rows. scaled=0 returns p+1 rows, with the intercept in the first row.
Related Stats functions
Ml
bayesopt · classify · confusionmat · crossvalind · cvpartition · fitclinear · fitctree · fitlm · kmeans · knnsearch · lasso · lassoglm · linkage · lscov · mnrfit · optimizableVariable · pdist · pdist2 · perfcurve · predict · regress · squareform · test · training · tsne
Summary
binocdf · boxplot · cdf · cdfplot · chi2cdf · corr · corrcoef · corrcov · cov · cov2corr · dummyvar · ecdf · filloutliers · fitdist · geomean · grpstats · harmmean · icdf · isoutlier · kstest · kurtosis · lsline · mad · mode · nanmax · normalize · normcdf · norminv · normpdf · onehotdecode · onehotencode · pdf · prctile · quantile · refline · rmse · skewness · tabulate · tcdf · tiedrank · tinv · tpdf · ttest2 · wblinv
Random
binornd · bootstrp · datasample · dividerand · exprnd · gamrnd · lhsdesign · mvnrnd · normrnd · random · randsample · rng · trnd · unidrnd · unifrnd · wblrnd
Hist
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how ridge is executed, line by line, in Rust.
- View the source for ridge in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.