simpleLinearRegression
Fits a simple linear regression model (y = intercept + slope * x) using ordinary least squares.
Simple linear regression finds the straight line that best fits the data by minimizing the sum of squared residuals (differences between observed and predicted y values). The result includes the fitted coefficients, goodness-of-fit measure (R²), standard errors, and residuals.
Example:
val x = doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0)
val y = doubleArrayOf(3.0, 5.0, 7.0, 9.0, 11.0)
val result = simpleLinearRegression(x, y)
result.slope // 2.0
result.intercept // 1.0
result.rSquared // 1.0 (perfect fit)
result.predict(6.0) // 13.0Note on numerical precision: the intercept is computed as meanY - slope * meanX. For data with a large constant offset (e.g. values around 1e12), this subtraction of nearly equal numbers may lose significant digits in the intercept estimate. The slope and R² are not affected.
Return
a SimpleLinearRegressionResult containing the fitted model and diagnostics.
Parameters
the array of predictor (independent variable) observations.
the array of response (dependent variable) observations, must have the same size as x.
Throws
if all x values are identical (no variance to fit a slope).