casacore
Loading...
Searching...
No Matches
NonLinearFit.h
Go to the documentation of this file.
1//# NonLinearFit.h: Class for non-linear least-squares fit.
2//# Copyright (C) 1994,1995,1996,1999,2000,2001,2002,2004
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef SCIMATH_NONLINEARFIT_H
27#define SCIMATH_NONLINEARFIT_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/scimath/Fitting/GenericL2Fit.h>
32namespace casacore { //# begin namesapce casa
33//# Forward declarations
34
35//
36// <summary> Class for non-linear least-squares fit.
37// </summary>
38//
39// <reviewed reviewer="wbrouw" date="2006/06/15" tests="tNonLinearFitLM.cc">
40// </reviewed>
41//
42// <prerequisite>
43// <li> <linkto class="Functional">Functional</linkto>
44// <li> <linkto class="Function">Function</linkto>
45// <li> <linkto module="Fitting">Fitting</linkto>
46// </prerequisite>
47//
48// <etymology>
49// A nonlinear function is used to fit a set of data points.
50// </etymology>
51//
52// <synopsis>
53// NOTE: Constraints added. Documentation out of date at moment, check
54// the tLinearFitSVD and tNonLinearFirLM programs for examples.
55//
56// The following is a brief summary of the non-linear least-squares fit
57// problem.
58// See module header, <linkto module="Fitting">Fitting</linkto>,
59// for a more complete description.
60//
61// Given a set of N data points (measurements), (x(i), y(i)) i = 0,...,N-1,
62// along with a set of standard deviations, sigma(i), for the data points,
63// and a specified non-linear function, f(x;a) where a = a(j) j = 0,...,M-1
64// are a set of parameters to be
65// determined, the non-linear least-squares fit tries to minimize
66// <srcblock>
67// chi-square = [(y(0)-f(x(0);a)/sigma(0)]^2 + [(y(1)-f(x(1);a)/sigma(1)]^2 +
68// ... + [(y(N-1)-f(x(N-1);a))/sigma(N-1)]^2.
69// </srcblock>
70// by adjusting {a(j)} in the equation.
71//
72// For multidimensional functions, x(i) is a vector, and
73// <srcblock>
74// f(x(i);a) = f(x(i,0), x(i,1), x(i,2), ...;a)
75// </srcblock>
76//
77// If the measurement errors (standard deviation sigma) are not known
78// at all, they can all be set to one initially. In this case, we assume all
79// measurements have the same standard deviation, after minimizing
80// chi-square, we recompute
81// <srcblock>
82// sigma^2 = {(y(0)-z(0))^2 + (y(1)-z(1))^2 + ...
83// + (y(N-1)-z(N-1))^2}/(N-M) = chi-square/(N-M).
84// </srcblock>
85//
86// A statistic weight can be also be assigned to each measurement if the
87// standard deviation is not available. sigma can be calculated from
88// <srcblock>
89// sigma = 1/ sqrt(weight)
90// </srcblock>
91// Alternatively a 'weight' switch can be set with <src>asWeight()</src>.
92// For best arithmetic performance, weight should be normalized to a maximum
93// value of one. Having a large weight value can sometimes lead to overflow
94// problems.
95//
96// The function to be fitted to the data can be given as an instance of the
97// <linkto class="Function">Function</linkto> class.
98// One can also form a sum of functions using the
99// <linkto class="CompoundFunction">CompoundFunction</linkto>.
100//
101// For small datasets the usage of the calls is:
102// <ul>
103// <li> Create a functional description of the parameters
104// <li> Create a fitter: NonLinearFit<T> fitter();
105// <li> Set the functional representation: fitter.setFunction()
106// <li> Do the fit to the data: fitter.fit(x, data, sigma)
107// (or do a number of calls to buildNormalMatrix(x, data, sigma)
108// and finish of with fitter.fit() or fitter.sol())
109// <li> if needed the covariance; residuals; chiSquared, parameter errors
110// can all be obtained
111// </ul>
112// Note that the fitter is reusable. An example is given in the following.
113//
114// The solution of a fit always produces the total number of parameters given
115// to the fitter. I.e. including any parameters that were fixed. In the
116// latter case the solution returned will be the fixed value.
117//
118// <templating arg=T>
119// <li> Float
120// <li> Double
121// <li> Complex
122// <li> DComplex
123// </templating>
124//
125// If there are a large number of unknowns or a large number of data points
126// machine memory limits (or timing reasons) may not allow a complete
127// in-core fitting to be performed. In this case one can incrementally
128// build the normal equation (see buildNormalMatrix()).
129//
130// The normal operation of the class tests for real inversion problems
131// only. If tests are needed for almost collinear columns in the
132// solution matrix, the collinearity can be set as the square of the sine of
133// the minimum angle allowed.
134//
135// Singular Value Decomposition is supported by setting the 'svd' switch,
136// which has a behaviour completely identical to, apart from a
137// default collinearity check of 1e-8.
138//
139// Other information (see a.o. <linkto class=LSQaips>LSQaips</linkto>) can
140// be set and obtained as well.
141// </synopsis>
142//
143// <motivation>
144// The creation of the class module was driven by the need to write code
145// to fit Gaussian functions to data points.
146// </motivation>
147//
148// <example>
149// </example>
150
151template<class T> class NonLinearFit : public GenericL2Fit<T>
152{
153public:
154 //# Constants
155 // Default maximum number of iterations (30)
156 static const uInt MAXITER = 30;
157 // Default convergence criterium (0.001)
158 static const Double CRITERIUM;
159
160 //# Constructors
161 // Create a fitter: the normal way to generate a fitter object. Necessary
162 // data will be deduced from the Functional provided with
163 // <src>setFunction()</src>.
164 // Create optionally a fitter with SVD behaviour specified.
165 explicit NonLinearFit(Bool svd=False);
166 // Copy constructor (deep copy)
168 // Assignment (deep copy)
170
171 // Destructor
172 virtual ~NonLinearFit();
173
174 // setMaxIter() sets the maximum number of iterations to do before stopping.
175 // Default value is 30.
176 void setMaxIter(uInt maxIter=MAXITER);
177
178 // getMaxIter() queries what the maximum number of iterations currently is
179 uInt getMaxIter() const { return maxiter_p; };
180
181 // currentIteration() queries what the current iteration is
183
184 // setCriteria() sets the convergence criteria. The actual value and
185 // its interpretation depends on the derived class used to do the
186 // actual iteration. Default value is 0.001.
187 void setCriteria(const Double criteria=CRITERIUM) {criterium_p = criteria; };
188
189 // getCriteria() queries the current criteria
190 Double getCriteria() const { return criterium_p; };
191
192 // Check to see if the fit has converged
193 Bool converged() const { return converge_p; };
194
195protected:
196 //#Data
197 // Maximum number of iterations
199 // Current iteration number
201 // Convergence criteria
203 // Has fit converged
205
206 //# Member functions
207 // Generalised fitter
208 virtual Bool fitIt
210 const Array<typename FunctionTraits<T>::BaseType> &x,
211 const Vector<typename FunctionTraits<T>::BaseType> &y,
212 const Vector<typename FunctionTraits<T>::BaseType> *const sigma,
213 const Vector<Bool> *const mask=0) = 0;
214
215private:
216 //# Data
217
218 //# Member functions
219
220protected:
221 //# Make members of parent classes known.
222 using GenericL2Fit<T>::pCount_p;
224 using GenericL2Fit<T>::arg_p;
225 using GenericL2Fit<T>::sol_p;
226 using GenericL2Fit<T>::fsol_p;
227 using GenericL2Fit<T>::solved_p;
228 using GenericL2Fit<T>::nr_p;
229 using GenericL2Fit<T>::svd_p;
230 using GenericL2Fit<T>::condEq_p;
231 using GenericL2Fit<T>::fullEq_p;
232 using GenericL2Fit<T>::err_p;
233 using GenericL2Fit<T>::ferr_p;
234 using GenericL2Fit<T>::errors_p;
235 using GenericL2Fit<T>::valder_p;
236 using GenericL2Fit<T>::set;
239 using GenericL2Fit<T>::isReady;
240};
241
242} //# End namespace casacore
243#ifndef CASACORE_NO_AUTO_TEMPLATES
244#include <casacore/scimath/Fitting/NonLinearFit.tcc>
245#endif //# CASACORE_NO_AUTO_TEMPLATES
246#endif
T BaseType
Template base type.
Bool solved_p
Have solution.
void fillSVDConstraints()
Get the SVD constraints.
uInt nr_p
The rank of the solution.
Vector< typename FunctionTraits< T >::BaseType > condEq_p
Condition equation parameters (for number of adjustable parameters)
Vector< typename FunctionTraits< T >::BaseType > err_p
Local error area.
void buildConstraint()
Build the constraint equations.
Vector< typename FunctionTraits< T >::BaseType > ferr_p
uInt pCount_p
Number of available parameters.
Vector< typename FunctionTraits< T >::BaseType > sol_p
Local solution area.
Bool svd_p
SVD indicator.
Bool errors_p
Have errors.
Function< typename FunctionTraits< T >::DiffType, typename FunctionTraits< T >::DiffType > * ptr_derive_p
Function to use in evaluating condition equation.
Vector< typename FunctionTraits< T >::BaseType > fsol_p
Vector< typename FunctionTraits< T >::ArgType > arg_p
Contiguous argument areas.
FunctionTraits< T >::DiffType valder_p
Local value and derivatives.
Vector< typename FunctionTraits< T >::BaseType > fullEq_p
Equation for all available parameters.
void set(uInt nUnknowns, uInt nConstraints=0)
Set new sizes (default is for Real)
LSQFit::ReadyCode isReady() const
Ask the state of the non-linear solutions.
Definition LSQFit.h:747
static const String sol
Definition LSQFit.h:855
static const Double CRITERIUM
Default convergence criterium (0.001)
virtual ~NonLinearFit()
Destructor.
uInt curiter_p
Current iteration number.
Double getCriteria() const
getCriteria() queries the current criteria
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)=0
Generalised fitter.
static const uInt MAXITER
Default maximum number of iterations (30)
NonLinearFit(Bool svd=False)
Create a fitter: the normal way to generate a fitter object.
void setMaxIter(uInt maxIter=MAXITER)
setMaxIter() sets the maximum number of iterations to do before stopping.
void setCriteria(const Double criteria=CRITERIUM)
setCriteria() sets the convergence criteria.
uInt getMaxIter() const
getMaxIter() queries what the maximum number of iterations currently is
Bool converged() const
Check to see if the fit has converged.
uInt currentIteration() const
currentIteration() queries what the current iteration is
NonLinearFit & operator=(const NonLinearFit &other)
Assignment (deep copy)
uInt maxiter_p
Maximum number of iterations.
NonLinearFit(const NonLinearFit &other)
Copy constructor (deep copy)
Double criterium_p
Convergence criteria.
Bool converge_p
Has fit converged.
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
unsigned int uInt
Definition aipstype.h:49
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
double Double
Definition aipstype.h:53