Fit
✖
Fit
Details and Options




- Fit is also known as linear regression or least squares fit. With regularization, it is also known as LASSO and ridge regression.
- Fit is typically used for fitting combinations of functions to data, including polynomials and exponentials. It provides one of the simplest ways to get a model from data.
- The best fit minimizes the sum of squares
.
- The data can have the following forms:
-
{v1,…,vn} equivalent to {{1,v1},…,{n,vn}} {{x1,v1},…,{xn,vn}} univariate data with values vi at coordinates xi {{x1,y1,v1},…} bivariate data with values vi and coordinates {xi,yi} {{x1,y1,…,v1},…} multivariate data with values vi at coordinates {xi,yi,…} - The design matrix m has elements that come from evaluating the functions at the coordinates,
. In matrix notation, the best fit minimizes the norm
where
and
.
- The functions fi should depend only on the variables {x,y,…}.
- The possible fit properties "prop" include:
-
"BasisFunctions" funs the basis functions "BestFit" the best fit linear combination of basis functions "BestFitParameters" the vector that gives the best fit
"Coordinates" {{x1,y1,…},…} the coordinates of vars in data "Data" data the data "DesignMatrix" m "FitResiduals" the differences between the model and the fit at the coordinates "Function" Function[{x,y,…},a1 f1+…+an fn] best fit pure function "PredictedResponse" fitted values for the data coordinates "Response" the response vector from the input data
{"prop1","prop2",…} several fit properties - Fit takes the following options:
-
NormFunction Norm measure of the deviations to minimize FitRegularization None regularization for the fit parameters WorkingPrecision Automatic the precision to use - With NormFunction->normf and FitRegularization->rfun, Fit finds the coefficient vector a that minimizes normf[{a.f(x1,y1,…)-v1,…,a.f(xk,yk,…)-vk}] + rfun[a].
- The setting for NormFunction can be given in the following forms:
-
normf a function normf that is applied to the deviations {"Penalty", pf} sum of the penalty function pf applied to each component of the deviations {"HuberPenalty",α} sum of Huber penalty function for each component {"DeadzoneLinearPenalty",α} sum of deadzone linear penalty function for each component - The setting for FitRegularization may be given in the following forms:
-
None no regularization rfun regularize with rfun[a] {"Tikhonov",λ} regularize with {"LASSO",λ} regularize with {"Variation",λ} regularize with {"TotalVariation",λ} regularize with {"Curvature",λ} regularize with {r1,r2,…} regularize with the sum of terms from r1,… - With WorkingPrecision->Automatic, exact numbers given as input to Fit are converted to approximate numbers with machine precision.

Examples
open allclose allBasic Examples (2)Summary of the most common use cases

https://wolfram.com/xid/0bueoc-pkoez
Find the line that best fits the data:

https://wolfram.com/xid/0bueoc-bwni2y


https://wolfram.com/xid/0bueoc-xf98n

Find the quadratic that best fits the data:

https://wolfram.com/xid/0bueoc-e9ytlr

Show the data with the two curves:

https://wolfram.com/xid/0bueoc-q721j

Find the best fit parameters given a design matrix and response vector:

https://wolfram.com/xid/0bueoc-cg8u3z

Scope (2)Survey of the scope of standard use cases
Here is some data defined with exact values:

https://wolfram.com/xid/0bueoc-ffef1x
Fit the data to a linear combination of sine functions using machine arithmetic:

https://wolfram.com/xid/0bueoc-kskd9b

Fit the data using 24-digit precision arithmetic:

https://wolfram.com/xid/0bueoc-eoygk2


https://wolfram.com/xid/0bueoc-bu988t

Here is some data in three dimensions:

https://wolfram.com/xid/0bueoc-z0oor
Find the plane that best fits the data:

https://wolfram.com/xid/0bueoc-ft4zol

Show the plane with the data points:

https://wolfram.com/xid/0bueoc-ew2lr9

Find the quadratic that best fits the data:

https://wolfram.com/xid/0bueoc-lmyy2s

The quadratic actually interpolates the data:

https://wolfram.com/xid/0bueoc-eozpzy

Generalizations & Extensions (1)Generalized and extended use cases

https://wolfram.com/xid/0bueoc-n5twaw
Fit to a quadratic. When coordinates are not given, the values are assumed to be paired up with 1, 2, …:

https://wolfram.com/xid/0bueoc-dn0c01


https://wolfram.com/xid/0bueoc-depuw


https://wolfram.com/xid/0bueoc-ctnwml

Options (6)Common values & functionality for each option
FitRegularization (2)
Tikhonov regularization controls the size of the best fit parameters:

https://wolfram.com/xid/0bueoc-bi6rp3

https://wolfram.com/xid/0bueoc-gtx2n3

Without the regularization, the coefficients are quite large:

https://wolfram.com/xid/0bueoc-oycysv

This can also be referred to by "L2" or "RidgeRegression":

https://wolfram.com/xid/0bueoc-flihrc


https://wolfram.com/xid/0bueoc-m48g3

