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

NameTypeRequiredDefaultDescription
yNumericArrayYesResponse vector with one value per observation.
XNumericArrayYesPredictor matrix with observations in rows and predictors in columns.
kNumericArrayYesNonnegative ridge parameters.
scaledAnyNo1Scaling flag: 1 returns standardized coefficients, 0 restores original scale and intercept.

Returns

NameTypeDescription
BNumericArrayCoefficient estimates, one column per ridge parameter.

Errors

IdentifierWhenMessage
RunMat:ridge:InvalidArgumentInputs, dimensions, ridge parameters, or scaling flag are malformed.ridge: invalid argument
RunMat:ridge:NumericalThe regularized normal equations cannot be solved numerically.ridge: numerical solve failed
RunMat:ridge:InternalRunMat cannot allocate or construct ridge outputs.ridge: internal error

How ridge works

  • X must be an n-by-p real numeric matrix and y must be a vector with n elements.
  • k accepts a nonnegative finite scalar or vector. Each output column corresponds to one k value in input order.
  • Rows containing NaN in X or y are 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.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how ridge is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.