StatisticsUtilities.h

Go to the documentation of this file.
00001 //# Copyright (C) 2000,2001
00002 //# Associated Universities, Inc. Washington DC, USA.
00003 //#
00004 //# This library is free software; you can redistribute it and/or modify it
00005 //# under the terms of the GNU Library General Public License as published by
00006 //# the Free Software Foundation; either version 2 of the License, or (at your
00007 //# option) any later version.
00008 //#
00009 //# This library is distributed in the hope that it will be useful, but WITHOUT
00010 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00012 //# License for more details.
00013 //#
00014 //# You should have received a copy of the GNU Library General Public License
00015 //# along with this library; if not, write to the Free Software Foundation,
00016 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00017 //#
00018 //# Correspondence concerning AIPS++ should be addressed as follows:
00019 //#        Internet email: aips2-request@nrao.edu.
00020 //#        Postal address: AIPS++ Project Office
00021 //#                        National Radio Astronomy Observatory
00022 //#                        520 Edgemont Road
00023 //#                        Charlottesville, VA 22903-2475 USA
00024 //#
00025 //# $Id: Array.h 21545 2015-01-22 19:36:35Z gervandiepen $
00026 
00027 #ifndef SCIMATH_STATISTICSUTILITIES_H
00028 #define SCIMATH_STATISTICSUTILITIES_H
00029 
00030 #include <casacore/casa/Exceptions/Error.h>
00031 #include <casacore/scimath/Mathematics/StatisticsTypes.h>
00032 #include <casacore/casa/Utilities/DataType.h>
00033 #include <casacore/casa/aips.h>
00034 
00035 #include <iostream>
00036 #include <casacore/casa/iosfwd.h>
00037 
00038 namespace casacore {
00039 
00040 // Various statistics related methods for the statistics framework.
00041 
00042 template <class AccumType> class StatisticsUtilities {
00043 public:
00044 
00045         // description of a regularly spaced bins with the first bin having lower limit
00046         // of minLimit and having nBins equally spaced bins of width binWidth, so that
00047         // the upper limit of the last bin is given by minLimit + nBins*binWidth
00048         struct BinDesc {
00049                 AccumType binWidth;
00050                 AccumType minLimit;
00051                 uInt nBins;
00052         };
00053 
00054         ~StatisticsUtilities() {}
00055 
00056         // <group>
00057         // accumulate values. It is the responsibility of the caller to keep track
00058         // of the accumulated values after each call. This class does not since it
00059         // has no state.
00060         // The accumulation derivation for mean and variance can be found at
00061         // www.itl.nist.gov/div898/software/dataplot/refman2/ch2/weighvar.pdf
00062         // nvariance is an accumulated value. It is related to the variance via
00063         // variance = nvariance/npts or nvariance/(npts-1) depending on your preferred definition
00064         // in the non-weighted case and
00065         // wvariance = wnvariance/sumofweights or wnvariance/(sumofweights-1) in the weighted case
00066         // It's basic definition is nvariance = sum((x_i - mean)**2),
00067         // wnvariance = sum((weight_i*(x_i - mean)**2)
00068         // npts is a Double rather than an Int64 because of compilation issues when T is a Complex
00069         inline static void accumulate (
00070                 Double& npts, AccumType& sum, AccumType& mean, const AccumType& datum
00071         );
00072 
00073         // in order to optimize performance, no checking is done for the weight == 0 case
00074         // callers should ensure that the weigth is not zero before calling this method,
00075         // and shouldn't call this method if the weight is 0. Expect a segfault because of
00076         // division by zero if sumweights and weight are both zero.
00077         inline static void waccumulate (
00078                 Double& npts, AccumType& sumweights, AccumType& wsum, AccumType& wmean,
00079                 const AccumType& datum, const AccumType& weight
00080         );
00081 
00082         inline static void accumulate (
00083                 Double& npts, AccumType& sum, AccumType& mean, AccumType& nvariance,
00084                 AccumType& sumsq, const AccumType& datum
00085         );
00086 
00087         // wsumsq is the weighted sum of squares, sum(w_i*x_i*x_i)
00088         inline static void waccumulate (
00089                 Double& npts, AccumType& sumweights, AccumType& wsum,
00090                 AccumType& wmean, AccumType& wnvariance, AccumType& wsumsq,
00091                 const AccumType& datum, const AccumType& weight
00092         );
00093         // </group>
00094 
00095         // <group>
00096         // The assignment operator of class LocationType should use copy, not reference,
00097         // semantics.
00098         template <class LocationType>
00099         inline static void accumulate (
00100                 Double& npts, AccumType& sum, AccumType& mean, AccumType& nvariance,
00101                 AccumType& sumsq, AccumType& datamin,
00102                 AccumType& datamax, LocationType& minpos, LocationType& maxpos,
00103                 const AccumType& datum, const LocationType& location
00104         );
00105 
00106         template <class LocationType>
00107         inline static void waccumulate (
00108                 Double& npts, AccumType& sumofweights, AccumType& sum, AccumType& mean,
00109                 AccumType& nvariance, AccumType& sumsq, AccumType& datamin, AccumType& datamax,
00110                 LocationType& minpos, LocationType& maxpos,
00111                 const AccumType& datum, const AccumType& weight, const LocationType& location
00112         );
00113 
00114         // </group>
00115 
00116         // <group>
00117         // return True if the max or min was updated, False otherwise.
00118         template <class LocationType>
00119         inline static Bool doMax(
00120                 AccumType& datamax, LocationType& maxpos, Bool isFirst,
00121                 const AccumType& datum, const LocationType& location
00122         );
00123 
00124         template <class LocationType>
00125         inline static Bool doMin(
00126                 AccumType& datamin, LocationType& minpos, Bool isFirst,
00127                 const AccumType& datum, const LocationType& location
00128         );
00129         // </group>
00130 
00131         // <group>
00132         // These versions are for symmetric accumulation about a specified center
00133         // point. The actual point is accumulated, as is a "virtual" point that is
00134         // symmetric about the specified center. Of course, the trivial relationship
00135         // that the mean is the specified center is used to simplify things.
00136         /*
00137         inline static void accumulateSym (
00138                 Double& npts, AccumType& sum, const AccumType& datum, const AccumType& center
00139         );
00140         */
00141 
00142         /*
00143         inline static void waccumulateSym (
00144                 Double& npts, AccumType& sumweights, AccumType& wsum,
00145                 const AccumType& datum, const AccumType& weight, const AccumType& center
00146         );
00147         */
00148 
00149         inline static void accumulateSym (
00150                 Double& npts, AccumType& nvariance,
00151                 AccumType& sumsq, const AccumType& datum, const AccumType& center
00152         );
00153 
00154         // wsumsq is the weighted sum of squares, sum(w_i*x_i*x_i)
00155         inline static void waccumulateSym (
00156                 Double& npts, AccumType& sumweights,
00157                 AccumType& wnvariance, AccumType& wsumsq,
00158                 const AccumType& datum, const AccumType& weight, const AccumType& center
00159         );
00160 
00161         // <src>maxpos</src> and <src>minpos</src> refer to actual, not
00162         // virtually created, data only.
00163         template <class LocationType>
00164         inline static void accumulateSym (
00165                 Double& npts, AccumType& nvariance,
00166                 AccumType& sumsq, AccumType& datamin,
00167                 AccumType& datamax, LocationType& minpos, LocationType& maxpos,
00168                 const AccumType& datum, const LocationType& location, const AccumType& center
00169         );
00170 
00171         template <class LocationType>
00172         inline static void waccumulateSym (
00173                 Double& npts, AccumType& sumofweights,
00174                 AccumType& nvariance, AccumType& sumsq, AccumType& datamin, AccumType& datamax,
00175                 LocationType& minpos, LocationType& maxpos,
00176                 const AccumType& datum, const AccumType& weight, const LocationType& location,
00177                 const AccumType& center
00178         );
00179 
00180         // </group>
00181         // This does the obvious conversions. The Complex and DComplex versions
00182         // (implemented after the class definition) are used solely to permit compilation. In general, these versions should
00183         // never actually be called
00184         inline static Int getInt(const AccumType& v) {
00185                 return (Int)v;
00186         }
00187 
00188         inline static Bool includeDatum(
00189                 const AccumType& datum, typename DataRanges::const_iterator beginRange,
00190                 typename DataRanges::const_iterator endRange, Bool isInclude
00191         );
00192 
00193     // use two statistics sets to get the statistics set that would
00194     // result in combining the two data sets used to produce the
00195     // individual statistics sets. The quantile related stats are
00196     // not considered, since it is not in general possible to determine
00197     // the resultant quantiles from the information provided; only
00198     // the aggregate statistics make sense.
00199     static StatsData<AccumType> combine(
00200         const vector<StatsData<AccumType> >& stats
00201     );
00202 
00203 private:
00204 
00205         const static AccumType TWO;
00206 
00207         StatisticsUtilities() {}
00208 
00209 };
00210 
00211 // The Complex and DComplex versions
00212 // are used solely to permit compilation. In general, these versions should
00213 // never actually be called
00214 template<>
00215 inline Int StatisticsUtilities<casacore::Complex>::getInt(const casacore::Complex&) {
00216         ThrowCc("This version for complex data types should never be called");
00217 }
00218 
00219 template<>
00220 inline Int StatisticsUtilities<casacore::DComplex>::getInt(const casacore::DComplex&) {
00221         ThrowCc("Logic Error: This version for complex data types should never be called");
00222 }
00223 
00224 template <class T>
00225 ostream &operator<<(ostream &os, const typename StatisticsUtilities<T>::BinDesc &desc) {
00226         os << "min limit " << desc.minLimit << " bin width " << desc.binWidth
00227                 << " nbins " << desc.nBins;
00228         return os;
00229 }
00230 
00231 }
00232 
00233 #ifndef CASACORE_NO_AUTO_TEMPLATES
00234 #include <casacore/scimath/Mathematics/StatisticsUtilities.tcc>
00235 #endif //# CASACORE_NO_AUTO_TEMPLATES
00236 
00237 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1