Documentation

Polynomial Regression

@statili/stats ·v0.0.2-beta.0 ·1 export

signatures
polynomial(arg1: Partial<RegressionOptions>): (args_0: DataPoint[]) => RegressionResult
polynomial(suppliedOptions: Partial<RegressionOptions>, data: DataPoint[]): RegressionResult
Performs polynomial regression of configurable degree.
Fits the curve y = c₀ + c₁x + c₂x² + … + cₙxⁿ by solving the normal equations via Gaussian elimination with partial pivoting.

Parameters

NameTypeDescription
suppliedOptions Partial<RegressionOptions> { precision?, order? }order sets the polynomial degree (default 2). Increasing degree improves fit but risks overfitting when n is close to order + 1.
data DataPoint[] Array of [x, y] tuples. Requires at least order + 1 valid points.

Returns

RegressionResult

Discriminant union:

  • ok: true — includes coefficients, degree, equation, r2, rmse, n, predict.
  • ok: false — includes errorType and message.
insight
  • Curve shapedegree and the sign of the leading coefficient determine whether the curve opens upward / downward (quadratic) or has inflection points (cubic+).
  • Goodness of fitr2 and rmse as with linear regression.
  • Overfit risk — high degree relative to n (rule of thumb: degree > n / 3) suggests the model may be fitting noise rather than signal.
  • Vertex / extremum — for a quadratic y = c₂x² + c₁x + c₀, the vertex is at x = −c₁ / (2c₂).

Examples

// Quadratic fit (default)
const data: DataPoint[] = [[1,1],[2,4],[3,9],[4,16],[5,25]];
const result = polynomial({}, data);
if (result.ok) {
  console.log(result.equation);       // "y = 1x² + 0x + 0"
  console.log(result.r2);             // ≈ 1
  console.log(result.predict(6)[1]);  // ≈ 36
}
Cubic fit
const cubic = polynomial({ order: 3, precision: 4 }, data);
// Curried / partial application (data-last)
const fitQuadratic = polynomial({ order: 2 });
const result = fitQuadratic(data);