casacore
Loading...
Searching...
No Matches
StatAcc.h
Go to the documentation of this file.
1//# StatAcc.h: Statistics Accumulator
2//# Copyright (C) 1996,1999,2000,2001
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef SCIMATH_STATACC_H
27#define SCIMATH_STATACC_H
28
29#include <casacore/casa/aips.h>
30#include <casacore/casa/BasicMath/Math.h>
31#include <casacore/casa/Utilities/Fallible.h>
32#include <casacore/casa/iosfwd.h>
33#include <casacore/casa/Arrays/ArrayFwd.h>
34
35namespace casacore { //# NAMESPACE CASACORE - BEGIN
36
37// forward declarations:
38template <class T> class Block;
39class String;
40
41// <reviewed reviewer="" date="" tests="tStatAcc" demos="">
42
43// <prerequisite>
44// <li> module Arrays
45// <li> <linkto module="Arrays:description">Arrays </linkto> module
46// </prerequisite>
47//
48// <summary>
49// A statistics accumulator
50// </summary>
51//
52// <etymology>
53// StatAcc stands for `Statistics Accumulator'.
54// </etymology>
55//
56// <templating arg=T>
57// <li> A statistics accumulator accepts (weighted) input values and
58// calculates simple statistice (min, max, weighted mean, rms etc).
59// The accepted input types are real, i.e. Int, uInt, Float, Double,
60// but not Complex. The reason for this is that the < operator of
61// Complex (needed for min/max) works on the norm in any case, and
62// the sqrt function (needed for rms) yields an ambiguous result.
63//
64// Restriction to real types also allows the internal arithmetic type
65// to be Double rather than the input type. The latter would give
66// all kinds of complications with weighting, accuracy and overflow
67// if the input type would be Int or uInt.
68// </templating>
69
70// <synopsis>
71// The (weighted) values are fed to StatAcc via the member function
72// `put'. They can be fed individually, or in the form of an
73// Array. The weights are optional (default = 1) and always have
74// type Float.
75//
76// Asking for a result does not change the internal state. The
77// type of the returned results is always Fallible<Double>.
78// A result is invalid if no input values with non-zero weight
79// have been accumulated yet.
80//
81// The accumulator
82// can be re-initialised with the function `reset'. Accumulators
83// can be added to each other, which is as if their combined values had been
84// accumulated in the same accumulator.
85//
86// Some functions have been provided to display a summary of the
87// statistics results. One may choose between a one-line format
88// (with an optional associated header line), and a list.
89// </synopsis>
90//
91// <example>
92// <srcblock>
93// StatAcc<T> s; // T is Float, Double, Int etc
94// Matrix<T> vv(2,5); // a matrix (array) of input values
95// Matrix<Float> wgt(2,5); // an associated matrix of weights
96// .... fill vv and wgt with values and individual weights ...
97// s.put(vv,wgt); // accumulate the weighted values
98// Fallible<Double> min = s.getMin(); // return the minimum value
99//
100// s.reset(); // re-initialise
101// s.put(vv); // if wgt omitted, default = 1.0
102// if (s.getRms().isValid() { // check validity of rms
103// ... use it ...
104// }
105// </srcblock>
106// </example>
107//
108// <motivation>
109// One often needs simple statistics of a series of values, which
110// may occur by themselves or in arrays at various points in a program.
111// Sincs it is a pain to have to assign accumulation variables, and to
112// write statistics evaluation code (including exceptions),
113// this helper class is provided.
114// </motivation>
115//
116// <todo asof="">
117// </todo>
118
119
120// ***************************************************************************
121
122template<class T> class StatAcc {
123public:
124 // constructors and destructor.
125 // <group>
127 StatAcc(const StatAcc&);
129 // </group>
130
131 // Reset or copy the accumulator attributes.
132 // <group>
133 void reset();
134 void copy(const StatAcc&);
135 // </group>
136
137 // Operators for adding and copying accumulators.
138 // <group>
142 // </group>
143
144 // Accumulate input value(s) v with weight w.
145 // If weight is omitted, the default=1.
146 // <group>
147 inline void put(const T v);
148 inline void put(const T v, const Float w);
149 void put(const Array<T>& v);
150 void put(const Array<T>& v, const Array<Float>& w);
151 void put(const Block<T>& v);
152 void put(const Block<T>& v, const Block<Float>& w);
153 // </group>
154
155 // Get statistics results one at a time.
156 // Count is the nr of values accumulated.
157 // Wtot is the sum of the weights.
158 // Rms is defined w.r.t. the mean, and is the square of Variance.
159 // RmsAbs is the root-mean-square of the absolute input values.
160 // <group>
161 Double getWtot() const;
162 uInt getCount() const;
169 // </group>
170
171 // Print summary of accumulated statistics.
172 // Line is a one-line summary, including the (short) caption.
173 // LineHeader gives a one-line explanation of the numbers.
174 // List uses a separate line for each result (mean, max etc).
175 // <group>
176 void printSummaryList(std::ostream&, const String& caption) const;
177 void printSummaryLine(std::ostream&, const String& caption) const;
178 void printSummaryLineHeader(std::ostream&, const String& caption) const;
179 // </group>
180
181private:
182 Double itsWtot; //# Sum of weights
183 Double itsWsum; //# Sum of weighted values
184 Double itsWssum; //# Sum of weighted squares
185 Double itsMin; //# Minimum value
186 Double itsMax; //# Maximum value
187 uInt itsCount; //# Number of samples
188
189 // Accumulate a single weighted value.
190 void put1(const T, const Float);
191
192};
193
194
195
196//*************************** inline functions, have to be in StatAcc.h ****
197
198
199// Accumulate a single value:
200
201template<class T>
202inline void StatAcc<T>::put(const T v) {
203 put1(v, 1); // default weight = 1
204}
205
206template<class T>
207inline void StatAcc<T>::put(const T v, const Float w) {
208 put1(v, w);
209}
210
211
212
213} //# NAMESPACE CASACORE - END
214
215#ifndef CASACORE_NO_AUTO_TEMPLATES
216#include <casacore/scimath/Mathematics/StatAcc.tcc>
217#endif //# CASACORE_NO_AUTO_TEMPLATES
218#endif
219
220
221
222
223
224
225
226
227
228
229
230
simple 1-D array
Definition Block.h:198
Mark a value as valid or invalid.
Definition Fallible.h:121
A statistics accumulator
Definition StatAcc.h:122
void reset()
Reset or copy the accumulator attributes.
Double getWtot() const
Get statistics results one at a time.
void put(const Block< T > &v, const Block< Float > &w)
Fallible< Double > getRms() const
StatAcc()
constructors and destructor.
void copy(const StatAcc &)
void put(const T v)
Accumulate input value(s) v with weight w.
Definition StatAcc.h:202
void put(const Array< T > &v, const Array< Float > &w)
uInt getCount() const
Fallible< Double > getMin() const
StatAcc & operator+=(const StatAcc &)
Fallible< Double > getMean() const
void put(const Array< T > &v)
void printSummaryLineHeader(std::ostream &, const String &caption) const
void put(const Block< T > &v)
void printSummaryList(std::ostream &, const String &caption) const
Print summary of accumulated statistics.
Fallible< Double > getVariance() const
StatAcc operator+(const StatAcc &)
StatAcc(const StatAcc &)
void put1(const T, const Float)
Accumulate a single weighted value.
Fallible< Double > getMax() const
void printSummaryLine(std::ostream &, const String &caption) const
Fallible< Double > getRmsAbs() const
StatAcc & operator=(const StatAcc &)
Operators for adding and copying accumulators.
String: the storage and methods of handling collections of characters.
Definition String.h:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
float Float
Definition aipstype.h:52
double Double
Definition aipstype.h:53