Fit
Fit[data,{f_{1},…,f_{n}},{x,y,…}]
finds a fit a_{1} f_{1}+…+a_{n} f_{n} to a list of data for functions f_{1},…,f_{n} of variables {x,y,…}.
Fit[{m,v}]
finds a fit vector a that minimizes for a design matrix m.
Fit[…,"prop"]
specifies what fit property prop should be returned.
Details and Options
 Fit is also known as linear regression or least squares fit.
 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:

{v_{1},…,v_{n}} equivalent to {{1,v_{1}},…,{n,v_{n}}} {{x_{1},v_{1}},…,{x_{n},v_{n}}} univariate data with values v_{i} at coordinates x_{i} {{x_{1},y_{1},v_{1}},…} bivariate data with values v_{i} and coordinates {x_{i},y_{i}} {{x_{1},y_{1},…,v_{1}},…} multivariate data with values v_{i} at coordinates {x_{i},y_{i},…}  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 f_{i} 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" {{x_{1},y_{1},…},…} 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,…},a_{1} f_{1}+…+a_{n} f_{n}] best fit pure function "PredictedResponse" fitted values for the data coordinates "Response" the response vector from the input data {"prop_{1}","prop_{2}",…} 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(x_{1},y_{1},…)v_{1},…,a.f(x_{k},y_{k},…)v_{k}}] + 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 {r_{1},r_{2},…} regularize with the sum of terms from r_{1},…  With WorkingPrecision>Automatic, exact numbers given as input to Fit are converted to approximate numbers with machine precision. »
Examples
open allclose allBasic Examples (2)
Generalizations & Extensions (1)
Options (6)
FitRegularization (2)
Tikhonov regularization controls the size of the best fit parameters:
Without the regularization, the coefficients are quite large:
This can also be referred to by "L2" or "RidgeRegression":
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:
Without the regularization, the coefficients are quite large:
Applications (6)
Compute a highdegree polynomial fit to data using a Chebyshev basis:
Use regularization to stabilize the numerical solution to where is perturbed:
The solution found by LinearSolve has very large entries:
Regularized solutions stay much closer to the solution of the unperturbed problem:
Use variation regularization to find a smooth approximation for an input signal:
The output target is a step function:
The input is determined from the output via convolution with :
Without regularization, the predicated response matches the signal very closely, but the computed input has a lot of oscillation:
With regularization of the variation, a much smoother approximation can be found:
Regularization of the input size may also be included:
Smooth a corrupted signal using variation regularization:
Plot the tradeoff between the norm of the residuals and the norm of the variation:
Choose a value of near where the curve bends sharply:
Use total variation regularization to smooth a corrupted signal with jumps:
Regularize with the parameter :
A smaller value of gives less smoothing but the residual norm is smaller:
Use LASSO (L1) regularization to find a sparse fit (basis pursuit):
The goal is to approximate the signal with just of few of the thousands of Gabor basis functions:
With , a fit is found using only 41 of the basis elements:
Once the important elements of the basis have been found, error can be reduced by finding the least squares fit to these elements:
With a smaller value , a fit is found using more of the basis elements:
Properties & Relations (5)
Fit gives the best fit function:
LinearModelFit allows for extraction of additional information about the fitting:
Extract additional results and diagnostics:
This is the sum of squares error for a line a+b x:
Find the minimum symbolically:
These are the coefficients given by Fit:
The exact coefficients may be found by using WorkingPrecision>Infinity:
This is the sum of squares error for a quadratic a+b x+c x^2:
Find the minimum symbolically:
These are the coefficients given by Fit:
When a polynomial fit is done to a high enough degree, Fit returns the interpolating polynomial:
The result is consistent with that given by InterpolatingPolynomial:
Fit will use the time stamps of a TimeSeries as variables:
Rescale the time stamps and fit again:
Fit acts pathwise on a multipath TemporalData:
Possible Issues (2)
Here is some data from a random perturbation of a Gaussian:
This is a function that gives the standard basis for polynomials:
Show the fits computed for successively higherdegree polynomials:
The problem is that the coefficients get very small for higher powers:
Giving the basis in terms of scaled and shifted values helps with this problem:
Reconstruct a sparse signal from compressed sensing data:
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:
The samples to send are constructed by multiplication of the signal with the matrix :
Reconstruction can be done by minimizing for all possible solutions of :
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):
This gives the basis elements. To find the best solution, solve the linear equations corresponding to those components: