Nonlinear Curve Fitting | Extreme Optimization Numerical Libraries for .NET Professional |

Nonlinear curve fitting extends linear curve fitting to curves whose parameters appear in the function expression in arbitrary ways, not just linearly. Almost any function that can be expressed in closed form can be used for nonlinear curve fitting. With this increased power comes the drawback that it is more difficult to estimate the parameters. It may not be possible to guarantee the best solution is found.

When defining what nonlinear fitting means for arbitrary inputs, the key observation is that in the definition of a nonlinear curve,

f(x) = f(a_{1}, a_{2}, ...,
a_{n}; x)
,

the variable x can be of any type. The actual fitting algorithm only does calculations in terms of the curve parameters and in terms of functions defined by the user.

The NonlinearCurveFitter

The nonlinear function is defined by a delegate that maps a T value to a real number. Likewise, the partial derivatives with respect to the curve parameters must be supplied as a delegate that maps a T value to a real vector. This delegate must also take a second argument that is a vector to store the result.

The data is supplied as Vector

By default, the fit is unweighted. All weights are set equal to 1. Weights can be specified in one of two ways. The first way is to set the WeightVector property to a vector with as many components as there are data points. Each component of the vector specifies the weight of the corresponding data point.

If error estimates for the Y values are available, then the
GetWeightVectorFromErrors of the
WeightFunctions
class can be used to construct a vector with the weights
that correspond to the errors. Its only argument is a
Vector

The Optimizer property gives access to the Levenberg-Marquardt optimizer that is used to compute the least squares solution. You can set tolerances and control convergence as set ut in the chapter on optimization.

The Fit method performs the actual least
squares calculation, and adjusts the parameters of the Curve to correspond to the least squares fit. The
BestFitParameters property returns a
Vector

The Optimizer property also gives access to details of the calculation. It is useful to inspect its Status property to make sure the algorithm ended normally. A value other than Converged indicates some kind of problem with the algorithm.

In the first example, we fit dose response data to a four parameter logistic curve.

Vector dose = Vector.Create(/*...*/); Vector response = Vector.Create(/*...*/); Vector error = Vector.Create(/*...*/); FourParameterLogisticCurve doseResponseCurve = new FourParameterLogisticCurve(); NonlinearCurveFitter fitter = new NonlinearCurveFitter(); fitter.Curve = doseResponseCurve; fitter.XValues = dose; fitter.YValues = response; fitter.WeightVector = WeightFunctions.GetWeightVectorFromErrors(error); fitter.Fit(); Console.WriteLine("Initial value: {0,10:F6} +/- {1:F4}", doseResponseCurve.InitialValue, s[0]); Console.WriteLine("Final value: {0,10:F6} +/- {1:F4}", doseResponseCurve.FinalValue, s[1]); Console.WriteLine("Center: {0,10:F6} +/- {1:F4}", doseResponseCurve.Center, s[2]); Console.WriteLine("Hill slope: {0,10:F6} +/- {1:F4}", doseResponseCurve.HillSlope, s[3]);

Two things are worth noting. One is the use of the GetWeightVectorFromErrors method to transform the errors of the observations into their corresponding weights. Also, the FourParameterLogisticCurve class exposes properties that name the curve properties. Many of the predefined curves define such named parameters.

Copyright Â© 2004-2015,
Extreme Optimization. All rights reserved.

*Extreme Optimization,* *Complexity made simple*, *M#*, and *M
Sharp* are trademarks of ExoAnalytics Inc.

*Microsoft*, *Visual C#, Visual Basic, Visual Studio*, *Visual
Studio.NET*, and the *Optimized for Visual Studio* logo

are
registered trademarks of Microsoft Corporation.