ridge static method

RegressionResult ridge(
  1. Matrix X,
  2. dynamic y, {
  3. double alpha = 1.0,
})

Ridge regression (L2 regularization).

Implementation

static RegressionResult ridge(Matrix X, dynamic y, {double alpha = 1.0}) {
  List<num> yList = _toNumList(y);
  int n = X.rowCount;
  int p = X.columnCount;

  if (yList.length != n) {
    throw ArgumentError('y length must match X rows');
  }

  // Add intercept column
  List<List<double>> xData = [];
  for (int i = 0; i < n; i++) {
    List<double> row = [1.0];
    for (int j = 0; j < p; j++) {
      row.add(X[i][j].toDouble());
    }
    xData.add(row);
  }

  Matrix xMat = Matrix(xData);
  Matrix yMat = Matrix(yList.map((e) => [e.toDouble()]).toList());

  // Use built-in ridge regression
  Matrix beta = LinearSystemSolvers.ridgeRegression(xMat, yMat, alpha);

  List<num> coefficients = [];
  for (int i = 0; i < beta.rowCount; i++) {
    coefficients.add(_toNum(beta[i][0]));
  }

  // Calculate predictions and metrics
  List<num> predictions = [];
  for (int i = 0; i < n; i++) {
    num pred = coefficients[0];
    for (int j = 0; j < p; j++) {
      pred += coefficients[j + 1] * _toNum(X[i][j]);
    }
    predictions.add(pred);
  }

  List<num> residuals = [];
  for (int i = 0; i < n; i++) {
    residuals.add(yList[i] - predictions[i]);
  }

  num yMean = yList.reduce((a, b) => a + b) / n;
  num ssTot =
      yList.map((yi) => (yi - yMean) * (yi - yMean)).reduce((a, b) => a + b);
  num ssRes = residuals.map((r) => r * r).reduce((a, b) => a + b);
  num rSquared = 1 - ssRes / ssTot;

  return RegressionResult(
    coefficients: Series(coefficients, name: 'coefficients'),
    rSquared: rSquared,
    adjustedRSquared: 1 - (1 - rSquared) * (n - 1) / (n - p - 1),
    residuals: Series(residuals, name: 'residuals'),
    predictions: Series(predictions, name: 'predictions'),
  );
}