casacore
Loading...
Searching...
No Matches
GenericL2Fit.h
Go to the documentation of this file.
1//# GenericL2Fit.h: Generic base class for least-squares fit.
2//#
3//# Copyright (C) 2001,2002,2004,2005
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_GENERICL2FIT_H
28#define SCIMATH_GENERICL2FIT_H
29
30//# Includes
31#include <casacore/casa/aips.h>
32#include <casacore/casa/Arrays/Matrix.h>
33#include <casacore/casa/Arrays/Vector.h>
34#include <casacore/casa/Containers/Block.h>
35#include <casacore/scimath/Fitting/LSQaips.h>
36#include <casacore/scimath/Fitting/LSQTraits.h>
37#include <casacore/scimath/Functionals/Function.h>
38#include <casacore/scimath/Functionals/FunctionTraits.h>
39#include <casacore/scimath/Mathematics/AutoDiff.h>
40
41namespace casacore { // begin namespace casa
42
43//# Forward declarations
44template <class T, class U> class Function;
45
46// <summary> Generic base class for least-squares fit.
47// </summary>
48//
49// <reviewed reviewer="wbrouw" date="2004/06/14" tests="tLinearFitSVD.cc"
50// demos="">
51// </reviewed>
52//
53// <prerequisite>
54// <li> <linkto class="Function">Function</linkto>
55// <li> <linkto module="Fitting">Fitting</linkto>
56// </prerequisite>
57//
58// <etymology>
59// A set of data point is fit with some functional equation.
60// The class acts as a generic base class for <src>L2</src> type
61// fits.
62// </etymology>
63//
64// <synopsis>
65// NOTE: Constraints added. Documentation out of date at moment, check
66// the tLinearFitSVD and tNonLinearFitLM programs for examples.
67//
68// The class acts as a base class for L2-type (least-squares) fitting.
69// Actual classes (se e.g. <linkto class=LinearFit>LinearFit</linkto> and
70// <linkto class=NonLinearFit>NonLinearFit</linkto>.
71//
72// The following is a brief summary of the linear least-squares fit problem.
73// See module header, <linkto module="Fitting">Fitting</linkto>,
74// for a more complete description.
75//
76// Given a set of N data points (measurements), (x(i), y(i)) i = 0,...,N-1,
77// along with a set of standard deviations, sigma(i), for the data points,
78// and M specified functions, f(j)(x) j = 0,...,M-1, we form a linear
79// combination of the functions:
80// <srcblock>
81// z(i) = a(0)f(0)(x(i)) + a(1)f(1)(x(i)) + ... + a(M-1)f(M-1)(x(i)),
82// </srcblock>
83// where a(j) j = 0,...,M-1 are a set of parameters to be determined.
84// The linear least-squares fit tries to minimize
85// <srcblock>
86// chi-square = [(y(0)-z(0))/sigma(0)]^2 + [(y(1)-z(1))/sigma(1)]^2 + ...
87// + [(y(N-1)-z(N-1))/sigma(N-1)]^2.
88// </srcblock>
89// by adjusting {a(j)} in the equation.
90//
91// For complex numbers, <code>[(y(i)-z(i))/sigma(i)]^2</code> in chi-square
92// is replaced by
93// <code>[(y(i)-z(i))/sigma(i)]*conjugate([(y(i)-z(i))/sigma(i)])</code>
94//
95// For multidimensional functions, x(i) is a vector, and
96// <srcblock>
97// f(j)(x(i)) = f(j)(x(i,0), x(i,1), x(i,2), ...)
98// </srcblock>
99//
100// Normally, it is necessary that N > M for the solutions to be valid, since
101// there must be more data points than model parameters to be solved.
102//
103// If the measurement errors (standard deviation sigma) are not known
104// at all, they can all be set to one initially. In this case, we assume all
105// measurements have the same standard deviation, after minimizing
106// chi-square, we recompute
107// <srcblock>
108// sigma^2 = {(y(0)-z(0))^2 + (y(1)-z(1))^2 + ...
109// + (y(N-1)-z(N-1))^2}/(N-M) = chi-square/(N-M).
110// </srcblock>
111//
112// A statistic weight can also be assigned to each measurement if the
113// standard deviation is not available. sigma can be calculated from
114// <srcblock>
115// sigma = 1/ sqrt(weight)
116// </srcblock>
117// Alternatively a 'weight' switch can be set with <src>asWeight()</src>.
118// For best arithmetic performance, weight should be normalized to a maximum
119// value of one. Having a large weight value can sometimes lead to overflow
120// problems.
121//
122// The function to be fitted to the data can be given as an instance of the
123// <linkto class="Function">Function</linkto> class.
124// One can also form a sum of functions using the
125// <linkto class="CompoundFunction">CompoundFunction</linkto>.
126//
127// For small datasets the usage of the calls is:
128// <ul>
129// <li> Create a functional description of the parameters
130// <li> Create a fitter: GenericL2Fit<T> fitter();
131// <li> Set the functional representation: fitter.setFunction()
132// <li> Do the fit to the data: fitter.fit(x, data, sigma)
133// (or do a number of calls to buildNormalMatrix(x, data, sigma)
134// and finish of with fitter.fit() or fitter.sol())
135// <li> if needed the covariance; residuals; chiSquared, parameter errors
136// can all be obtained
137// </ul>
138// Note that the fitter is reusable. An example is given in the following.
139//
140// The solution of a fit always produces the total number of parameters given
141// to the fitter. I.e. including any parameters that were fixed. In the
142// latter case the solution returned will be the fixed value.
143//
144// <templating arg=T>
145// <li> The following data types can be used to instantiate the GenericL2Fit
146// templated class:
147// Known classes for FunctionTraits. I.e simple numerical like
148// <src>Float</src>, <src>Double</src>, <src>Complex</src>,
149// <src>DComplex</src>; and the <src>AutoDiff<></src> versions.
150// </templating>
151//
152// If there are a large number of unknowns or a large number of data points
153// machine memory limits (or timing reasons) may not allow a complete
154// in-core fitting to be performed. In this case one can incrementally
155// build the normal equation (see buildNormalMatrix()).
156//
157// The normal operation of the class tests for real inversion problems
158// only. If tests are needed for almost collinear columns in the
159// solution matrix, the collinearity can be set as the square of the sine of
160// the minimum angle allowed.
161//
162// Singular Value Decomposition is supported by the
163// <em> asSVD()</em> (which will also set the
164// default collinearity to 1e-8).
165//
166// Other information (see a.o. <linkto class=LSQFit>LSQFit</linkto>) can
167// be set and obtained as well.
168// </synopsis>
169//
170// <motivation>
171// The creation of this class was driven by the need to write code
172// to perform baseline fitting or continuum subtraction.
173// </motivation>
174
175// <example>
176// In the following a polynomial is fitted through the first 20 prime numbers.
177// The data is given in the x vector (1 to 20) and in the primesTable
178// (2, 3, ..., 71) (see tLinearFitSVD test program). In the following
179// all four methods to calculate a polynomial through the data is used
180// <srcblock>
181// // The list of coordinate x-values
182// Vector<Double> x(nPrimes);
183// indgen(x, 1.0); // 1, 2, ...
184// Vector<Double> primesTable(nPrimes);
185// for (uInt i=1; i < nPrimes; i++) {
186// primesTable(i) =
187// Primes::nextLargerPrimeThan(Int(primesTable(i-1)+0.01));
188// }
189// Vector<Double> sigma(nPrimes);
190// sigma = 1.0;
191// // The fitter
192// LinearFit<Double> fitter;
193// // Linear combination of functions describing 1 + x + x*x
194// combination.setCoefficient(0, 1.0); // 1
195// combination.setCoefficient(1, 1.0); // x
196// combination.setCoefficient(2, 1.0); // x^2
197// // Get the solution
198// fitter.setFunction(combination);
199// Vector<Double> solution = fitter.fit(x, primesTable, sigma);
200// // Try with a function with automatic derivatives (note that default
201// // polynomial has zero first guess)
202// LinearFit<AutoDiffA<Double> > fitad;
203// Polynomial<AutoDiffA<Double> > sqre(2);
204// fitad.setFunction(sqre);
205// solution = fitad.fit(x, primesTable, sigma);
206// </srcblock>
207// In the test program examples are given on how to get the other
208// information, and other examples.
209// </example>
210
211template<class T> class GenericL2Fit : public LSQaips {
212 public:
213 //# Constants
214 // Default collinearity test for SVD
216
217 //# Constructors
218 // Create a fitter: the normal way to generate a fitter object. Necessary
219 // data will be deduced from the Functional provided with
220 // <src>setFunction()</src>
222 // Copy constructor (deep copy)
224 // Assignment (deep copy)
226
227 // Destructor
228 virtual ~GenericL2Fit();
229
230 // Sets the function to be fitted. Upon entry, the argument function object
231 // is cloned. The cloned copy is used in the later fitting process.
232 // A valid function should be an instance of the
233 // <linkto class="Function">Function</linkto> class,
234 // so that derivatives with respect to the adjustable parameters
235 // can be calculated. The current values of the "available" parameters
236 // of the function are taken as the initial guess for the non-linear fitting.
237 template <class U>
238 void setFunction(const Function<U,U> &function) { resetFunction();
239 ptr_derive_p = function.cloneAD(); setFunctionEx(); }
240
241 // Set the possible constraint functions. The <src>addConstraint</src>
242 // will add one; the <src>setConstraint</src> will [re-]set the
243 // <src>n</src>th constraint. If unsucessful, False returned.<br>
244 // Constraint functional can only be set when the function to be fitted
245 // has been set. It should have the same number of parameters as the function
246 // to be fitted. The <src>x</src> should have the correct dimension.
247 // <group>
248 template <class U>
250 const Function<U,U> &function,
251 const Vector<typename FunctionTraits<T>::BaseType> &x,
252 const typename FunctionTraits<T>::BaseType y=
253 typename FunctionTraits<T>::BaseType(0)) {
254 if (n >= constrFun_p.nelements() ||
255 !ptr_derive_p ||
256 ptr_derive_p->nparameters() != function.nparameters() ||
257 function.ndim() != x.nelements()) return False;
258 delete constrFun_p[n]; constrFun_p[n] = 0;
259 constrFun_p[n] = function.cloneAD(); return setConstraintEx(n, x, y); }
261 const Vector<typename FunctionTraits<T>::BaseType> &x,
262 const typename FunctionTraits<T>::BaseType y=
263 typename FunctionTraits<T>::BaseType(0));
265 const typename FunctionTraits<T>::BaseType y=
266 typename FunctionTraits<T>::BaseType(0));
268 typename FunctionTraits<T>::DiffType> &function,
269 const Vector<typename FunctionTraits<T>::BaseType> &x,
270 const typename FunctionTraits<T>::BaseType y=
271 typename FunctionTraits<T>::BaseType(0));
273 const typename FunctionTraits<T>::BaseType y=
274 typename FunctionTraits<T>::BaseType(0));
276 typename FunctionTraits<T>::BaseType(0));
277 // </group>
278 // Set the collinearity factor as the square of the sine of the
279 // minimum angle allowed between input vectors (default zero for non-SVD,
280 // 1e-8 for SVD)
281 void setCollinearity(const Double cln);
282
283 // Set sigma values to be interpreted as weight (i.e. 1/sigma/sigma).
284 // A value of zero or -1 will be skipped. The switch will stay in effect
285 // until set False again explicitly. Default is False.
286 void asWeight(const Bool aswgt) { asweight_p = aswgt; }
287
288 // Set the use of SVD or not (default). When set the default collinearity
289 // is set as well.
290 void asSVD(const Bool svd);
291
292 // Return a pointer to the function being fitted. Should
293 // never delete this pointer.
294 // <group>
300 fittedFunction() const { return ptr_derive_p; }
301 // </group>
302 // Return the number of fitted parameters
303 uInt fittedNumber() const { return aCount_ai; }
304
305 // Return the number of constraints, and pointers to constraint functions.
306 // A <src>0-pointer</src> will be returned if no such constraint present.
307 // This pointer should never be destroyed.
308 // <group>
309 uInt NConstraints() { return constrFun_p.nelements(); }
312 return (n >= constrFun_p.nelements() ? 0 : constrFun_p[n]); }
313 // </group>
314
315 // Return the nth constraint equation derived from SVD
316 // Note that the number present will be given by <src>getDeficiency()</src>
318 BaseType>::base> getSVDConstraint(uInt n);
319 // Set the parameter values. The input is a vector of parameters; all
320 // or only the masked ones' values will be set, using the input values
321 // <group>
323 (const Vector<typename FunctionTraits<T>::BaseType> &parms);
325 (const Vector<typename FunctionTraits<T>::BaseType> &parms);
326 // </group>
327
328 // Fit the function to the data. If no sigma provided, all ones assumed.
329 // In the case of no x,y,sigma the fitting equations are supposed to be
330 // generated by previous calls to buildNormalMatrix. Note that the ones
331 // with a scalar sigma will assume sigma=1 (overloading problem). The mask
332 // assumes that if present, points with False will be skipped.
333 // <thrown>
334 // <li> AipsError if unmatched array sizes given
335 // <li> AipsError if equations cannot be inverted (not in SVD case and in
336 // the case of the Bool versions.)
337 // </thrown>
338 // <group>
341 const Vector<typename FunctionTraits<T>::BaseType> &y,
342 const Vector<typename FunctionTraits<T>::BaseType> &sigma,
343 const Vector<Bool> *const mask=0);
346 const Vector<typename FunctionTraits<T>::BaseType> &y,
347 const Vector<typename FunctionTraits<T>::BaseType> &sigma,
348 const Vector<Bool> *const mask=0);
351 const Vector<typename FunctionTraits<T>::BaseType> &y,
352 const Vector<Bool> *const mask=0);
355 const Vector<typename FunctionTraits<T>::BaseType> &y,
356 const Vector<Bool> *const mask=0);
358 fit(const Vector<Bool> *const mask=0);
360 const Vector<typename FunctionTraits<T>::BaseType> &x,
361 const Vector<typename FunctionTraits<T>::BaseType> &y,
362 const Vector<typename FunctionTraits<T>::BaseType> &sigma,
363 const Vector<Bool> *const mask=0);
365 const Matrix<typename FunctionTraits<T>::BaseType> &x,
366 const Vector<typename FunctionTraits<T>::BaseType> &y,
367 const Vector<typename FunctionTraits<T>::BaseType> &sigma,
368 const Vector<Bool> *const mask=0);
370 const Vector<typename FunctionTraits<T>::BaseType> &x,
371 const Vector<typename FunctionTraits<T>::BaseType> &y,
372 const typename FunctionTraits<T>::BaseType &sigma,
373 const Vector<Bool> *const mask=0);
375 const Matrix<typename FunctionTraits<T>::BaseType> &x,
376 const Vector<typename FunctionTraits<T>::BaseType> &y,
377 const typename FunctionTraits<T>::BaseType &sigma,
378 const Vector<Bool> *const mask=0);
380 const Vector<Bool> *const mask=0);
381 // </group>
382
383 // Obtain the chi squared. It has already been calculated during the
384 // fitting process.
385 // <group>
386 Double chiSquare() const { return getChi(); }
387 // </group>
388
389 // Get the errors on the solved values
390 // <thrown>
391 // <li> AipsError if none present (or Bool returned)
392 // </thrown>
393 // <group>
396 // </group>
397
398 // Get covariance matrix
399 // <group>
402 // </group>
403
404 // Generate the normal equations by one or more calls to the
405 // buildNormalMatrix(), before calling a fit() without arguments.
406 // The arguments are the same as for the fit(arguments) function.
407 // A False is returned if the Array sizes are unmatched.
408 // <group>
410 (const Vector<typename FunctionTraits<T>::BaseType> &x,
411 const Vector<typename FunctionTraits<T>::BaseType> &y,
412 const Vector<typename FunctionTraits<T>::BaseType> &sigma,
413 const Vector<Bool> *const mask=0);
415 (const Matrix<typename FunctionTraits<T>::BaseType> &x,
416 const Vector<typename FunctionTraits<T>::BaseType> &y,
417 const Vector<typename FunctionTraits<T>::BaseType> &sigma,
418 const Vector<Bool> *const mask=0);
420 (const Vector<typename FunctionTraits<T>::BaseType> &x,
421 const Vector<typename FunctionTraits<T>::BaseType> &y,
422 const Vector<Bool> *const mask=0);
424 (const Matrix<typename FunctionTraits<T>::BaseType> &x,
425 const Vector<typename FunctionTraits<T>::BaseType> &y,
426 const Vector<Bool> *const mask=0);
427 // </group>
428 // Return the residual after a fit in y. x can
429 // be a vector (if 1D function) or a matrix (ND functional), as in the
430 // fit() methods. If sol is given, it is the solution derived from
431 // a fit and its value will be used; otherwise only the parameters
432 // in the fitted functional will be used.
433 // If <src>model</src> is given as <src>True</src>, the model, rather
434 // the residual <src><data>-<model></src> will be returned in <src>y</src>.
435 // False is returned if residuals cannot be calculated.
436 // <thrown>
437 // <li> Aipserror if illegal array sizes
438 // </thrown>
439 // <group>
441 const Array<typename FunctionTraits<T>::BaseType> &x,
442 const Vector<typename FunctionTraits<T>::BaseType> &sol,
443 const Bool model=False);
445 const Array<typename FunctionTraits<T>::BaseType> &x,
446 const Bool model=False);
447 // </group>
448 // Get the rank of the solution (or zero of no fit() done yet). A
449 // valid solution will have the same rank as the number of unknowns (or
450 // double that number in the complex case). For SVD solutions the
451 // rank could be less.
452 uInt getRank() const {
453 return (solved_p ? nUnknowns()-getDeficiency() : 0); }
454
455 protected:
456 //#Data
457 // Adjustable
459 // SVD indicator
461 // Function to use in evaluating condition equation
464 // List of functions describing the possible constraint equations
465 // e.g. The sum of 3 angles w`could be described by a
466 // <src>HyperPlane(3)</src> function with <src>[1,1,1]</src>
467 // as parameters; giving <src>[1,1,1]</src> as argument vector and
468 // <src>3.1415</src> as value.
469 // <group>
472 // List of vectors describing the constraint equations' arguments
474 // List of values describing the constraint equations' value
476 // </group>
477 // Number of available parameters
479 // Number of dimensions of input data
481 // No normal equations yet.
483 // Have solution
485 // Have errors
488 // Interpret as weights rather than as sigma the given values.
490 // The rank of the solution
492 // Condition equation parameters (for number of adjustable parameters)
494 // Equation for all available parameters
496 // Contiguous argument areas
497 // <group>
500 // </group>
501 // Local solution area
502 // <group>
505 // </group>
506 // Local error area
507 // <group>
510 // </group>
511 // Local value and derivatives
513 // Local SVD constraints
515 BaseType>::base> > consvd_p;
516 //# Member functions
517 // Generalised fitter
518 virtual Bool fitIt
520 const Array<typename FunctionTraits<T>::BaseType> &x,
521 const Vector<typename FunctionTraits<T>::BaseType> &y,
522 const Vector<typename FunctionTraits<T>::BaseType> *const sigma,
523 const Vector<Bool> *const mask=0) = 0;
524 // Build the normal matrix
526 const Vector<typename FunctionTraits<T>::BaseType> &y,
527 const Vector<typename FunctionTraits<T>::BaseType>
528 *const sigma,
529 const Vector<Bool> *const mask=0);
530 // Build the constraint equations
532 // Get the SVD constraints
534 // Calculate residuals
536 const Array<typename FunctionTraits<T>::BaseType> &x,
537 const Vector<typename FunctionTraits<T>::BaseType>
538 *const sol, const Bool model=False);
539 // Function to get evaluated functional value
542 uInt j, uInt i) const;
543 // Initialise the fitter with number of solvable parameters
544 void initfit_p(uInt parcnt);
545 // Return number of condition equations and check sizes x, y, sigma
546 // <thrown>
547 // <li> Aipserror if size inconsistencies
548 // </thrown>
550 (const Array<typename FunctionTraits<T>::BaseType> &x,
551 const Vector<typename FunctionTraits<T>::BaseType> &y,
552 const Vector<typename FunctionTraits<T>::BaseType> *const sigma);
553 // Reset all the input
555
556 private:
557 //# Data
558
559 //# Member functions
560 // Set function properties
562 // Set Constraint properties
564 const Vector<typename FunctionTraits<T>::BaseType> &x,
565 const typename FunctionTraits<T>::BaseType y);
566};
567
568} //# End namespace casacore
569#ifndef CASACORE_NO_AUTO_TEMPLATES
570#include <casacore/scimath/Fitting/GenericL2Fit.tcc>
571#endif //# CASACORE_NO_AUTO_TEMPLATES
572#endif
AutoDiff< T > DiffType
Default type for differentiation.
T BaseType
Template base type.
virtual uInt ndim() const =0
Returns the number of dimensions of function.
uInt nparameters() const
Returns the number of parameters.
Definition Function.h:228
virtual Function< typename FunctionTraits< T >::DiffType > * cloneAD() const
uInt ndim_p
Number of dimensions of input data.
GenericL2Fit(const GenericL2Fit &other)
Copy constructor (deep copy)
void resetFunction()
Reset all the input.
Bool solved_p
Have solution.
void compuCovariance(Matrix< Double > &cov)
void setParameterValues(const Vector< typename FunctionTraits< T >::BaseType > &parms)
Set the parameter values.
const Vector< typename FunctionTraits< T >::BaseType > & errors() const
Get the errors on the solved values.
Bool errors(Vector< typename FunctionTraits< T >::BaseType > &err) const
Vector< Vector< typename LSQTraits< typename FunctionTraits< T >::BaseType >::base > > consvd_p
Local SVD constraints.
Bool needInit_p
No normal equations yet.
void fillSVDConstraints()
Get the SVD constraints.
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)
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.
uInt nr_p
The rank of the solution.
Vector< typename FunctionTraits< T >::BaseType > condEq_p
Condition equation parameters (for number of adjustable parameters)
Bool addConstraint(const typename FunctionTraits< T >::BaseType y=typename FunctionTraits< T >::BaseType(0))
void asSVD(const Bool svd)
Set the use of SVD or not (default).
void buildNormalMatrix(const Matrix< typename FunctionTraits< T >::BaseType > &x, const Vector< typename FunctionTraits< T >::BaseType > &y, const Vector< Bool > *const mask=0)
Function< typename FunctionTraits< T >::DiffType, typename FunctionTraits< T >::DiffType > * fittedFunction()
Return a pointer to the function being fitted.
const Double COLLINEARITY
Default collinearity test for SVD.
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 > err_p
Local error area.
Vector< typename FunctionTraits< T >::ArgType > carg_p
uInt aCount_ai
Adjustable.
Vector< typename FunctionTraits< T >::BaseType > fit(const Vector< Bool > *const mask=0)
void buildConstraint()
Build the constraint equations.
Vector< typename FunctionTraits< T >::BaseType > ferr_p
PtrBlock< Function< typename FunctionTraits< T >::DiffType, typename FunctionTraits< T >::DiffType > * > constrFun_p
List of functions describing the possible constraint equations e.g.
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)
uInt getRank() const
Get the rank of the solution (or zero of no fit() done yet).
Double chiSquare() const
Obtain the chi squared.
Bool setConstraint(const uInt n, const Vector< typename FunctionTraits< T >::BaseType > &x, const typename FunctionTraits< T >::BaseType y=typename FunctionTraits< T >::BaseType(0))
void asWeight(const Bool aswgt)
Set sigma values to be interpreted as weight (i.e.
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)
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.
GenericL2Fit & operator=(const GenericL2Fit &other)
Assignment (deep copy)
FunctionTraits< T >::BaseType getVal_p(const Array< typename FunctionTraits< T >::BaseType > &x, uInt j, uInt i) const
Function to get evaluated functional value.
Bool fit(Vector< typename FunctionTraits< T >::BaseType > &sol, const Vector< Bool > *const mask=0)
uInt pCount_p
Number of available parameters.
Vector< typename FunctionTraits< T >::BaseType > sol_p
Local solution area.
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 asweight_p
Interpret as weights rather than as sigma the given values.
GenericL2Fit()
Create a fitter: the normal way to generate a fitter object.
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)
uInt fittedNumber() const
Return the number of fitted parameters.
void setFunction(const Function< U, U > &function)
Sets the function to be fitted.
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 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.
Bool svd_p
SVD indicator.
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()...
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.
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.
uInt NConstraints()
Return the number of constraints, and pointers to constraint functions.
virtual ~GenericL2Fit()
Destructor.
Bool addConstraint(const Vector< typename FunctionTraits< T >::BaseType > &x, const typename FunctionTraits< T >::BaseType y=typename FunctionTraits< T >::BaseType(0))
Bool residual(Vector< typename FunctionTraits< T >::BaseType > &y, const Array< typename FunctionTraits< T >::BaseType > &x, const Bool model=False)
Bool errors_p
Have errors.
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)
Function< typename FunctionTraits< T >::DiffType, typename FunctionTraits< T >::DiffType > * ptr_derive_p
Function to use in evaluating condition equation.
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.
void setCollinearity(const Double cln)
Set the collinearity factor as the square of the sine of the minimum angle allowed between input vect...
Bool setConstraint(const uInt n, const typename FunctionTraits< T >::BaseType y=typename FunctionTraits< T >::BaseType(0))
const Function< typename FunctionTraits< T >::DiffType, typename FunctionTraits< T >::DiffType > * fittedFunction() const
Bool setConstraintEx(const uInt n, const Vector< typename FunctionTraits< T >::BaseType > &x, const typename FunctionTraits< T >::BaseType y)
Set Constraint properties.
PtrBlock< Vector< typename FunctionTraits< T >::BaseType > * > constrArg_p
List of vectors describing the constraint equations' arguments.
void setFunctionEx()
Set function properties.
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)
void initfit_p(uInt parcnt)
Initialise the fitter with number of solvable parameters.
Vector< typename FunctionTraits< T >::BaseType > fsol_p
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.
PtrBlock< typename FunctionTraits< T >::BaseType * > constrVal_p
List of values describing the constraint equations' value.
Matrix< Double > compuCovariance()
Get covariance matrix.
Function< typename FunctionTraits< T >::DiffType, typename FunctionTraits< T >::DiffType > * getConstraint(const uInt n)
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.
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 get...
void setMaskedParameterValues(const Vector< typename FunctionTraits< T >::BaseType > &parms)
uInt nUnknowns() const
Get the number of unknowns.
Definition LSQFit.h:767
Double getChi() const
Get chi^2 (both are identical); the standard deviation (per observation) and the standard deviation p...
static const String sol
Definition LSQFit.h:855
uInt getDeficiency() const
Get the rank deficiency Warning: Note that the number is returned assuming real values; For complex ...
Definition LSQFit.h:773
A drop-in replacement for Block<T*>.
Definition Block.h:812
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