Scalar FunctionsSeries Functions
series_fit_poly
Applies polynomial regression to a series, fitting a curve of the specified degree.
Syntax
series_fit_poly(y_series, x_series, degree)Parameters
Prop
Type
Returns: dynamic
Examples
Example 1 — Quadratic fit with explicit x-values — tuple destructuring names each output column (rsquare=1.0 means perfect fit to y = 10x²)
print (rsquare, coefficients, variance, rvariance, fitted) = series_fit_poly(
dynamic([10, 40, 90, 160, 250]),
dynamic([1, 2, 3, 4, 5]),
2
)| rsquare (real) | coefficients (dynamic) | variance (real) | rvariance (real) | fitted (dynamic) |
|---|---|---|---|---|
| 1.0 | [-0.00000000000006614506921984932,0.00000000000004466268624777775,9.999999999999993] | 9350.0 | 0.0000000000000000000000000005301145283085199 | [9.999999999999972,39.99999999999999,90.00000000000001,160.0,250.0] |
Example 2 — Quadratic fit with auto-generated x-values [1,2,3,4,5] — rsquare near 1.0 confirms the data follows a parabolic trend
print (rsquare, coefficients, variance, rvariance, fitted) = series_fit_poly(
dynamic([5, 12, 25, 44, 69]),
2
)| rsquare (real) | coefficients (dynamic) | variance (real) | rvariance (real) | fitted (dynamic) |
|---|---|---|---|---|
| 1.0 | [3.9999999999998592,-1.9999999999998883,2.9999999999999822] | 671.5 | 0.0000000000000000000000000024269305749124428 | [4.999999999999953,12.00000000000001,25.000000000000036,44.00000000000002,68.99999999999997] |
Example 3 — Compare linear (degree=1) vs quadratic (degree=2) — the higher rsquare tells you which model fits better
print (r_linear, c1, v1, rv1, fit_linear) = series_fit_poly(dynamic([5, 12, 25, 44, 69]), 1), (r_quad, c2, v2, rv2, fit_quad) = series_fit_poly(
dynamic([5, 12, 25, 44, 69]),
2
)| r_linear (real) | c1 (dynamic) | v1 (real) | rv1 (real) | fit_linear (dynamic) | r_quad (real) | c2 (dynamic) | v2 (real) | rv2 (real) | fit_quad (dynamic) |
|---|---|---|---|---|---|---|---|---|---|
| 0.953090096798213 | [-17.0,16.0] | 671.5 | 42.0 | [-1.0,15.0,31.0,47.0,63.0] | 1.0 | [3.9999999999998592,-1.9999999999998883,2.9999999999999822] | 671.5 | 0.0000000000000000000000000024269305749124428 | [4.999999999999953,12.00000000000001,25.000000000000036,44.00000000000002,68.99999999999997] |