Skip to main content

LinearRegression

Ordinary Least Squares (OLS) linear regression. Fits a linear model y^=Xw+b\hat{y} = Xw + b by minimizing the sum of squared residuals.

Objective Function

w^=argminwXwy22\hat{w} = \arg\min_w \|Xw - y\|_2^2

The gradient condition X(Xwy)=0X^\top(Xw - y) = 0 yields the normal equation:

XXw=XyX^\top X\, w = X^\top y

Solver

Skigen solves the least squares problem via Eigen::ColPivHouseholderQR, which computes a column-pivoted QR decomposition of XX. This is numerically more stable than forming and inverting XXX^\top X directly, and it correctly handles rank-deficient design matrices.

The factorization X=QRPX = QRP^\top yields the solution w=R1Qyw = R^{-1}Q^\top y (with appropriate permutation), and also provides the numerical rank of XX.

Computational Complexity

The QR factorization costs O(np2)O(np^2) where nn is the number of samples and pp is the number of features. For npn \gg p, this is dominated by forming the QQ factor.

When to Use

  • OLS is the baseline linear model — use it when features are uncorrelated and the system is well-determined (n>pn > p).
  • For ill-conditioned or underdetermined problems, prefer Ridge.
  • For feature selection, prefer Lasso or ElasticNet.

Mirrors sklearn.linear_model.LinearRegression.

Constructor

Skigen::LinearRegression<Scalar> model(bool fit_intercept = true);
ParameterDefaultDescription
fit_intercepttrueWhether to center the data and compute an intercept

Methods

MethodDescription
fit(X, y)Fit the model via QR decomposition
predict(X)Predict y^=Xw+b\hat{y} = Xw + b
score(X, y)Return the R2R^2 coefficient of determination

Fitted Attributes

AccessorTypeDescription
coef()RowVectorTypeEstimated coefficient vector w^\hat{w}
intercept()ScalarIntercept term bb
rank()IndexTypeNumerical rank of the design matrix XX

Example

#include <Skigen/LinearModel>
#include <Eigen/Dense>
#include <iostream>

int main() {
Eigen::MatrixXd X(4, 2);
X << 1, 1, 1, 2, 2, 2, 2, 3;
Eigen::VectorXd y(4);
y << 6, 8, 9, 11;

Skigen::LinearRegression model;
model.fit(X, y);

std::cout << "Coef: " << model.coef() << "\n";
std::cout << "Intercept: " << model.intercept() << "\n";
std::cout << "Rank: " << model.rank() << "\n";
std::cout << "R²: " << model.score(X, y) << "\n";
}
API Reference

For full parameter details and method signatures, see the auto-generated LinearRegression API Reference.