CLsqFit:Using the CLsqFit Class CLsqFit:Using Multiple Independent Variables

Basis Functions


The topic Overview of the CLsqFit Class describes how the linear least squares method separates the "rubber function" being fit into the dot product of two vectors: a coefficient vector and a basis vector. The rubber function being fit must evaluate each data point to create a basis vector from its independent variables. These basis vectors are then used to compute the coefficient vector. Mira provides built-in basis functions for fitting an n-dimensional polynomial of up to 10 variables and 100 coefficients and a hyperplane of up to 10 variables. For any other function form, you must write a basis function in your script, then register it with the CLsqFit object. This topic describes how to write your own basis functions.

Creating a Basis Function in the Script

A basis function declared in the script has a standard interface: it receives 2 or 3 arguments and returns 1 argument. The input arguments describe the number of coefficients being fit, the value(s) of the independent variable(s) being fit and, optionally, the mean coordinates for each independent variable. The basis function uses these arguments to create a basis vector in a lua table and then returns that table. For example, if b is the basis vector assembled by the basis function, then the function must return b.

The basis function is declared using either of the following 2 forms: function(n,x) or function(n,x,m), where the arguments are defined as follows:

n

The number of coefficients being fit, including forced coefficients.

c

A table containing the values of the independent variable(s), or coordinates, at the sample point. The number of table elements equals the number of basis dimensions.

m

A table containing the mean coordinate for the independent variable(s). This parameter is optional. The number of table elements equals the number of basis dimensions.

The optional argument m is used if you want to estimate the fit about the mean coordinate(s) rather than about the origin. Fitting about the mean minimizes the statistical uncertainties of the coefficients. If you don't care to fit about the mean, your basis function can declare only the first 2 arguments. Remember that the coordinate vector c is always passed as a lua table, even if the fit is made to only 1 variable, such as the x coordinate. Therefore, when fitting a single variable, the basis function must use the subscript [1] to access variable dimension 1 of the table it receives. In this class, the term "dimension" is often used in place of "independent variable" or "coordinate". Thus fitting up to 10 independent variables corresponds to fitting up to 10 basis dimensions. After declaring your basis function, you must specify the number of coefficients and dimensions it uses; these are the number of independent variables used by the data points andthe number of coefficients involved in the fit.

Related methods

The following methods are used along with your basis function:

SetBasisFunc

This method registers your basis function with the CLsqFit object. If you do not call this method, the basis function defaults to the built-in n-dimensional polynomial.

SetNumCoefs

Declares the number of coefficients to be fit, which equals the numbers used by your basis function, as well as the number of basis dimensions. Note: forced coefficients are included in the coefficient count since they are represented in the basis function.

GetBasisDim

Returns the number of basis dimensions declared by the SetNumCoefs method.

Examples of Basis Functions

A basis function defined in your script takes the following form:

Template of a Basis Function

function F( n, c, m )

declare the function

  b = {}

create a table for the basis vector

   ...

code that fills the basis vector

  return b

return the status and the basis vector

end

end of the basis function

Several examples of a script basis function are given below.

Fitting a line

To fit a line, the basis function would look like this:

function F( n, c )

n is the number of coefs; c is the independent variable

  b = {}

declare a lua table for the basis vector

  b[1] = 1

basis vector element [1] is 1

  b[2] = c[1]

basis vector element [2] is c[1], or the x coordinate

  return b

return the basis vector

end

end of the basis function

To fit the line about the mean x coordinate, the basis function would look like this:

function F( n, c, m )

n is the number of coefs; c is the independent variable; m is the mean

  b = {}

declare a lua table for the basis vector

  b[1] = 1

basis vector element [1] is 1

  b[2] = c[1] - m[1]

basis vector element [2] is c[1]

  return b

return the basis vector

end

end of the basis function

Note that fitting the line about the origin and about the mean coordinates give different coefficients and errors for the same data.

Fitting a quadratic

To fit a quadratic about the origin, the basis function would look like this:

function F( n, c )

n is the number of coefs; c is the independent variable

  b = {}

declare a lua table for the basis vector

  b[1] = 1

basis vector element [1] is 1

  b[2] = c[1]

basis vector element [2] is c (the x coordinate)

  b[3] = c[1] * c[1]

basis vector element [3] is c^2

  return b

return the basis vector

end

end of the basis function

Fitting a polynomial of 1 independent variable

To see how the parameter n might be used, consider fitting a polynomial of order n-1 (the order of a polynomial is the highest power in the polynomial). Including the "constant" term, the polynomial of order n-1 has n coefficients. Here is the basis function:

function F( n, c )

n is the number of coefs; c is the independent variable

  b = {}

declare a lua table for the basis vector

  b[1] = 1

basis vector element [1] is 1

  for i = 2, n do

compute each power of the polynomial recursively

    b[i] = b[i-1] * c[1]

basis vector element [n] is c^(n-1)

  end

end of loop

  return b

return the basis vector

end

end of the basis function

Fitting a warped plane to 2 independent variables

Note that the tables c and m can contain up to 10 elements for fitting as many as 10 independent variables, or "basis dimensions". For example, if fitting to x,y coordinates, the table c has 2 elements, c[1] for the x coordinate and c[2] for the y coordinate. The following basis function fits a warped plan to an (x,y) surface using using coordinates about the mean:

function F( n, c )

n is the number of coefs; c is the independent variable

  b = {}

declare a lua table for the basis vector

  b[1] = 1

basis vector element b[1] is 1 (the constant term)

  b[2] = c[1]

b[2] = x

  b[3] = c[2]

b[3] = y

  b[4] = c[1] * c[2]

b[4] = x * y

  return b

return the basis vector

end

end of the basis function

This case is handled by the built-in, n-dimensional polynomial basis function selected using SetBasisFunc( 1 ). Mira defaults to this basis function if you do not explicitly select a basis function in the script. In this example, you would then specify a 2x2 coefficient fit using SetNumCoefs( {2,2} ). Notice that the multiple coefficient numbers are combined into a table using {}.

Fitting a hyperplane to n independent variables

As a final example, consider a "hyperplane" of the form f = a[1] + a[2] x + a[3] y + a[4] z + ... The hyperplane fits n+1 coefficients because of the constant term a[1]. This is the same as the built-in basis function selected using SetBasisFunc( 2 ).

function HP( n, c, m )

n is the number of coefs; c is the independent variable

  b = {}

declare a lua table for the basis vector

  b[1] = 1

basis vector element [1] is 1 (the constant term)

  for i = 2, n do

 

    b[i] = c[i-1] - m[i-1]

 

  end

 

  return b

return the basis vector

end

end of the basis function

Related Topics

CLsqFit class, SetBasisFunc