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