casacore
Public Member Functions | Protected Member Functions | List of all members
casacore::LinearFit< T > Class Template Reference

More...

#include <LinearFit.h>

Public Member Functions

 LinearFit ()
 Create a fitter: the normal way to generate a fitter object. More...
 
 LinearFit (const LinearFit &other)
 Copy constructor (deep copy) More...
 
LinearFitoperator= (const LinearFit &other)
 Assignment (deep copy) More...
 
virtual ~LinearFit ()
 Destructor. More...
 
- Public Member Functions inherited from casacore::GenericL2Fit< T >
 GenericL2Fit ()
 Create a fitter: the normal way to generate a fitter object. More...
 
 GenericL2Fit (const GenericL2Fit &other)
 Copy constructor (deep copy) More...
 
GenericL2Fitoperator= (const GenericL2Fit &other)
 Assignment (deep copy) More...
 
virtual ~GenericL2Fit ()
 Destructor. More...
 
template<class U >
void setFunction (const Function< U, U > &function)
 Sets the function to be fitted. More...
 
template<class U >
Bool setConstraint (const uInt n, const Function< U, U > &function, const Vector< typename FunctionTraits< T >::BaseType > &x, const typename FunctionTraits< T >::BaseType y=typename FunctionTraits< T >::BaseType(0))
 Set the possible constraint functions. More...
 
Bool setConstraint (const uInt n, const Vector< typename FunctionTraits< T >::BaseType > &x, const typename FunctionTraits< T >::BaseType y=typename FunctionTraits< T >::BaseType(0))
 
Bool setConstraint (const uInt n, const typename FunctionTraits< T >::BaseType y=typename FunctionTraits< T >::BaseType(0))
 
Bool addConstraint (const Function< typename FunctionTraits< T >::DiffType, typename FunctionTraits< T >::DiffType > &function, const Vector< typename FunctionTraits< T >::BaseType > &x, const typename FunctionTraits< T >::BaseType y=typename FunctionTraits< T >::BaseType(0))
 
Bool addConstraint (const Vector< typename FunctionTraits< T >::BaseType > &x, const typename FunctionTraits< T >::BaseType y=typename FunctionTraits< T >::BaseType(0))
 
Bool addConstraint (const typename FunctionTraits< T >::BaseType y=typename FunctionTraits< T >::BaseType(0))
 
void setCollinearity (const Double cln)
 Set the collinearity factor as the square of the sine of the minimum angle allowed between input vectors (default zero for non-SVD, 1e-8 for SVD) More...
 
