type TMtxMultiNonLinReg = class(TMtxComponent);
Performs multiple-nonlinear regression.
How to use TMtxMultiNonLinReg component?
1. Drop a TMtxMultiNonLinReg component on the form. 2. Define TMtxMultiNonLinReg.X the vector of independent variables. 3. Define TMtxMultiNonLinReg.Y the vector of dependent variables. Make sure that X and Y have the same length. 4. Define initial estimates for TMtxMultiNonLinReg.B regression parameters. 5. (Optionaly) define TMtxMultiNonLinReg.Weights. If you'll use weights, set TMtxMultiNonLinReg.UseWeights property to true. 6. Define regression function. 7. (Optionally) define the derivative procedure. If you don't define derivative procedure then the numeric approximation will be used to calculate the derivative at specific point. 8. Define the optimization method. Depending on your function different methods will give better/worse results. 9. (Optionally) define desired tolerance for result. 10. Define the maximum number of steps for optimization method. Internal optimization method will stop when number of iterations exceeds MaxIter or internal minimum precision is within Tolerance. 11. Verbose property, if assigned, stores Fun, evaluated at each iteration step. Optionally, you can also assign TOptControl object to the Verbose property. This allows the optimization procedure to be interrupted from another thread and optionally also allows logging and iteration count monitoring.
Results:
1. TMtxMultiNonLinReg.B : Regression coefficients estimates. 2. TMtxMultiNonLinReg.YCalc : Fitted Y values.
The component internally performs vectorized optimization process. It can also be used to speed-up nonlinear regression with only one independent variable by 10-20x.
Examples
// regress function
procedure Rat43(const B: TVec; const X: TVecList; const y: TVec);
begin
// Rat43 := B[0] / Power((1.0 + Exp(B[1]-B[2]*X)),1/B[3]);
y.Mul(x[0], -B[2]);
y.Add(B[1]);
y.Exp();
y.Add(1);
y.Power(1/B[3]);
y.DivideBy(B[0]);
end;
procedure TfrmNonLinTest.FormCreate(Sender: TObject);
var MtxNonLinReg: TMtxMultiNonLinReg;
begin
MtxNonLinReg := TMtxMultiNonLinReg.Create;
try
// Load data - independent variable
MtxNonLinReg.X.Add(); //add one variable
MtxNonLinReg.X[0].SetIt(false,[9.000, 14.000, 21.000, 28.000,
42.000, 57.000, 63.000, 70.000,
79.000]);
// Load data - dependent variable
MtxNonLinReg.Y.SetIt(false,[8.930, 10.800, 18.590, 22.330,
39.350, 56.110, 61.730, 64.620,
67.080]);
// Initial estimates for regression coefficients
MtxNonLinReg.b.SetIt(false,[100,10,1,1]);
// setup optimization parameters
MtxNonLinReg.Tolerance := 1.0e-6; // 6 digits should do the trick
MtxNonLinReg.GradTolerance := 1.0e-3 // 3 digits
MtxNonLinReg.MaxIterations := 400;
MtxNonLinReg.RegressFunction := Rat43; // regression function
// Marquardt method
MtxNonLinReg.OptMethod := optMarquardt;
MtxNonLinReg.Recalc;
// MtxNinLinReg.b now stores calculated regression parameter estimates
finally
MtxNonLinReg.Free;
end;
end;
Properties
| Name | Type | Description |
|---|---|---|
| AutoUpdate | Boolean | If true then changing any of the TMtxMultiNonLinReg properties will trigger the TMtxMultiNonLinReg.Recalc method. |
| B | TVec | Stores the regression coefficients. |
| BlockAssign | Boolean | Block streaming of specific properties when storing only a "template". |
| DeriveProcedure | TMultiDeriveProc | Defines derivatives of regression function. |
| Dirty | Boolean | When any of the TMtxNonLinReg properties changes, this property is automatically set to true. |
| EditorActive | Boolean | Returns True, if the component editor is displayed. |
| GradTolerance | Double | Defines minimal allowed gradient C-Norm. |
| MaxIteration | Integer | Defines one of the conditions for terminating the regression coefficient calculation. |
| OptMethod | TOptMethod | Optimization method used. |
| Reference | TReferenceList | Stores a list of components that have to be notified, when this component is destroyed. |
| RegressFunction | TMultiRegressFun | Defines the regression function of several regression parameters. |
| SoftSearch | Boolean | Defines line search algorithm for Quasi-Newton and Conjugate methods. |
| StopReason | TOptStopReason | Returns the reason why regression parameters calculation stopped. |
| Tolerance | Double | Defines one of the conditions for terminating the regression coefficient calculation. |
| UseWeights | Boolean | Weighted non-linear regression. |
| Verbose | TStrings | If not nil then the optimization method uses it for logging each optimization step. |
| Weights | TVec | Weights. |
| X | TVecList | Vector of independant variables. |
| Y | TVec | Vector of dependant variables. |
| YCalc | TVec | Vector of calculated values. |
Methods
| Name | Description |
|---|---|
| AllowStreaming | Returns the negatated value of BlockAssign. |
| Assign | Assign values of all published properties from Source. |
| AssignTemplate | Assign values of "template-like" published properties from Source. |
| EditorClass | To be overriden in descendanr classses. |
| LoadFromStream | Load the component from Src stream. |
| LoadTemplateFromFile | Load a template from file. |
| LoadTemplateFromStream | Load a template from stream. |
| Recalc | Triggers non-linear regression recalculation. |
| Reset | Reset is called after loading the data from stream or file. |
| SaveTemplateToFile | Save template to file. |
| SaveTemplateToStream | Save template to stream. |
| SaveToStream | Save the component to Dst stream. |