Polynomial Regression
# polynomial function
packages/stats/src/regression/polynomial.ts:49polynomial(arg1: Partial<RegressionOptions>): (args_0: DataPoint[]) => RegressionResult
polynomial(suppliedOptions: Partial<RegressionOptions>, data: DataPoint[]): RegressionResultPerforms 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
| Name | Type | Description |
|---|---|---|
| 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
RegressionResultDiscriminant union:
ok: true— includescoefficients,degree,equation,r2,rmse,n,predict.ok: false— includeserrorTypeandmessage.
insight
- Curve shape —
degreeand the sign of the leading coefficient determine whether the curve opens upward / downward (quadratic) or has inflection points (cubic+). - Goodness of fit —
r2andrmseas with linear regression. - Overfit risk — high
degreerelative ton(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
}const cubic = polynomial({ order: 3, precision: 4 }, data);// Curried / partial application (data-last)
const fitQuadratic = polynomial({ order: 2 });
const result = fitQuadratic(data);