casacore
Loading...
Searching...
No Matches
LinearFit.h
Go to the documentation of this file.
1//# LinearFit.h: Class for linear least-squares fit.
2//#
3//# Copyright (C) 1995,1999,2000,2001,2002,2004
4//# Associated Universities, Inc. Washington DC, USA.
5//#
6//# This library is free software; you can redistribute it and/or modify it
7//# under the terms of the GNU Library General Public License as published by
8//# the Free Software Foundation; either version 2 of the License, or (at your
9//# option) any later version.
10//#
11//# This library is distributed in the hope that it will be useful, but WITHOUT
12//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14//# License for more details.
15//#
16//# You should have received a copy of the GNU Library General Public License
17//# along with this library; if not, write to the Free Software Foundation,
18//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
19//#
20//# Correspondence concerning AIPS++ should be addressed as follows:
21//# Internet email: casa-feedback@nrao.edu.
22//# Postal address: AIPS++ Project Office
23//# National Radio Astronomy Observatory
24//# 520 Edgemont Road
25//# Charlottesville, VA 22903-2475 USA
26
27#ifndef SCIMATH_LINEARFIT_H
28#define SCIMATH_LINEARFIT_H
29
30//# Includes
31#include <casacore/casa/aips.h>
32#include <casacore/scimath/Fitting/GenericL2Fit.h>
33
34namespace casacore { //# NAMESPACE CASACORE - BEGIN
35
36//# Forward declarations
37
38// <summary> Class for linear least-squares fit.
39// </summary>
40//
41// <reviewed reviewer="wbrouw" date="2004/06/15" tests="tLinearFitSVD.cc"
42// demos="">
43// </reviewed>
44//
45// <prerequisite>
46// <li> <linkto class="Functional">Functional</linkto>
47// <li> <linkto class="Function">Function</linkto>
48// <li> <linkto module="Fitting">Fitting</linkto>
49// </prerequisite>
50//
51// <etymology>
52// A set of data point is fit with some functional equation.
53// The equations solved are linear equations. The functions
54// themselves however can be wildly nonlinear.
55// </etymology>
56//
57// <synopsis>
58// NOTE: Constraints added. Documentation out of date at moment, check
59// the tLinearFitSVD and tNonLinearFirLM programs for examples.
60//
61// The following is a brief summary of the linear least-squares fit problem.
62// See module header, <linkto module="Fitting">Fitting</linkto>,
63// for a more complete description.
64//
65// Given a set of N data points (measurements), (x(i), y(i)) i = 0,...,N-1,
66// along with a set of standard deviations, sigma(i), for the data points,
67// and M specified functions, f(j)(x) j = 0,...,M-1, we form a linear
68// combination of the functions:
69// <srcblock>
70// z(i) = a(0)f(0)(x(i)) + a(1)f(1)(x(i)) + ... + a(M-1)f(M-1)(x(i)),
71// </srcblock>
72// where a(j) j = 0,...,M-1 are a set of parameters to be determined.
73// The linear least-squares fit tries to minimize
74// <srcblock>
75// chi-square = [(y(0)-z(0))/sigma(0)]^2 + [(y(1)-z(1))/sigma(1)]^2 + ...
76// + [(y(N-1)-z(N-1))/sigma(N-1)]^2.
77// </srcblock>
78// by adjusting {a(j)} in the equation.
79//
80// For complex numbers, <code>[(y(i)-z(i))/sigma(i)]^2</code> in chi-square
81// is replaced by
82// <code>[(y(i)-z(i))/sigma(i)]*conjugate([(y(i)-z(i))/sigma(i)])</code>
83//
84// For multidimensional functions, x(i) is a vector, and
85// <srcblock>
86// f(j)(x(i)) = f(j)(x(i,0), x(i,1), x(i,2), ...)
87// </srcblock>
88//
89// Normally, it is necessary that N > M for the solutions to be valid, since
90// there must be more data points than model parameters to be solved.
91//
92// If the measurement errors (standard deviation sigma) are not known
93// at all, they can all be set to one initially. In this case, we assume all
94// measurements have the same standard deviation, after minimizing
95// chi-square, we recompute
96// <srcblock>
97// sigma^2 = {(y(0)-z(0))^2 + (y(1)-z(1))^2 + ...
98// + (y(N-1)-z(N-1))^2}/(N-M) = chi-square/(N-M).
99// </srcblock>
100//
101// A statistic weight can also be assigned to each measurement if the
102// standard deviation is not available. sigma can be calculated from
103// <srcblock>
104// sigma = 1/ sqrt(weight)
105// </srcblock>
106// Alternatively a 'weight' switch can be set with <src>asWeight()</src>.
107// For best arithmetic performance, weight should be normalized to a maximum
108// value of one. Having a large weight value can sometimes lead to overflow
109// problems.
110//
111// The function to be fitted to the data can be given as an instance of the
112// <linkto class="Function">Function</linkto> class.
113// One can also form a sum of functions using the
114// <linkto class="CompoundFunction">CompoundFunction</linkto>.
115//
116// For small datasets the usage of the calls is:
117// <ul>
118// <li> Create a functional description of the parameters
119// <li> Create a fitter: LinearFit<T> fitter();
120// <li> Set the functional representation: fitter.setFunction()
121// <li> Do the fit to the data: fitter.fit(x, data, sigma)
122// (or do a number of calls to buildNormalMatrix(x, data, sigma)
123// and finish of with fitter.fit() or fitter.sol())
124// <li> if needed the covariance; residuals; chiSquared, parameter errors
125// can all be obtained
126// </ul>
127// Note that the fitter is reusable. An example is given in the following.
128//
129// The solution of a fit always produces the total number of parameters given
130// to the fitter. I.e. including any parameters that were fixed. In the
131// latter case the solution returned will be the fixed value.
132//
133// <templating arg=T>
134// <li> Float
135// <li> Double
136// <li> Complex
137// <li> DComplex
138// </templating>
139//
140// If there are a large number of unknowns or a large number of data points
141// machine memory limits (or timing reasons) may not allow a complete
142// in-core fitting to be performed. In this case one can incrementally
143// build the normal equation (see buildNormalMatrix()).
144//
145// The normal operation of the class tests for real inversion problems
146// only. If tests are needed for almost collinear columns in the
147// solution matrix, the collinearity can be set as the square of the sine of
148// the minimum angle allowed.
149//
150// Singular Value Decomposition is supported by the
151// <linkto class=LinearFitSVD>LinearFitSVD</linkto> class,
152// which has a behaviour completely identical to this class (apart from a
153// default collinearity of 1e-8).
154//
155// Other information (see a.o. <linkto class=LSQFit>LSQFit</linkto>) can
156// be set and obtained as well.
157// </synopsis>
158//
159// <motivation>
160// The creation of this class was driven by the need to write code
161// to perform baseline fitting or continuum subtraction.
162// </motivation>
163
164// <example>
165//# /// redo example
166// In the following a polynomial is fitted through the first 20 prime numbers.
167// The data is given in the x vector (1 to 20) and in the primesTable
168// (2, 3, ..., 71) (see tLinearFitSVD test program). In the following
169// all four methods to calculate a polynomial through the data is used
170// <srcblock>
171// // The list of coordinate x-values
172// Vector<Double> x(nPrimes);
173// indgen((Array<Double>&)x, 1.0); // 1, 2, ...
174// Vector<Double> primesTable(nPrimes);
175// for (uInt i=1; i < nPrimes; i++) {
176// primesTable(i) =
177// Primes::nextLargerPrimeThan(Int(primesTable(i-1)+0.01));
178// };
179// Vector<Double> sigma(nPrimes);
180// sigma = 1.0;
181// // The fitter
182// LinearFit<Double> fitter;
183// Polynomial<AutoDiff<Double> > combination(2);
184// // Get the solution
185// fitter.setFunction(combination);
186// Vector<Double> solution = fitter.fit(x, primesTable, sigma);
187// // create a special function (should probably at beginning)
188// static void myfnc(Vector<Double> &y, const Double x) {
189// y(0) = 1; for (uInt i=1; i<y.nelements(); i++) y(i) = y(i-1)*x; };
190// fitter.setFunction(3, &myfnc);
191// solution = fitter.fit(x, primesTable, sigma);
192// // Create the direct coefficients table
193// fitter.setFunction(3);
194// Matrix<Double> xx(nPrimes, 3);
195// for (uInt i=0; i<nPrimes; i++) {
196// xx(i,0) = 1;
197// for (uInt j=1; j<3; j++) xx(i,j) = xx(i,j-1)*Double(i+1);
198// };
199// solution = fitter.fit(xx, primesTable, sigma);
200// </srcblock>
201// In the test program examples are given on how to get the other
202// information, and other examples.
203// </example>
204
205template<class T> class LinearFit : public GenericL2Fit<T>
206{
207public:
208 //# Constructors
209 // Create a fitter: the normal way to generate a fitter object. Necessary
210 // data will be deduced from the Functional provided with
211 // <src>setFunction()</src>
213 // Copy constructor (deep copy)
214 LinearFit(const LinearFit &other);
215 // Assignment (deep copy)
217
218 // Destructor
219 virtual ~LinearFit();
220
221 //# Member functions
222
223protected:
224 //#Data
225
226 //# Member functions
227 // Generalised fitter
228 virtual Bool fitIt
230 const Array<typename FunctionTraits<T>::BaseType> &x,
231 const Vector<typename FunctionTraits<T>::BaseType> &y,
232 const Vector<typename FunctionTraits<T>::BaseType> *const sigma,
233 const Vector<Bool> *const mask=0);
234
235private:
236 //# Data
237
238 //# Member functions
239
240protected:
241 //# Make members of parent classes known.
242 using GenericL2Fit<T>::pCount_p;
244 using GenericL2Fit<T>::sol_p;
245 using GenericL2Fit<T>::solved_p;
246 using GenericL2Fit<T>::nr_p;
247 using GenericL2Fit<T>::svd_p;
248 using GenericL2Fit<T>::condEq_p;
249 using GenericL2Fit<T>::err_p;
250 using GenericL2Fit<T>::errors_p;
254};
255
256
257} //# NAMESPACE CASACORE - END
258
259#ifndef CASACORE_NO_AUTO_TEMPLATES
260#include <casacore/scimath/Fitting/LinearFit.tcc>
261#endif //# CASACORE_NO_AUTO_TEMPLATES
262#endif
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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)
const Double COLLINEARITY
Default collinearity test for SVD.
Vector< typename FunctionTraits< T >::BaseType > err_p
Local error area.
void buildConstraint()
Build the constraint equations.
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.
static const String sol
Definition LSQFit.h:855
virtual ~LinearFit()
Destructor.
LinearFit & operator=(const LinearFit &other)
Assignment (deep copy)
LinearFit(const LinearFit &other)
Copy constructor (deep copy)
LinearFit()
Create a fitter: the normal way to generate a fitter object.
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.
this file contains all the compiler specific defines
Definition mainpage.dox:28
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