Qu

ols_model

Builtin index · Statistics & ML

result = ols_model(X, y)

Input: X, y · Output: result

What it is

Fits ordinary least squares with an automatic intercept on an N-row, D-column feature matrix X and a length-N target vector y.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = ols_model(X, y)
X matrix or vectornot documented yet
y matrix or vectornot documented yet
coef vectorlength-D slope vector
intercept numbera number
fitted —length-N in-sample predictions
residuals —length-N
r2 numbera number, the in-sample R²
Returns a fitted model Record with fields coef (length-D slope vector), intercept (a number), fitted (length-N in-sample predictions), residuals (length-N), and r2 (a number, the in-sample R²).

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

X = [1, 2, 3, 4, 5, 6, 7, 8] as matrix(4, 2)
y = [3.1, 5.2, 7.1, 9.3]

m = ols_model(X, y)          # ordinary least squares, automatic intercept

print("coefficients {m.coef}")
print("intercept    {m.intercept:.4f}")
print("R^2          {m.score(X, y):.4f}")
output
coefficients [1.025, 1.025]
intercept    -3.0500
R^2          0.9993

from specialized

m = ols_model(X, y)
print(round(m.coef, 3))            # a field, not a method call

yhat = m.predict(Xnew)
print(round(m.score(Xnew, ynew), 4))
output
[2, -1.005, 0.497]
1

from statistics-ml

Xp = randn(30, 2, seed = 71)
yp = Xp * [1.0, -1.0] + 0.05 * randn(30, seed = 72)
mp = ols_model(Xp, yp)
print("model .predict returns a prediction vector: {round(mp.predict(Xp[0:1, :]), 3)}")
output
model .predict returns a prediction vector: [-1.1, -1.639]

from statistics-ml

Xr = randn(60, 2, seed = 61)
beta = [1.5, -0.7]
yr = Xr * beta + 0.05 * randn(60, seed = 62)
om = ols_model(Xr, yr)
print("regression score is R2: {round(om.score(Xr, yr), 4)}")
output
regression score is R2: 0.999

from statistics-ml

How it works

The model protocol (spec §37.2): a fitted estimator is a Value::Model handle with named read-only fields plus predict/score via method-call sugar. These _model- suffixed constructors sit alongside (not instead of) the pre-existing one-shot ridge/kmeans/pca, since those already have their own tests/docs/notebooks depending on their current return shape. ols_model(X, y) — ordinary least squares with an automatic intercept: X/y are mean-centered, the slope is fit on the centered data, and intercept = mean(y) - colmeans(X)·coef recovers the uncentered line — the standard fit_intercept=True convention, distinct from ridge's own "no intercept, append a ones column yourself" contract.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.