Extreme Optimization >
User's Guide >
Mathematics Library >
Curve Fitting >
Linear Curve Fitting
Extreme Optimization User's Guide
User's Guide
Up: Curve Fitting Next: Nonlinear Curve Fitting Previous: Curve Fitting Contents
Linear Curve Fitting
In the context of curve fitting, a linear curve is a curve that has a linear dependence on the curve parameters.
Examples of linear curves are: lines, polynomials, Chebyshev series, and any linear combination of a set of curves.
The linearity greatly simplifies the calculations, as the solution can be expressed in terms of simple linear
algebra.
Mathematical background
Most curve fitting is done using linear combinations of a set of basis functions. A linear combination of
a set of functions, f1, f2..., fn is a function
whose value is given by
f(x) = a1f1(x) +
a2f2(x) + ... + anfn(x)
where a1, a2..., an are the
coefficients. The advantage of using this type of approximation is that the math is relatively simple. The
solution can be found by solving a standard linear algebra problem. In many cases, this can be done very
efficiently.
The basis functions f1, f2..., fn that are
combined to create the approximation together form a function basis. A linear combination is therefore
defined by the function basis and the coefficients of the basis functions in the combination.
Perhaps the most well-known example of a linear combination of basis functions is provided by polynomials. A
polynomial is a function that is a combination of the argument raised to different integer powers:
f(x) = a0 + a1x + ... +
anxn
The integer powers are the basis functions. Chebyshev polynomials form an alternate basis for the polynomials.
They can be defined mathematically by the identity
Tn(cos x) = cos nx
Their mathematical properties make them particularly suited for approximating functions.
In the Extreme Optimization Numerical Libraries for .NET, function bases are implemented by the
abstract FunctionBasis class and its derived
classes. Linear combinations are implemented using the LinearCombination class and its derived classes, including
Polynomial and ChebyshevSeries. The table below summarizes the classes
that implement function bases and the corresponding linear combinations.
Static curve fitting methods
Many classes expose static methods that allow you to obtain a least squares fit. The main drawback of the methods
discussed in this section is that they don't offer much control over the calculation of the least squares fit. There
is also no easy way to obtain information about the quality of the fit. However, they may be useful because they
offer a very quick way to get a result.
The LeastSquaresFit method of the
Polynomial class constructs a Polynomial that is the least squares fit of a specified degree
through a given set of points.
| C# | Copy Code |
double[] xValues = new double[7];
double[] yValues = new double[7];
double angle = 0;
for(int index = 0; index < 7; index++)
{
xValues[index] = Math.Cos(angle);
yValues[index] = -Math.Sin(angle);
angle = angle + Constants.Pi / 6;
}
Polynomial p = Polynomial.LeastSquaresFit(xValues, yValues, 4); |
| Visual Basic | Copy Code |
Dim xValues As Double() = New Double(7)
Dim yValues As Double() = New Double(7)
Dim angle As Double = 0
Dim index As Integer
For index = 0 to 6
xValues(index) = Math.Cos(angle)
yValues(index) = -Math.Sin(angle)
angle = angle + Constants.Pi / 6
Next
Dim p As Polynomial = Polynomial.LeastSquaresFit(xValues, yValues, 4); |
A similar method is available for the Line and ChebyshevSeries classes.
You can also create a function basis, and obtain the least squares fit using the functions in the function basis
for a set of data points using the LeastSquaresFit method. It returns
a LinearCombination curve. This allows you
to fit data to an arbitrary set of functions. You can also specify weights for each of the data points. The
weights specify how much weight each data point should have in the sum of squares that is to be minimized.
The following example fits a set of data points to a combination of the sine, cosine, and exponential
functions.
| C# | Copy Code |
Double[] xValues = new Double[] {0. 1, 2, 3, 4};
Double[] yValues = new Double[] {4, 1, 4, 10, 8};
GeneralFunctionBasis basis = new GeneralFunctionBasis(
new RealFunction(Math.Sin),
new RealFunction(Math.Cos),
new RealFunction(Math.Exp));
LinearCombination f = basis.LeastSquaresFit(xValues, yValues); |
| Visual Basic | Copy Code |
Dim xValues As Double() = New Double() {0, 1, 2, 3, 4}
Dim yValues As Double() = New Double() {4, 1, 4, 10, 8}
Dim basis As GeneralFunctionBasis = new GeneralFunctionBasis( _
new RealFunction(AddressOf Math.Sin), _
new RealFunction(AddressOf Math.Cos), _
new RealFunction(AddressOf Math.Exp))
Dim f As LinearCombination = basis.LeastSquaresFit(xValues, yValues) |
The LinearCurveFitter class
The LinearCurveFitter class performs a linear
least squares fit. It offers greater control over the procedure, and gives more extensive results.
To perform the fit, a LinearCurveFitter needs data points, and a curve to fit. You must set the
Curve property to an instance of a LinearCombination object. A LinearCombination
object can represent any combination of functions. Other objects, for example of type Line, Polynomial
or ChebyshevSeries are allowed, as they inherit from
LinearCombination.
The data is supplied as Vector objects. The
XValues and YValues properties specify the X and Y values of the data
points, respectively.
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.
Alternatively, the WeightFunction can be
used to calculate the weights based on the data points. The WeightFunctions class contains a number of predefined weight
functions listed in the table below. In the descriptions, x and y stand for the x and y values of
each data point.
| Field |
Description |
| OneOverX |
The weight equals 1/x. |
| OneOverXSquared |
The weight equals 1/x2. |
| OneOverY |
The weight equals 1/y. |
| OneOverYSquared |
The weight equals 1/y2. |
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 containing the parameters of the curve. The
GetStandardDeviations returns a
Vector containing the standard deviations of each
parameter as estimated in the least squares calculation.
Example 1: Fitting a Polynomial
In the first example, we fit some data to a quadratic polynomial.
| C# | Copy Code |
Vector loadData = new GeneralVector(/*...*/);
Vector deflectionData = new GeneralVector(/*...*/);
Polynomial poly = new Polynomial(2);
LinearCurveFitter fitter = new LinearCurveFitter();
fitter.Curve = poly;
fitter.XValues = loadData;
fitter.YValues = deflectionData;
fitter.Fit(); |
| Visual Basic | Copy Code |
Dim loadData As Vector = New GeneralVector(' ...
Dim deflectionData As Vector = New GeneralVector(' ...
Dim poly As Polynomial = New Polynomial(2)
Dim fitter As LinearCurveFitter = New LinearCurveFitter
fitter.Curve = poly
fitter.XValues = loadData
fitter.YValues = deflectionData
fitter.Fit() |
First, the data is loaded into the loadData and deflectionData vectors. We then create
the polynomial, poly. That gives us what we need to create the LinearCurveFitter objects.
We set its properties, and perform the fit.
Example 2: Fitting arbitrary functions
In the next example, we fit measurements of the conductance of copper to a theoretical model:
We have measurements of k(T) for a series of temperatures. To estimate the parameters
c1 and c2, we need to take the following steps:
- Transform the dependent variable k(T) into y = 1/k(T). The
expression for y is then linear in the basis functions 1/T and T2.
- Create a function basis with these two basis functions.
- Create a
LinearCombination curve using this function basis.
- Create the
LinearCurveFitter object and set its properties.
- Perform the fit and report the results.
The code below performs these steps. It assumes we have created two vector variables, temperature and
conductance, that contain the data of the observations.
| C# | Copy Code |
// First basis function for the conductance sample.
static double f1(double x)
{
return 1/x;
}
// Second basis function for the conductance sample.
static double f2(double x)
{
return x*x;
}
// ...
Vector y = Vector.Inverse(conductance);
RealFunction[] basisFunctions = new RealFunction[]
{new RealFunction(f1), new RealFunction(f2)};
GeneralFunctionBasis basis = new GeneralFunctionBasis(basisFunctions);
LinearCombination curve = new LinearCombination(basis);
LinearCurveFitter fitter = new LinearCurveFitter();
fitter.Curve = curve;
fitter.XValues = temperature;
fitter.YValues = y;
fitter.Fit();
Vector solution = fitter.BestFitParameters;
Vector s = fitter.GetStandardDeviations();
Console.WriteLine("c1: {0,20:E10} {1,20:E10}", solution[0], s[0]);
Console.WriteLine("c2: {0,20:E10} {1,20:E10}", solution[1], s[1]); |
| Visual Basic | Copy Code |
' First basis function for the conductance sample.
Function f1(ByVal x As Double) As Double
Return 1 / x
End Function
' Second basis function for the conductance sample.
Function f2(ByVal x As Double) As Double
Return x * x
End Function
' ...
Dim y As Vector = Vector.Inverse(conductance)
Dim basisFunctions As RealFunction() = New RealFunction() _
{New RealFunction(AddressOf f1), New RealFunction(AddressOf f2)}
Dim basis As GeneralFunctionBasis = _
New GeneralFunctionBasis(basisFunctions)
Dim myCurve As LinearCombination = New LinearCombination(basis)
Dim fitter As LinearCurveFitter = New LinearCurveFitter
fitter.Curve = myCurve
fitter.XValues = temperature
fitter.YValues = y
fitter.Fit()
Dim solution As Vector = fitter.BestFitParameters
Dim s As Vector = fitter.GetStandardDeviations()
Console.WriteLine("c1: {0,20:E10} {1,20:E10}", solution(0), s(0))
Console.WriteLine("c2: {0,20:E10} {1,20:E10}", solution(1), s(1)) |
Up: Curve Fitting Next: Nonlinear Curve Fitting Previous: Curve Fitting Contents
Copyright 2004-2008,
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 Visual Studio Logo are registered trademarks of Microsoft Corporation