Dew Stats Master .NET
TMtxNonLinReg Component
Summary
Performs nonlinear regression.

Unit
StatTools

Hierarchy
TMtxNonLinReg

Subclasses
None

Description
How to use TMtxNonLinReg component?
1) Drop a TMtxNonLinReg component on the form.
2) Define X the vector of independent variables.
3) Define Y the vector of dependent variables. Make sure that X and Y have the same length.
4) Define initial estimates for B regression parameters.
5) (Optionaly) define Weights. If you'll use weights, set 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.

Results:
1) B : Regression coefficients estimates.
2) YCalc : Fitted Y values.

Categories
Regression routines.

Example 1

In the following example we use the TMtxNonLinReg component to fit generated data to non-linear function B[0]/Power((1.0 + Exp(B[1]-B[2]*X)),1/B[3]). In this example exact derivate procedure is not used - algorithm uses numerical derivatives:

// regress function function Rat43(B: TVec; X: TSample): TSample; begin Result := B[0] / Power((1.0 + Exp(B[1]-B[2]*X)),1/B[3]); end; procedure TfrmNonLinTest.FormCreate(Sender: TObject); var MtxNonLinReg: TMtxNonLinReg; begin MtxNonLinReg := TMtxNonLinReg.Create; try // Load data - independent variable MtxNonLinReg.X.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 // Nelder-Marquardt method MtxNonLinReg.OptMethod := optMarquardt; MtxNonLinReg.Recalc; // MtxNinLinReg.b now stores calculated regression parameter estimates finally MtxNonLinReg.Destroy; end; end;
#include "MtxVecCpp.h" #include "StatTools.hpp" #include "Math387.hpp" double Rat43(TVec* b,double x) { double* B = b->PValues(0); return b[0] / Power((1.0 + Exp(b[1]-b[2]*x)),1/b[3]); } void __fastcall Example() { TMtxNonLinReg *nlr = new TMtxNonLinReg(this); try { // Load data - independent variable nlr->X->SetIt(false,OPENARRAY(TSample,9.000, 14.000, 21.000, 28.000, 42.000, 57.000, 63.000, 70.000, 79.000))); // Load data - dependent variable nlr->Y->SetIt(false,OPENARRAY(TSample,(8.930, 10.800, 18.590, 22.330, 39.350, 56.110, 61.730, 64.620, 67.080))); // Initial estimates for regression coefficients nlr->b->SetIt(false,OPENARRAY(TSample,(100,10,1,1))); // setup optimization parameters nlr->Tolerance = 1.0e-6; // 6 digits should do the trick nlr->GradTolerance = 1.0e-3 // 3 digits nlr->MaxIterations = 400; nlr->RegressFunction = Rat43; // regression function // Nelder-Marquardt method nlr->OptMethod = optMarquardt; nlr->Recalc(); // MtxNinLinReg->b now stores calculated regression parameter estimates } __finally { nlr->Free(); } }
using Dew.Math; using Dew.Stats; using Dew.Stats.Units; using System; namespace Dew.Examples { private double Rat43(TVec b, double x) { return b[0] / Math.Pow((1.0 + Math.Exp(b[1]-b[2]*x)),1/b[3]); } private void Example(StatTools.TMtxNonLinReg nlr) { // Load data - independent variable nlr.X.SetIt(false,new double[] {9.000, 14.000, 21.000, 28.000, 42.000, 57.000, 63.000, 70.000, 79.000}); // Load data - dependent variable nlr.Y.SetIt(false,new double[] {8.930, 10.800, 18.590, 22.330, 39.350, 56.110, 61.730, 64.620, 67.080}); // Initial estimates for regression coefficients nlr.b.SetIt(false,new double[] {100,10,1,1}); // setup optimization parameters nlr.Tolerance = 1.0e-6; // 6 digits should do the trick nlr.GradTolerance = 1.0e-3 // 3 digits nlr.MaxIterations = 400; nlr.RegressFunction = Rat43; // regression function // Nelder-Marquardt method nlr.OptMethod = Dew.Math.Units.Optimization.optMarquardt; nlr.Recalc(); // MtxNinLinReg->b now stores calculated regression parameter estimates } }


Properties

 Name  Summary 
AutoUpdate  
B Stores the regression coefficients. 
DeriveProcedure Defines derivatives of regression function. 
Dirty  
GradTolerance Defines minimal allowed gradient C-Norm. 
MaxIteration Defines one of the conditions for terminating the regression coefficient calculation. 
OptMethod Optimization method used. 
RegressFunction Defines the regression function of several regression parameters. 
SoftSearch Defines line search algorithm for Quasi-Newton and Conjugate methods. 
StopReason Returns the reason why regression parameters calculation stopped. 
Tolerance Defines one of the conditions for terminating the regression coefficient calculation. 
UseWeights Weighted non-linear regression. 
Weights Weights. 
X Vector of independant variables. 
Y Vector of dependant variables. 
YCalc Vector of calculated values. 

Methods

 Name  Summary 
Loaded  
Recalc Triggers non-linear regression recalculation. 

Copyright 2008 Dew Research
http://www.dewresearch.com