casacore
Loading...
Searching...
No Matches
StatisticsUtilities.h
Go to the documentation of this file.
1// # Copyright (C) 2000,2001
2// # Associated Universities, Inc. Washington DC, USA.
3// #
4// # This library is free software; you can redistribute it and/or modify it
5// # under the terms of the GNU Library General Public License as published by
6// # the Free Software Foundation; either version 2 of the License, or (at your
7// # option) any later version.
8// #
9// # This library is distributed in the hope that it will be useful, but WITHOUT
10// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12// # License for more details.
13// #
14// # You should have received a copy of the GNU Library General Public License
15// # along with this library; if not, write to the Free Software Foundation,
16// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
17// #
18// # Correspondence concerning AIPS++ should be addressed as follows:
19// # Internet email: casa-feedback@nrao.edu.
20// # Postal address: AIPS++ Project Office
21// # National Radio Astronomy Observatory
22// # 520 Edgemont Road
23// # Charlottesville, VA 22903-2475 USA
24// #
25
26#ifndef SCIMATH_STATISTICSUTILITIES_H
27#define SCIMATH_STATISTICSUTILITIES_H
28
29#include <casacore/casa/Exceptions/Error.h>
30#include <casacore/scimath/StatsFramework/StatisticsTypes.h>
31#include <casacore/scimath/StatsFramework/StatsHistogram.h>
32#include <casacore/casa/Utilities/DataType.h>
33#include <casacore/casa/aips.h>
34
35#include <iostream>
36#include <casacore/casa/iosfwd.h>
37
38namespace casacore {
39
41
42// Various statistics related methods for the statistics framework.
43
44template <class AccumType>
46 public:
48
50
51 // <group>
52 // accumulate values. It is the responsibility of the caller to keep track
53 // of the accumulated values after each call. This class does not since it
54 // has no state. The accumulation derivation for mean and variance can be
55 // found at
56 // www.itl.nist.gov/div898/software/dataplot/refman2/ch2/weighvar.pdf
57 // nvariance is an accumulated value. It is related to the variance via
58 // variance = nvariance/npts or nvariance/(npts-1) depending on your
59 // preferred definition in the non-weighted case and
60 // wvariance = wnvariance/sumofweights or wnvariance/(sumofweights-1)
61 // in the weighted case Its basic definition is
62 // nvariance = sum((x_i - mean)**2),
63 // wnvariance = sum((weight_i*(x_i - mean)**2)
64 // npts is a Double rather than an Int64 because of compilation issues when
65 // T is a Complex
66 inline static void accumulate(Double& npts, AccumType& sum, AccumType& mean,
67 const AccumType& datum);
68
69 // in order to optimize performance, no checking is done for the
70 // weight == 0 case callers should ensure that the weigth is not zero before
71 // calling this method, and shouldn't call this method if the weight is 0.
72 // Expect a segfault because of division by zero if sumweights and weight
73 // are both zero.
74 inline static void waccumulate(Double& npts, AccumType& sumweights, AccumType& wsum,
75 AccumType& wmean, const AccumType& datum, const AccumType& weight);
76
77 inline static void accumulate(Double& npts, AccumType& sum, AccumType& mean, AccumType& nvariance,
78 AccumType& sumsq, const AccumType& datum);
79
80 // wsumsq is the weighted sum of squares, sum(w_i*x_i*x_i)
81 inline static void waccumulate(Double& npts, AccumType& sumweights, AccumType& wsum,
82 AccumType& wmean, AccumType& wnvariance, AccumType& wsumsq,
83 const AccumType& datum, const AccumType& weight);
84 // </group>
85
86 // <group>
87 // The assignment operator of class LocationType should use copy, not
88 // reference, semantics.
89 template <class LocationType>
90 inline static void accumulate(Double& npts, AccumType& sum, AccumType& mean, AccumType& nvariance,
91 AccumType& sumsq, AccumType& datamin, AccumType& datamax,
92 LocationType& minpos, LocationType& maxpos, const AccumType& datum,
93 const LocationType& location);
94
95 template <class LocationType, class DataType>
96 inline static void accumulate(Double& npts, AccumType& sum, AccumType& mean, AccumType& nvariance,
97 AccumType& sumsq, DataType& datamin, DataType& datamax,
98 LocationType& minpos, LocationType& maxpos, const DataType& datum,
99 const LocationType& location);
100
101 template <class LocationType>
102 inline static void waccumulate(Double& npts, AccumType& sumofweights, AccumType& sum,
103 AccumType& mean, AccumType& nvariance, AccumType& sumsq,
104 AccumType& datamin, AccumType& datamax, LocationType& minpos,
105 LocationType& maxpos, const AccumType& datum,
106 const AccumType& weight, const LocationType& location);
107 // </group>
108
109 // <group>
110 // return True if the max or min was updated, False otherwise.
111 template <class LocationType>
112 inline static Bool doMax(AccumType& datamax, LocationType& maxpos, Bool isFirst,
113 const AccumType& datum, const LocationType& location);
114
115 template <class LocationType>
116 inline static Bool doMin(AccumType& datamin, LocationType& minpos, Bool isFirst,
117 const AccumType& datum, const LocationType& location);
118 // </group>
119
120 // <group>
121 // These versions are for symmetric accumulation about a specified center
122 // point. The actual point is accumulated, as is a "virtual" point that is
123 // symmetric about the specified center. Of course, the trivial relationship
124 // that the mean is the specified center is used to simplify things
125 inline static void accumulateSym(Double& npts, AccumType& nvariance, AccumType& sumsq,
126 const AccumType& datum, const AccumType& center);
127
128 // wsumsq is the weighted sum of squares, sum(w_i*x_i*x_i)
129 inline static void waccumulateSym(Double& npts, AccumType& sumweights, AccumType& wnvariance,
130 AccumType& wsumsq, const AccumType& datum,
131 const AccumType& weight, const AccumType& center);
132
133 // <src>maxpos</src> and <src>minpos</src> refer to actual, not
134 // virtually created, data only.
135 template <class LocationType>
136 inline static void accumulateSym(Double& npts, AccumType& nvariance, AccumType& sumsq,
137 AccumType& datamin, AccumType& datamax, LocationType& minpos,
138 LocationType& maxpos, const AccumType& datum,
139 const LocationType& location, const AccumType& center);
140
141 template <class LocationType>
142 inline static void waccumulateSym(Double& npts, AccumType& sumofweights, AccumType& nvariance,
143 AccumType& sumsq, AccumType& datamin, AccumType& datamax,
144 LocationType& minpos, LocationType& maxpos,
145 const AccumType& datum, const AccumType& weight,
146 const LocationType& location, const AccumType& center);
147
148 // convert in place by taking the absolute value of the difference of the
149 // std::vector and the median
150 inline static void convertToAbsDevMedArray(DataArray& myArray, AccumType median);
151 // </group>
152
153 inline static Bool includeDatum(const AccumType& datum,
154 typename DataRanges::const_iterator beginRange,
155 typename DataRanges::const_iterator endRange, Bool isInclude);
156
157 // The array can be changed by partially sorting it up to the largest index.
158 // Return a map of index to value in the sorted array.
159 static std::map<uInt64, AccumType> indicesToValues(std::vector<AccumType>& myArray,
160 const std::set<uInt64>& indices);
161
162 static void mergeResults(
163 std::vector<BinCountArray>& bins, std::vector<std::shared_ptr<AccumType>>& sameVal,
164 std::vector<Bool>& allSame, const std::unique_ptr<std::vector<BinCountArray>[]>& tBins,
165 const std::unique_ptr<std::vector<std::shared_ptr<AccumType>>[]>& tSameVal,
166 const std::unique_ptr<std::vector<Bool>[]>& tAllSame, uInt nThreadsMax);
167
168 // use two statistics sets to get the statistics set that would
169 // result in combining the two data sets used to produce the
170 // individual statistics sets. The quantile related stats are
171 // not considered, since it is not in general possible to determine
172 // the resultant quantiles from the information provided; only
173 // the aggregate statistics make sense.
174 static StatsData<AccumType> combine(const std::vector<StatsData<AccumType>>& stats);
175
176 template <class DataIterator, class MaskIterator, class WeightsIterator>
177 static uInt nThreadsMax(const StatsDataProvider<CASA_STATP>* const dataProvider);
178
179 static uInt threadIdx();
180
181 private:
182 const static AccumType TWO;
183};
184
185} // namespace casacore
186
187#ifndef CASACORE_NO_AUTO_TEMPLATES
188#include <casacore/scimath/StatsFramework/StatisticsUtilities.tcc>
189#endif // # CASACORE_NO_AUTO_TEMPLATES
190
191#endif
#define DataArray
Commonly used types in statistics framework.
#define CASA_STATD
because the template signature has become unwieldy
static void accumulate(Double &npts, AccumType &sum, AccumType &mean, const AccumType &datum)
accumulate values.
static void mergeResults(std::vector< BinCountArray > &bins, std::vector< std::shared_ptr< AccumType > > &sameVal, std::vector< Bool > &allSame, const std::unique_ptr< std::vector< BinCountArray >[]> &tBins, const std::unique_ptr< std::vector< std::shared_ptr< AccumType > >[]> &tSameVal, const std::unique_ptr< std::vector< Bool >[]> &tAllSame, uInt nThreadsMax)
static uInt nThreadsMax(const StatsDataProvider< CASA_STATP > *const dataProvider)
static Bool doMax(AccumType &datamax, LocationType &maxpos, Bool isFirst, const AccumType &datum, const LocationType &location)
return True if the max or min was updated, False otherwise.
static void accumulateSym(Double &npts, AccumType &nvariance, AccumType &sumsq, const AccumType &datum, const AccumType &center)
These versions are for symmetric accumulation about a specified center point.
static void waccumulateSym(Double &npts, AccumType &sumweights, AccumType &wnvariance, AccumType &wsumsq, const AccumType &datum, const AccumType &weight, const AccumType &center)
wsumsq is the weighted sum of squares, sum(w_i*x_i*x_i)
static StatsData< AccumType > combine(const std::vector< StatsData< AccumType > > &stats)
use two statistics sets to get the statistics set that would result in combining the two data sets us...
static void accumulateSym(Double &npts, AccumType &nvariance, AccumType &sumsq, AccumType &datamin, AccumType &datamax, LocationType &minpos, LocationType &maxpos, const AccumType &datum, const LocationType &location, const AccumType &center)
maxpos and minpos refer to actual, not virtually created, data only.
static void waccumulate(Double &npts, AccumType &sumofweights, AccumType &sum, AccumType &mean, AccumType &nvariance, AccumType &sumsq, AccumType &datamin, AccumType &datamax, LocationType &minpos, LocationType &maxpos, const AccumType &datum, const AccumType &weight, const LocationType &location)
static void waccumulate(Double &npts, AccumType &sumweights, AccumType &wsum, AccumType &wmean, AccumType &wnvariance, AccumType &wsumsq, const AccumType &datum, const AccumType &weight)
wsumsq is the weighted sum of squares, sum(w_i*x_i*x_i)
static void waccumulateSym(Double &npts, AccumType &sumofweights, AccumType &nvariance, AccumType &sumsq, AccumType &datamin, AccumType &datamax, LocationType &minpos, LocationType &maxpos, const AccumType &datum, const AccumType &weight, const LocationType &location, const AccumType &center)
static std::map< uInt64, AccumType > indicesToValues(std::vector< AccumType > &myArray, const std::set< uInt64 > &indices)
The array can be changed by partially sorting it up to the largest index.
static void convertToAbsDevMedArray(DataArray &myArray, AccumType median)
convert in place by taking the absolute value of the difference of the std::vector and the median
static void accumulate(Double &npts, AccumType &sum, AccumType &mean, AccumType &nvariance, AccumType &sumsq, const AccumType &datum)
static Bool includeDatum(const AccumType &datum, typename DataRanges::const_iterator beginRange, typename DataRanges::const_iterator endRange, Bool isInclude)
static void waccumulate(Double &npts, AccumType &sumweights, AccumType &wsum, AccumType &wmean, const AccumType &datum, const AccumType &weight)
in order to optimize performance, no checking is done for the weight == 0 case callers should ensure ...
static void accumulate(Double &npts, AccumType &sum, AccumType &mean, AccumType &nvariance, AccumType &sumsq, DataType &datamin, DataType &datamax, LocationType &minpos, LocationType &maxpos, const DataType &datum, const LocationType &location)
static void accumulate(Double &npts, AccumType &sum, AccumType &mean, AccumType &nvariance, AccumType &sumsq, AccumType &datamin, AccumType &datamax, LocationType &minpos, LocationType &maxpos, const AccumType &datum, const LocationType &location)
The assignment operator of class LocationType should use copy, not reference, semantics.
static Bool doMin(AccumType &datamin, LocationType &minpos, Bool isFirst, const AccumType &datum, const LocationType &location)
Abstract base class which defines interface for providing "datasets" to the statistics framework in c...
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
LatticeExprNode mean(const LatticeExprNode &expr)
double datamin() const
Definition hdu.h:412
LatticeExprNode sum(const LatticeExprNode &expr)
unsigned int uInt
Definition aipstype.h:49
std::pair< Int64, Int64 > LocationType
double datamax() const
Definition hdu.h:411
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
double Double
Definition aipstype.h:53
LatticeExprNode median(const LatticeExprNode &expr)