void asWeight (const Bool aswgt)
 Set sigma values to be interpreted as weight (i.e. More...
 
void asSVD (const Bool svd)
 Set the use of SVD or not (default). More...
 
Function< typename FunctionTraits< T >::DiffType, typename FunctionTraits< T >::DiffType > * fittedFunction ()
 Return a pointer to the function being fitted. More...
 
const Function< typename FunctionTraits< T >::DiffType, typename FunctionTraits< T >::DiffType > * fittedFunction () const
 
uInt fittedNumber () const
 Return the number of fitted parameters. More...
 
uInt NConstraints ()
 Return the number of constraints, and pointers to constraint functions. More...
 
Function< typename FunctionTraits< T >::DiffType, typename FunctionTraits< T >::DiffType > * getConstraint (const uInt n)
 
Vector< typename LSQTraits< typename FunctionTraits< T >::BaseType >::base > getSVDConstraint (uInt n)
 Return the nth constraint equation derived from SVD Note that the number present will be given by getDeficiency() More...
 
void setParameterValues (const Vector< typename FunctionTraits< T >::BaseType > &parms)
 Set the parameter values. More...
 
void setMaskedParameterValues (const Vector< typename FunctionTraits< T >::BaseType > &parms)
 
Vector< typename FunctionTraits< T >::BaseType > fit (const Vector< typename FunctionTraits< T >::BaseType > &x, const Vector< typename FunctionTraits< T >::BaseType > &y, const Vector< typename FunctionTraits< T >::BaseType > &sigma, const Vector< Bool > *const mask=0)
 Fit the function to the data. More...
 
Vector< typename FunctionTraits< T >::BaseType > fit (const Matrix< typename FunctionTraits< T >::BaseType > &x, const Vector< typename FunctionTraits< T >::BaseType > &y, const Vector< typename FunctionTraits< T >::BaseType > &sigma, const Vector< Bool > *const mask=0)
 
Vector< typename FunctionTraits< T >::BaseType > fit (const Vector< typename FunctionTraits< T >::BaseType > &x, const Vector< typename FunctionTraits< T >::BaseType > &y, const Vector< Bool > *const mask=0)
 
Vector< typename FunctionTraits< T >::BaseType > fit (const Matrix< typename FunctionTraits< T >::BaseType > &x, const Vector< typename FunctionTraits< T >::BaseType > &y, const Vector< Bool > *const mask=0)
 
Vector< typename FunctionTraits< T >::BaseType > fit (const Vector< Bool > *const mask=0)
 
Bool fit (Vector< typename FunctionTraits< T >::BaseType > &sol, const Vector< typename FunctionTraits< T >::BaseType > &x, const Vector< typename FunctionTraits< T >::BaseType > &y, const Vector< typename FunctionTraits< T >::BaseType > &sigma, const Vector< Bool > *const mask=0)
 
Bool fit (Vector< typename FunctionTraits< T >::BaseType > &sol, const Matrix< typename FunctionTraits< T >::BaseType > &x, const Vector< typename FunctionTraits< T >::BaseType > &y, const Vector< typename FunctionTraits< T >::BaseType > &sigma, const Vector< Bool > *const mask=0)
 
Bool fit (Vector< typename FunctionTraits< T >::BaseType > &sol, const Vector< typename FunctionTraits< T >::BaseType > &x, const Vector< typename FunctionTraits< T >::BaseType > &y, const typename FunctionTraits< T >::BaseType &sigma, const Vector< Bool > *const mask=0)
 
Bool fit (Vector< typename FunctionTraits< T >::BaseType > &sol, const Matrix< typename FunctionTraits< T >::BaseType > &x, const Vector< typename FunctionTraits< T >::BaseType > &y, const typename FunctionTraits< T >::BaseType &sigma, const Vector< Bool > *const mask=0)
 
Bool fit (Vector< typename FunctionTraits< T >::BaseType > &sol, const Vector< Bool > *const mask=0)
 
Double chiSquare () const
 Obtain the chi squared. More...
 
const Vector< typename FunctionTraits< T >::BaseType > & errors () const
 Get the errors on the solved values. More...
 
Bool errors (Vector< typename FunctionTraits< T >::BaseType > &err) const
 
Matrix< DoublecompuCovariance ()
 Get covariance matrix. More...
 
void compuCovariance (Matrix< Double > &cov)
 
void buildNormalMatrix (const Vector< typename FunctionTraits< T >::BaseType > &x, const Vector< typename FunctionTraits< T >::BaseType > &y, const Vector< typename FunctionTraits< T >::BaseType > &sigma, const Vector< Bool > *const mask=0)
 Generate the normal equations by one or more calls to the buildNormalMatrix(), before calling a fit() without arguments. More...
 
void buildNormalMatrix (const Matrix< typename FunctionTraits< T >::BaseType > &x, const Vector< typename FunctionTraits< T >::BaseType > &y, const Vector< typename FunctionTraits< T >::BaseType > &sigma, const Vector< Bool > *const mask=0)
 
void buildNormalMatrix (const Vector< typename FunctionTraits< T >::BaseType > &x, const Vector< typename FunctionTraits< T >::BaseType > &y, const Vector< Bool > *const mask=0)
 
void buildNormalMatrix (const Matrix< typename FunctionTraits< T >::BaseType > &x, const Vector< typename FunctionTraits< T >::BaseType > &y, const Vector< Bool > *const mask=0)
 
Bool residual (Vector< typename FunctionTraits< T >::BaseType > &y, const Array< typename FunctionTraits< T >::BaseType > &x, const Vector< typename FunctionTraits< T >::BaseType > &sol, const Bool model=False)
 Return the residual after a fit in y. More...
 
Bool residual (Vector< typename FunctionTraits< T >::BaseType > &y, const Array< typename FunctionTraits< T >::BaseType > &x, const Bool model=False)
 
uInt getRank () const
 Get the rank of the solution (or zero of no fit() done yet). More...
 

Protected Member Functions

virtual Bool fitIt (Vector< typename FunctionTraits< T >::BaseType > &sol, const Array< typename FunctionTraits< T >::BaseType > &x, const Vector< typename FunctionTraits< T >::BaseType > &y, const Vector< typename FunctionTraits< T >::BaseType > *const sigma, const Vector< Bool > *const mask=0)
 Generalised fitter. More...
 
- Protected Member Functions inherited from casacore::GenericL2Fit< T >
void buildMatrix (const Array< typename FunctionTraits< T >::BaseType > &x, const Vector< typename FunctionTraits< T >::BaseType > &y, const Vector< typename FunctionTraits< T >::BaseType > *const sigma, const Vector< Bool > *const mask=0)
 Build the normal matrix. More...
 
void buildConstraint ()
 Build the constraint equations. More...
 
void fillSVDConstraints ()
 Get the SVD constraints. More...
 
Bool buildResidual (Vector< typename FunctionTraits< T >::BaseType > &y, const Array< typename FunctionTraits< T >::BaseType > &x, const Vector< typename FunctionTraits< T >::BaseType > *const sol, const Bool model=False)
 Calculate residuals. More...
 
FunctionTraits< T >::BaseType getVal_p (const Array< typename FunctionTraits< T >::BaseType > &x, uInt j, uInt i) const
 Function to get evaluated functional value. More...
 
void initfit_p (uInt parcnt)
 Initialise the fitter with number of solvable parameters. More...
 
uInt testInput_p (const Array< typename FunctionTraits< T >::BaseType > &x, const Vector< typename FunctionTraits< T >::BaseType > &y, const Vector< typename FunctionTraits< T >::BaseType > *const sigma)
 Return number of condition equations and check sizes x, y, sigma. More...
 
void resetFunction ()
 Reset all the input. More...
 

Additional Inherited Members

- Public Attributes inherited from casacore::GenericL2Fit< T >
const Double COLLINEARITY
 Default collinearity test for SVD. More...
 
- Protected Attributes inherited from casacore::GenericL2Fit< T >
uInt aCount_ai
 Adjustable. More...
 
Bool svd_p
 SVD indicator. More...
 
Function< typename FunctionTraits< T >::DiffType, typename FunctionTraits< T >::DiffType > * ptr_derive_p
 Function to use in evaluating condition equation. More...
 
PtrBlock< Function< typename FunctionTraits< T >::DiffType, typename FunctionTraits< T >::DiffType > * > constrFun_p
 List of functions describing the possible constraint equations e.g. More...
 
PtrBlock< Vector< typename FunctionTraits< T >::BaseType > * > constrArg_p
 List of vectors describing the constraint equations' arguments. More...
 
PtrBlock< typename FunctionTraits< T >::BaseType * > constrVal_p
 List of values describing the constraint equations' value. More...
 
uInt pCount_p
 Number of available parameters. More...
 
uInt ndim_p
 Number of dimensions of input data. More...
 
Bool needInit_p
 No normal equations yet. More...
 
Bool solved_p
 Have solution. More...
 
Bool errors_p
 Have errors. More...
 
Bool ferrors_p
 
Bool asweight_p
 Interpret as weights rather than as sigma the given values. More...
 
uInt nr_p
 The rank of the solution. More...
 
Vector< typename FunctionTraits< T >::BaseType > condEq_p
 Condition equation parameters (for number of adjustable parameters) More...
 
Vector< typename FunctionTraits< T >::BaseType > fullEq_p
 Equation for all available parameters. More...
 
Vector< typename FunctionTraits< T >::ArgType > arg_p
 Contiguous argument areas. More...
 
Vector< typename FunctionTraits< T >::ArgType > carg_p
 
Vector< typename FunctionTraits< T >::BaseType > sol_p
 Local solution area. More...
 
Vector< typename FunctionTraits< T >::BaseType > fsol_p
 
Vector< typename FunctionTraits< T >::BaseType > err_p
 Local error area. More...
 
Vector< typename FunctionTraits< T >::BaseType > ferr_p
 
FunctionTraits< T >::DiffType valder_p
 Local value and derivatives. More...
 
Vector< Vector< typename LSQTraits< typename FunctionTraits< T >::BaseType >::base > > consvd_p
 Local SVD constraints. More...
 

Detailed Description

template<class T>
class casacore::LinearFit< T >

Class for linear least-squares fit.

Review Status

Reviewed By:
wbrouw
Date Reviewed:
2004/06/15
Test programs:
tLinearFitSVD

Prerequisite

Etymology

A set of data point is fit with some functional equation. The equations solved are linear equations. The functions themselves however can be wildly nonlinear.

Synopsis

NOTE: Constraints added. Documentation out of date at moment, check the tLinearFitSVD and tNonLinearFirLM programs for examples.

The following is a brief summary of the linear least-squares fit problem. See module header, Fitting, for a more complete description.

Given a set of N data points (measurements), (x(i), y(i)) i = 0,...,N-1, along with a set of standard deviations, sigma(i), for the data points, and M specified functions, f(j)(x) j = 0,...,M-1, we form a linear combination of the functions:

z(i) = a(0)f(0)(x(i)) + a(1)f(1)(x(i)) +... + a(M-1)f(M-1)(x(i)),

where a(j) j = 0,...,M-1 are a set of parameters to be determined. The linear least-squares fit tries to minimize

chi-square = [(y(0)-z(0))/sigma(0)]^2 + [(y(1)-z(1))/sigma(1)]^2 +..\.
+ [(y(N-1)-z(N-1))/sigma(N-1)]^2.

by adjusting {a(j)} in the equation.

For complex numbers, [(y(i)-z(i))/sigma(i)]^2 in chi-square is replaced by [(y(i)-z(i))/sigma(i)]*conjugate([(y(i)-z(i))/sigma(i)])

For multidimensional functions, x(i) is a vector, and

f(j)(x(i)) = f(j)(x(i,0), x(i,1), x(i,2),...)


Normally, it is necessary that N > M for the solutions to be valid, since there must be more data points than model parameters to be solved.

If the measurement errors (standard deviation sigma) are not known at all, they can all be set to one initially. In this case, we assume all measurements have the same standard deviation, after minimizing chi-square, we recompute

sigma^2 = {(y(0)-z(0))^2 + (y(1)-z(1))^2 +..\.
+ (y(N-1)-z(N-1))^2}/(N-M) = chi-square/(N-M).


A statistic weight can also be assigned to each measurement if the standard deviation is not available. sigma can be calculated from

sigma = 1/ sqrt(weight)

Alternatively a 'weight' switch can be set with asWeight(). For best arithmetic performance, weight should be normalized to a maximum value of one. Having a large weight value can sometimes lead to overflow problems.

The function to be fitted to the data can be given as an instance of the Function class. One can also form a sum of functions using the CompoundFunction.

For small datasets the usage of the calls is:

Note that the fitter is reusable. An example is given in the following.

The solution of a fit always produces the total number of parameters given to the fitter. I.e. including any parameters that were fixed. In the latter case the solution returned will be the fixed value.

Template Type Argument Requirements (T)

If there are a large number of unknowns or a large number of data points machine memory limits (or timing reasons) may not allow a complete in-core fitting to be performed. In this case one can incrementally build the normal equation (see buildNormalMatrix()).

The normal operation of the class tests for real inversion problems only. If tests are needed for almost collinear columns in the solution matrix, the collinearity can be set as the square of the sine of the minimum angle allowed.

Singular Value Decomposition is supported by the LinearFitSVD class, which has a behaviour completely identical to this class (apart from a default collinearity of 1e-8).

Other information (see a.o. LSQFit) can be set and obtained as well.

Motivation

The creation of this class was driven by the need to write code to perform baseline fitting or continuum subtraction.

Example

In the following a polynomial is fitted through the first 20 prime numbers. The data is given in the x vector (1 to 20) and in the primesTable (2, 3,..., 71) (see tLinearFitSVD test program). In the following all four methods to calculate a polynomial through the data is used

// The list of coordinate x-values
Vector<Double> x(nPrimes);
indgen((Array<Double>&)x, 1.0); // 1, 2,..\.
Vector<Double> primesTable(nPrimes);
for (uInt i=1; i < nPrimes; i++) {
primesTable(i) =
Primes::nextLargerPrimeThan(Int(primesTable(i-1)+0.01));
};
Vector<Double> sigma(nPrimes);
sigma = 1.0;
// The fitter
LinearFit<Double> fitter;
Polynomial<AutoDiff<Double> > combination(2);
// Get the solution
fitter.setFunction(combination);
Vector<Double> solution = fitter.fit(x, primesTable, sigma);
// create a special function (should probably at beginning)
static void myfnc(Vector<Double> &y, const Double x) {
y(0) = 1; for (uInt i=1; i<y.nelements(); i++) y(i) = y(i-1)*x; };
fitter.setFunction(3, &myfnc);
solution = fitter.fit(x, primesTable, sigma);
// Create the direct coefficients table
fitter.setFunction(3);
Matrix<Double> xx(nPrimes, 3);
for (uInt i=0; i<nPrimes; i++) {
xx(i,0) = 1;
for (uInt j=1; j<3; j++) xx(i,j) = xx(i,j-1)*Double(i+1);
};
solution = fitter.fit(xx, primesTable, sigma);

In the test program examples are given on how to get the other information, and other examples.

Definition at line 207 of file LinearFit.h.

Constructor & Destructor Documentation

◆ LinearFit() [1/2]

template<class T >
casacore::LinearFit< T >::LinearFit ( )

Create a fitter: the normal way to generate a fitter object.

Necessary data will be deduced from the Functional provided with setFunction()

◆ LinearFit() [2/2]

template<class T >
casacore::LinearFit< T >::LinearFit ( const LinearFit< T > &  other)

Copy constructor (deep copy)

◆ ~LinearFit()

template<class T >
virtual casacore::LinearFit< T >::~LinearFit ( )
virtual

Destructor.

Member Function Documentation

◆ fitIt()

template<class T >
virtual Bool casacore::LinearFit< T >::fitIt ( Vector< typename FunctionTraits< T >::BaseType > &  sol,
const Array< typename FunctionTraits< T >::BaseType > &  x,
const Vector< typename FunctionTraits< T >::BaseType > &  y,
const Vector< typename FunctionTraits< T >::BaseType > *const  sigma,
const Vector< Bool > *const  mask = 0 
)
protectedvirtual

Generalised fitter.

Implements casacore::GenericL2Fit< T >.

◆ operator=()

template<class T >
LinearFit& casacore::LinearFit< T >::operator= ( const LinearFit< T > &  other)

Assignment (deep copy)


The documentation for this class was generated from the following file:
casacore::sqrt
LatticeExprNode sqrt(const LatticeExprNode &expr)
casacore::square
TableExprNode square(const TableExprNode &node)
Definition: ExprNode.h:1303
casacore::Double
double Double
Definition: aipstype.h:55
casacore::indgen
void indgen(TableVector< T > &tv, Int start, Int inc)
Definition: TabVecMath.h:400
casacore::uInt
unsigned int uInt
Definition: aipstype.h:51
casacore::Primes::nextLargerPrimeThan
static uInt nextLargerPrimeThan(uInt number)
This function finds the next largest prime than number, returns that value and stores it in the table...
casacore::Int
int Int
Definition: aipstype.h:50
N
uInt N
Axis number.
Definition: ArrayAccessor.h:60