LASSO (least absolute shrinkage and selection operator) regularization selects the most significant basis functions and gives some control of the size of the best fit parameters:

https://wolfram.com/xid/0bueoc-bh4kit

https://wolfram.com/xid/0bueoc-fe5xhw

Without the regularization, the coefficients are quite large:

https://wolfram.com/xid/0bueoc-d9ve2e

This can also be referred to by "L1":

https://wolfram.com/xid/0bueoc-wob9x

NormFunction (3)
Measure the "best" fit using the 1-norm:

https://wolfram.com/xid/0bueoc-13x60

https://wolfram.com/xid/0bueoc-fk5rbc


https://wolfram.com/xid/0bueoc-kc8k6k

Compare with the least squares (gold) and max norm (green) fits:

https://wolfram.com/xid/0bueoc-b4crna

Manipulate the norm p interactively:

https://wolfram.com/xid/0bueoc-b0o5rh

Use the Huber penalty function to balance the influence of outliers with least squares:

https://wolfram.com/xid/0bueoc-ix5yo

https://wolfram.com/xid/0bueoc-jisagw


https://wolfram.com/xid/0bueoc-em5xtl

Manipulate the parameter interactively:

https://wolfram.com/xid/0bueoc-c1paqd

Compute a weighted least squares fit:

https://wolfram.com/xid/0bueoc-lm6ziz

Applications (6)Sample problems that can be solved with this function
Compute a high-degree polynomial fit to data using a Chebyshev basis:

https://wolfram.com/xid/0bueoc-erorb

https://wolfram.com/xid/0bueoc-p75wm

https://wolfram.com/xid/0bueoc-dha2n0

Use regularization to stabilize the numerical solution to
where
is perturbed:

https://wolfram.com/xid/0bueoc-kvx1yd
The solution found by LinearSolve has very large entries:

https://wolfram.com/xid/0bueoc-68x39


Regularized solutions stay much closer to the solution of the unperturbed problem:

https://wolfram.com/xid/0bueoc-ebhhmm

https://wolfram.com/xid/0bueoc-e2ixp9

Use variation regularization to find a smooth approximation for an input signal:

https://wolfram.com/xid/0bueoc-jlvhc0
The output target is a step function:

https://wolfram.com/xid/0bueoc-bjvk5g

The input is determined from the output via convolution with :

https://wolfram.com/xid/0bueoc-bm6cdb
Without regularization, the predicated response matches the signal very closely, but the computed input has a lot of oscillation:

https://wolfram.com/xid/0bueoc-cbkyw6

With regularization of the variation, a much smoother approximation can be found:

https://wolfram.com/xid/0bueoc-n0ze4x

Regularization of the input size may also be included:

https://wolfram.com/xid/0bueoc-iksvlq

Smooth a corrupted signal using variation regularization:

https://wolfram.com/xid/0bueoc-judoj

Plot the tradeoff between the norm of the residuals and the norm of the variation:

https://wolfram.com/xid/0bueoc-ggm323

https://wolfram.com/xid/0bueoc-lc0ll

https://wolfram.com/xid/0bueoc-erxznz

Choose a value of near where the curve bends sharply:

https://wolfram.com/xid/0bueoc-fwoy0h

https://wolfram.com/xid/0bueoc-j0tcm1

Use total variation regularization to smooth a corrupted signal with jumps:

https://wolfram.com/xid/0bueoc-l19w6

Regularize with the parameter :

https://wolfram.com/xid/0bueoc-bn9ck2

https://wolfram.com/xid/0bueoc-61xvt

A smaller value of gives less smoothing but the residual norm is smaller:

https://wolfram.com/xid/0bueoc-dne8jz

https://wolfram.com/xid/0bueoc-cvu134


https://wolfram.com/xid/0bueoc-lin2e0

Use LASSO (L1) regularization to find a sparse fit (basis pursuit):

https://wolfram.com/xid/0bueoc-mhy4vc

https://wolfram.com/xid/0bueoc-h71gfl

The goal is to approximate the signal with just of few of the thousands of Gabor basis functions:

https://wolfram.com/xid/0bueoc-dgq4qs

With , a fit is found using only 41 of the basis elements:

https://wolfram.com/xid/0bueoc-ejwzp7


https://wolfram.com/xid/0bueoc-cl20h8

Once the important elements of the basis have been found, error can be reduced by finding the least squares fit to these elements:

https://wolfram.com/xid/0bueoc-pz6wlv

With a smaller value , a fit is found using more of the basis elements:

https://wolfram.com/xid/0bueoc-bph0ya


https://wolfram.com/xid/0bueoc-eap6v

Properties & Relations (5)Properties of the function, and connections to other functions
Fit gives the best fit function:

https://wolfram.com/xid/0bueoc-btxa7

LinearModelFit allows for extraction of additional information about the fitting:

https://wolfram.com/xid/0bueoc-bdsgz8


https://wolfram.com/xid/0bueoc-h4tokg

Extract additional results and diagnostics:

https://wolfram.com/xid/0bueoc-mir4j5


