00001 00002 // ----------------------------------------------------------------------------- 00003 00004 /* 00005 00006 CalStatsFitter.h 00007 00008 Description: 00009 ------------ 00010 This header file contains definitions for the CalStatsFitter class. 00011 00012 Classes: 00013 -------- 00014 CalStatsFitter - This class contains the fit statistics static enums and 00015 functions. 00016 00017 Modification history: 00018 --------------------- 00019 2011 Dec 08 - Nick Elias, NRAO 00020 Initial version created. 00021 2012 Jan 25 - Nick Elias, NRAO 00022 Error checking added. 00023 00024 */ 00025 00026 // ----------------------------------------------------------------------------- 00027 // Start of define macro to prevent multiple loading 00028 // ----------------------------------------------------------------------------- 00029 00030 #ifndef CAL_STATS_FITTER_H 00031 #define CAL_STATS_FITTER_H 00032 00033 // ----------------------------------------------------------------------------- 00034 // Includes 00035 // ----------------------------------------------------------------------------- 00036 00037 #include <casa/aips.h> 00038 00039 #include <casa/Exceptions/Error.h> 00040 00041 #include <casa/Arrays/Vector.h> 00042 00043 #include <casa/Arrays/ArrayMath.h> 00044 #include <casa/Arrays/ArrayLogical.h> 00045 00046 #include <casa/Arrays/MaskedArray.h> 00047 00048 #include <scimath/Functionals/Polynomial.h> 00049 #include <scimath/Mathematics/AutoDiff.h> 00050 #include <scimath/Fitting/LinearFitSVD.h> 00051 00052 // ----------------------------------------------------------------------------- 00053 // Start of casa namespace 00054 // ----------------------------------------------------------------------------- 00055 00056 namespace casa { 00057 00058 // ----------------------------------------------------------------------------- 00059 // Start of CalStatsFitter class definition 00060 // ----------------------------------------------------------------------------- 00061 00062 /* 00063 00064 CalStatsFitter 00065 00066 Description: 00067 ------------ 00068 This class contains the fit statistics static enums and functions. 00069 00070 NB: At present, this class acts as a namespace for static functions. 00071 00072 Classes: 00073 -------- 00074 CalStatsFitter - This class contains the fit statistics enums and functions. 00075 00076 Class public static member functions: 00077 ------------------------------------- 00078 fit - This member function is the fitting interface of this class. 00079 orderName - This function returns the string corresponding to the 00080 CalStatsFitter::ORDER enum. 00081 typeName - This function returns the string corresponding to the 00082 CalStatsFitter::TYPE enum. 00083 weightName - This function returns the string corresponding to the 00084 CalStatsFitter::WEIGHT enum. 00085 00086 Class private static member functions (least-squares fitting): 00087 -------------------------------------------------------------- 00088 lsqFit - This member function calculates polynomial least-squares fits. 00089 00090 Class private static member functions (robust fitting): 00091 ------------------------------------------------------- 00092 robustFit - This member function calculates polynomial robust fits. 00093 slope - This member function calculates the robust slope. 00094 brackFunc - This member function is root-finding bracketing function used to 00095 determine the slope. 00096 signum - This member function calculates the signum function (scalar). 00097 signum - This member function calculates the signum function (vector). 00098 theil - This member function estimates the slope and slope error using 00099 Theil's method. 00100 00101 Modification history: 00102 --------------------- 00103 2011 Dec 08 - Nick Elias, NRAO 00104 Initial version created with enums ORDER, TYPE, and WEIGHT; public 00105 static member functions init() and fit(); and private static 00106 member functions lsqFit(), robustFit(), numDataWeight(), slope(), 00107 brackFunc(), signum() (scalar), and signum() (vector). 00108 2011 Dec 11 - Nick Elias, NRAO 00109 Added value, value error, and model vectors to the FIT structure. 00110 Added dealloc() (pointer) and dealloc() (reference) public static 00111 member functions. 00112 2011 Dec 21 - Nick Elias, NRAO 00113 Public static member functions init() and dealloc() removed 00114 because all of their duties are subsumed by the nested class 00115 FIT (it used used to be a structure). 00116 2012 Jan 24 - Nick Elias, NRAO 00117 Private static member function theil() added. Private static 00118 member function numDataWeight() removed because initial robust 00119 estimates of fit parameters (before final "trimmed" least squares) 00120 no longer employ weighting. 00121 2012 Mar 06 - Nick Elias, NRAO 00122 Static public member functions orderName(), typeName(), and 00123 weightName() added. 00124 2012 Mar 15 - Nick Elias, NRAO 00125 Public members dResVar and dResMean added to the nested FIT class. 00126 00127 */ 00128 00129 // ----------------------------------------------------------------------------- 00130 00131 class CalStatsFitter { 00132 00133 public: 00134 00135 // Order enums 00136 typedef enum ORDER { 00137 ORDER_INIT=-1, AVERAGE=0, LINEAR, QUADRATIC 00138 } ORDER; 00139 00140 // Type enums 00141 typedef enum TYPE { 00142 TYPE_INIT=-1, LSQ=0, ROBUST 00143 } TYPE; 00144 00145 // Weight enums 00146 typedef enum WEIGHT { 00147 WEIGHT_INIT=-1, NO=False, YES=True 00148 } WEIGHT; 00149 00150 // FIT nested class 00151 class FIT { 00152 public: 00153 ORDER eOrder; // Fit order 00154 TYPE eType; // Fit type 00155 WEIGHT eWeight; // Fit weight 00156 Bool bValid; // Fit validity boolean 00157 Vector<Double> oPars; // Fit parameters 00158 Matrix<Double> oCovars; // Fit parameter covariances 00159 Vector<Double> oModel; // Fit model 00160 Vector<Double> oRes; // Fit residuals 00161 Double dResVar; // Fit variance of residuals 00162 Double dResMean; // Fit mean of residuals 00163 Double dRedChi2; // Fit reduced chi^2 00164 FIT( void ); 00165 FIT( const FIT& oFit ); 00166 ~FIT( void ); 00167 FIT& operator=( const FIT& oFit ); 00168 }; 00169 00170 // User interface to all fitting capabilities 00171 static FIT& fit( const Vector<Double>& oAbs, const Vector<Double>& oValue, 00172 const Vector<Double>& oValueErr, Vector<Bool>& oFlag, 00173 const ORDER& eOrder, const TYPE& eType, const WEIGHT& eWeight ); 00174 00175 // The enum names 00176 static String& orderName( const ORDER& eOrder ); 00177 static String& typeName( const TYPE& eOrder ); 00178 static String& weightName( const WEIGHT& eOrder ); 00179 00180 private: 00181 00182 // Least-squares fitting function 00183 static FIT& lsqFit( const Vector<Double>& oAbs, 00184 const Vector<Double>& oValue, const Vector<Double>& oValueErr, 00185 Vector<Bool>& oFlag, const ORDER& eOrder, const WEIGHT& eWeight ); 00186 00187 // Robust fitting function 00188 static FIT& robustFit( const Vector<Double>& oAbs, 00189 const Vector<Double>& oValue, const Vector<Double>& oValueErr, 00190 Vector<Bool>& oFlag, const ORDER& eOrder, const WEIGHT& eWeight, 00191 const Double& dTrim ); 00192 00193 // Functions required to calculate a robust linear fit 00194 static Double& slope( const Vector<Double>& oAbs, 00195 const Vector<Double>& oValue, const Double& dSlope, 00196 const Double& dSlopeErr, const Double& dFudge, const uInt& uiNumSlope, 00197 const uInt& uiNumIter ); 00198 static Double& brackFunc( const Vector<Double>& oAbs, 00199 const Vector<Double>& oValue, const Double& dSlope ); 00200 00201 // Signum functions (also required to calculate a robust linear fit) 00202 static Double& signum( const Double& dValue ); 00203 static Vector<Double>& signum( const Vector<Double>& oValue ); 00204 00205 // Function for calculating slope estimate using Theil's method 00206 static void theil( const Vector<Double>& oAbs, const Vector<Double>& oValue, 00207 Double& dSlope, Double& dSlopeErr ); 00208 00209 }; 00210 00211 // ----------------------------------------------------------------------------- 00212 // End of CalStatsFitter class definition 00213 // ----------------------------------------------------------------------------- 00214 00215 }; 00216 00217 // ----------------------------------------------------------------------------- 00218 // End of casa namespace 00219 // ----------------------------------------------------------------------------- 00220 00221 #endif 00222 00223 // ----------------------------------------------------------------------------- 00224 // End of define macro to prevent multiple loading 00225 // -----------------------------------------------------------------------------