CLsqFit:Class Description CLsqFit:Basis Functions

Using the CLsqFit Class


Overview of Least Squares Estimation

The least square method minimizes the squared difference between an objective function, or so-called "rubber function", and a collection of sample data. The term "rubber function" is often used because the least squares estimation process adjusts the shape of the chosen function by varying its coefficient values until it provides the "best fit" to the sample data. The best fit is evaluated in terms of the variance between the function and the sample data. Each sample point consists of 1 or more coordinates, an observed value at those coordinates, and an optional weight.

This class uses the linear least squares method for estimating the coefficients of the "rubber function". This method which requires that the coefficients and independent variables be separated into the dot product of a coefficient vector and a basis vector. Said another way, the function being fit can be written as an arithmetic series of terms in which each term contains only 1 coefficient. For example, y = a[1] + a[2]x is the equation of a line and is linear in the coefficients a. Conversely, y = a[1] exp (a[2] x) is not linear because more than one coefficient appears in the term a[1] exp (a[2] x). Since the latter equation is not linear, it cannot be fit using linear least squares. However, since the equation of a line is linear in terms of the coefficients a, it can be separated into the dot product of a coefficient vector (a[1], a[2]) and a basis vector (1, x ). The least squares estimate determines the coefficients (a[1], a[2]) which minimize the squared error between the y values estimated using (a[1], a[2]) and the value y which is observed at given coordinates. In general, x may be a vector containing up to 10 coordinates (in other words, the basis may have up to 10 dimensions). For example, fitting a function to a 2-dimensional surface, such as the intensity of a 2-d image, involves the 2 coordinates x and y, hence the least squares basis would have 2 dimensions.

Oftentimes, the data sample contains outlying values that do not follow the same "rubber function" as the majority of the data. Statistically speaking, the outliers are not drawn from the same population as teh majority of the data. These "bad" data may be automatically excluded from the fit by enabling data rejection. You also may adjust the weights of sample points or manually delete outliers from use in the fitting process. Thus there are 3 ways to exclude a point from the fitting process: 1) set its weight to zero, 2) delete it from the fit, or 3) enable data rejection and allow it to be automatically rejected. A deleted point can later be reinstated by undeleting it. For a further discussion, see Rejecting Outliers from the Fit.

After a fit is computed, any number of operations be be undertaken with the results. For example, you may wish to examine the coefficient values and their uncertainties, or statistical errors. You might also wish to evaluate the fit to predict the function value at new coordinates. The independence between the calculated coefficients can be assessed by examining the covariance matrix. The overall rms uncertainty of the fit is given by the fit standard deviation. The difference between the observed and predicted values of the sample data, known as the residual, also may be obtained to evaluate the quality of each sample point.

Basic Fitting Procedure

The following procedure fits n-dimensional data using sample data that are either single values or RGB values:

  1. Create a CLsqFit object using the new method.

  2. Choose a basis function to fit: Either adopt the default n-dimensional polynomial or use SetBasisFunc to specify it.

  3. Specify the number of coefficients to fit using SetNumCoefs.

  4. Add data to the sample using AddPt or AddPtWt for each point.

  5. Execute the fit using Fit.

  6. Fetch results using GetSigmaFit, GetCoef, GetCoefErr, GetCovar, GetResid, and other methods.

Extending the basic procedure

After doing the basic least squares procedure outlined above, you can add many enhancements to work with the data or refine the results:

After making any such changes, use Fit to recalculate the fit.

Related Topics

CLsqFit class, Using CLsqFit with Image Pixels