https://wolfram.com/xid/0bueoc-c0mr00
This is the sum of squares error for a line a+b x:

https://wolfram.com/xid/0bueoc-ohfkn3

Find the minimum symbolically:

https://wolfram.com/xid/0bueoc-mwyj75


https://wolfram.com/xid/0bueoc-d5830k

These are the coefficients given by Fit:

https://wolfram.com/xid/0bueoc-drti8f

The exact coefficients may be found by using WorkingPrecision->Infinity:

https://wolfram.com/xid/0bueoc-h1a4zt

This is the sum of squares error for a quadratic a+b x+c x^2:

https://wolfram.com/xid/0bueoc-bfu974

Find the minimum symbolically:

https://wolfram.com/xid/0bueoc-m7jdrn


https://wolfram.com/xid/0bueoc-350k1

These are the coefficients given by Fit:

https://wolfram.com/xid/0bueoc-o768e6

When a polynomial fit is done to a high enough degree, Fit returns the interpolating polynomial:

https://wolfram.com/xid/0bueoc-cewkv1

The result is consistent with that given by InterpolatingPolynomial:

https://wolfram.com/xid/0bueoc-bjeykz


https://wolfram.com/xid/0bueoc-e99f7r

Fit will use the time stamps of a TimeSeries as variables:

https://wolfram.com/xid/0bueoc-55rjns

https://wolfram.com/xid/0bueoc-8vunzb


https://wolfram.com/xid/0bueoc-xiogrn

https://wolfram.com/xid/0bueoc-c50i1w

Rescale the time stamps and fit again:

https://wolfram.com/xid/0bueoc-hiel46


https://wolfram.com/xid/0bueoc-7d00cg


https://wolfram.com/xid/0bueoc-emgg00


https://wolfram.com/xid/0bueoc-siexdh

Fit acts pathwise on a multipath TemporalData:

https://wolfram.com/xid/0bueoc-1mqe20

Possible Issues (2)Common pitfalls and unexpected behavior
Here is some data from a random perturbation of a Gaussian:

https://wolfram.com/xid/0bueoc-dlm1ug

This is a function that gives the standard basis for polynomials:

https://wolfram.com/xid/0bueoc-yaul3

https://wolfram.com/xid/0bueoc-l811rv

Show the fits computed for successively higher-degree polynomials:

https://wolfram.com/xid/0bueoc-nok1m

The problem is that the coefficients get very small for higher powers:

https://wolfram.com/xid/0bueoc-ifqj1b

Giving the basis in terms of scaled and shifted values helps with this problem:

https://wolfram.com/xid/0bueoc-o0m70g

Reconstruct a sparse signal from compressed sensing data:

https://wolfram.com/xid/0bueoc-oq5p22

Compressed sensing consists of multiplying the signal with an matrix with
so only data of length
has to be transmitted. If
is large enough, a random matrix with independently identically distributed normal entries will satisfy the restricted isometry property, and the original signal can be recovered with very high probability:

https://wolfram.com/xid/0bueoc-692an


https://wolfram.com/xid/0bueoc-f6iz6s
The samples to send are constructed by multiplication of the signal with the matrix :

https://wolfram.com/xid/0bueoc-chzui8
Reconstruction can be done by minimizing for all possible solutions of
:

https://wolfram.com/xid/0bueoc-wjo92


https://wolfram.com/xid/0bueoc-egwlkq

Even though the minimization is solved by linear optimization, it is relatively slow because all the constraints are equality constraints. The solution can be found much faster using basis pursuit (L1 regularization):

https://wolfram.com/xid/0bueoc-c6z3ug

This gives the basis elements. To find the best solution, solve the linear equations corresponding to those components:

https://wolfram.com/xid/0bueoc-bylut


https://wolfram.com/xid/0bueoc-junxc6

Wolfram Research (1988), Fit, Wolfram Language function, https://reference.wolfram.com/language/ref/Fit.html (updated 2019).
Text
Wolfram Research (1988), Fit, Wolfram Language function, https://reference.wolfram.com/language/ref/Fit.html (updated 2019).
Wolfram Research (1988), Fit, Wolfram Language function, https://reference.wolfram.com/language/ref/Fit.html (updated 2019).
CMS
Wolfram Language. 1988. "Fit." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2019. https://reference.wolfram.com/language/ref/Fit.html.
Wolfram Language. 1988. "Fit." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2019. https://reference.wolfram.com/language/ref/Fit.html.
APA
Wolfram Language. (1988). Fit. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Fit.html
Wolfram Language. (1988). Fit. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Fit.html
BibTeX
@misc{reference.wolfram_2025_fit, author="Wolfram Research", title="{Fit}", year="2019", howpublished="\url{https://reference.wolfram.com/language/ref/Fit.html}", note=[Accessed: 25-April-2025
]}
BibLaTeX
@online{reference.wolfram_2025_fit, organization={Wolfram Research}, title={Fit}, year={2019}, url={https://reference.wolfram.com/language/ref/Fit.html}, note=[Accessed: 25-April-2025
]}