Using the CLsqFit Class
The least square method minimizes the squared difference between an objective function (the function being fit), and a collection of sample data. The term "rubber function" is sometimes used because the least squares process adjusts the shape of the basis function like a rubber sheet in order to minmize 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 an objective function. The linear method 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-dimensional 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 are deviant from the majority of the data. Statistically speaking, the outliers are not drawn from the same population as the 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.
The following procedure fits n-dimensional data using sample data that are either single values or RGB values:
Create a CLsqFit object using the new method or new_lsqfit function.
Choose a basis function to fit: Adopt the default n-dimensional polynomial or use SetBasisFunc to specify it as a hyperplane or a user-defined function.
Specify the number of coefficients to fit using SetNumCoefs.
Add data to the sample using AddPt or AddPtWt for each point.
Execute the fit using Fit.
Fetch results using GetSigmaFit, GetCoef, GetCoefErr, GetCovar, GetResid, and other methods.
After doing the basic least squares procedure outlined above, you can add many enhancements to work with the data or refine the results:
Set one or more of the fit coefficients to specified values using ForceCoef.
Change the weight of sample points using SetPtWt.
Delete sample points using DeletePt,
Add more sample points using AddPt or AddPtWt.
Enable automatic rejection of deviant points using DoRejection.
Change the basis function or the number of fit coefficients using SetBasisFunc and SetNumCoefs.
After making any such changes, use Fit to recalculate the fit.
CLsqFit class, Using CLsqFit with Image Pixels
Mira Pro x64 Script User's Guide, Copyright Ⓒ 2023 Mirametrics,
Inc. All Rights Reserved.