MATLAB: What Weighted-Least-Squares Fitting capabilities are available in MATLAB 6.1 (R12.1) and the Toolboxes

Curve Fitting Toolboxfitlinearnonnonlinearoutliersregressionrobustweighted

Currently, the presence of data outliers can create an undesirable fit. Because the outlier lies far away from the true pattern of data, it induces error to the true fit. A workaround to this problem would be to minimize the weight(s) of such outlier(s).

Best Answer

In MATLAB, the LSCOV function can perform weighted-least-square regression.
 
x = lscov(A,b,w)
where w is a vector length m of real positive weights, returns the weighted least squares solution to the linear system A*x = b, that is, x minimizes (b - A*x)'*diag(w)*(b - A*x). w typically contains either counts or inverse variances.
In addition, there are three toolboxes you can use to implement weights for your fits:
==================
1. Statistics Toolbox:
==================
Weighted linear regression in the Statistics Toolbox is part of the ROBUSTFIT function,
 
B = ROBUSTFIT(X,Y,'WFUN',TUNE,'CONST')
uses the weighting function 'WFUN' and tuning constant TUNE.
'WFUN' can be any of 'andrews' 'bisquare', 'cauchy', 'fair', 'huber','logistic', 'talwar', 'welsch'.
As an alternative to specifying one of the named weight functions shown above, you can also write your own weight function (wfun) that takes a vector of scaled residuals as input and produces a vector of weights as output.
For documentation on ROBUSTFIT, you can type "doc robustfit" (without quotes) at the MATLAB command prompt or view the online documentation found at the URL below:
For MATLAB versions prior to 7.1 (R14SP3), we do not support a non-linear weighted least-square fit in the Statistics Toolbox.
====================
2. Curve Fitting Toolbox
====================
We have a more general weighted least square regression capability in the Curve Fitting Toolbox that supports any fit, linear and non-linear.
The weight is part of the options to the Fit, and is supplied using the function FITOPTIONS. Go to the following URL for documentation on FITOPTIONS:
In the Curve Fitting Toolbox, the weight can actually be any vector of weights associated with the response data.
Follow this link for more information about this Toolbox:
====================
3. Optimization Toolbox.
====================
LSQNONLIN and LSQCURVEFIT are least-squares solvers in the Optimization Toolbox that can be used to fit equations to your data.
In order to use LSQNONLIN to do a weighted least square fit, you will need to have an equation into which you want to fit your data. For an example on weighted least squares fitting using LSQNONLIN, see the article <http://jp.mathworks.com/matlabcentral/answers/95519-how-can-i-use-the-lsqnonlin-function-within-the-optimization-toolbox-to-obtain-the-weighted-least-sq "How can I use the LSQNONLIN function within the Optimization Toolbox to obtain the weighted least squares fit?">.