Regression / Curve Fit

Shipping
regression

Fit a model (linear, polynomial, exponential, Gaussian, custom…) to a signal with weighted least squares

Signature

Inputs

  • signalSignalrequiredTraining data: timestamps are the independent variable x, values are y (≥ 2 samples). Per-sample σ² drives inverse-variance weighting.
  • weightsSignalOPTIONAL per-point weights wᵢ for weighted least squares. When unfed, the node falls back to inverse-variance weights from the signal's σ², else ordinary least squares.

Outputs

  • fittedSignalModel prediction ŷ over the same timestamps (carries the input unit).
  • residualsSignaly − ŷ over the same timestamps (input unit).
  • r2Scalar(Weighted) coefficient of determination R² (dimensionless).
  • rmseScalar(Weighted) root-mean-square error of the residuals (input unit).
  • paramsDataFrameOne column per fitted coefficient, one row of values (e.g. c0…cN for a polynomial, a/b/c for a custom sinusoid).

Parameters

KeyTypeDefaultNotes
modelenumlinearone of: linear, polynomial, exponential, logarithmic, power, gaussian, custom
solverenumautoone of: auto, svd, levenberg_marquardt
orderint2Polynomial degree N (shown only when model = polynomial).
custom_formulaformulaa * sin(b * x) + cExpression in x with free parameters (shown when model = custom). Built-ins: sin, cos, exp, ln, sqrt, pi, e.
initial_guessestextComma-separated LM seed values in alphabetical parameter order (shown when model = custom). Empty = auto-estimate.
max_iterationsint100Levenberg-Marquardt iteration budget (patience) for non-linear models; ignored by the SVD/linear path.

Description

Regression (a.k.a. Curve Fit) fits a chosen model to a signal, using the signal's timestamps as the independent variable $x$ and its values as $y$. It supports seven model families — linear, polynomial (any degree), exponential, logarithmic, power, Gaussian, and a fully custom formula in — and dispatches to the right solver automatically: fast SVD for linearizable models, Levenberg-Marquardt for the non-linear (gaussian/custom) ones.

Every fit is a weighted least-squares problem. Weights are resolved by a clear precedence: an explicit weights Signal wins; otherwise, if the input signal carries per-sample variances , the node uses inverse-variance weighting (the maximum-likelihood weighting for independent Gaussian noise); otherwise the fit is ordinary (unweighted). This lets measurement uncertainty automatically down-weight noisy points and suppress outliers.

Outputs are lossless: fitted and residuals are Signals over the original timestamps (carrying the input unit), r2 (dimensionless) and rmse (input unit) score the fit, and the params DataFrame exposes every coefficient the solver produced — one named column each — so any coefficient is readable downstream. The node is stateless.

Per-sample on the fitted curve is deliberately dropped: a correct confidence band requires propagating the parameter covariance through the model Jacobian, which is not an element-wise map, so input variances are instead honoured the correct way — as fit weights.

Mathematics

Examples

Exact linear fit

For sampled at with model = linear, the fit is exact: r2 , residuals , and the params table reports c0 (intercept), c1 (slope).

Custom sinusoid via Levenberg-Marquardt

With model = custom, custom_formula = a * sin(b * x) + c on a noisy , the LM solver (with multi-start seeding) recovers and . Raise max_iterations if a hard non-linear fit reports non-convergence.

Outlier suppression via σ²

If the input signal carries variances that are tiny for trustworthy points and huge () at one outlier, the node auto-applies inverse-variance weights and recovers the clean line — no explicit weights wiring needed.

Applications

  • Calibration curves and trend-line fitting on measured data.
  • Peak fitting (Gaussian) for spectroscopy / chromatography.
  • Arbitrary phenomenological models via the custom-formula solver.
  • Uncertainty-aware fitting where noisy samples are down-weighted automatically.

Neat

Weight precedence is explicit-weights → inverse-variance-from-σ² → unweighted, so a signal that already carries measurement uncertainty is fit optimally with nothing extra wired.

The params output is a full DataFrame — every solver coefficient is named and exposed, so you can route a fitted slope or Gaussian width straight into the rest of the graph.

Auto solver selection: linearizable models go through a one-shot SVD, while gaussian/custom use Levenberg-Marquardt with multi-start seeding to escape the single-seed convergence failures.

It shares its fitting core with the app's FFI curve-fit endpoint, so an interactive fit and a graph node give identical results.

Known issues

The fitted/residuals Signals carry no per-sample σ² (a proper confidence band needs the parameter covariance through the Jacobian, not an element-wise map).

Non-linear (gaussian/custom) fits can fail to converge on poorly-seeded or oscillatory data — raise max_iterations or supply initial_guesses.

Requires at least 2 samples; fewer errors out.

See also

regressioncurve-fitleast-squareslevenberg-marquardtsvdweightedstateless