Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JStats.hh
Go to the documentation of this file.
1#ifndef __JTOOLS__JSTATS__
2#define __JTOOLS__JSTATS__
3
4#include <ostream>
5#include <iomanip>
6#include <cmath>
7#include <limits>
8#include <algorithm>
9
10#include "JLang/JException.hh"
11#include "JLang/JTitle.hh"
12#include "JLang/JVectorize.hh"
13#include "JLang/JManip.hh"
14
15#include "JMath/JMath.hh"
16
17
18/**
19 * \author mdejong
20 */
21
22namespace JTOOLS {}
23namespace JPP { using namespace JTOOLS; }
24
25namespace JTOOLS {
26
27 using JMATH::JMath;
28 using JLANG::JTitle;
32
33
34 /**
35 * Auxiliary data structure for running average and standard deviation.
36 *
37 * See Knuth TAOCP vol 2, 3rd edition, page 232.\n
38 * This class acts as a zero-dimensional histogram.\n
39 * Note that if a weight is used, it should strictly be positive.
40 */
41 struct JStats :
42 public JTitle,
43 public JMath<JStats>
44 {
45 /**
46 * Constructor.
47 *
48 * \param title title
49 */
50 JStats(const JTitle& title = "") :
52 {
53 reset();
54 }
55
56
57 /**
58 * Constructor.
59 *
60 * \param title title
61 * \param buffer input data
62 * \param w weight
63 */
64 template<class JElement_t, class JAllocator_t>
67 const double w = 1.0) :
69 {
70 reset();
71
72 put(buffer, w);
73 }
74
75
76 /**
77 * Reset.
78 */
79 void reset()
80 {
81 mean = 0.0;
82 sigma = 0.0;
83 total = 0.0;
84 count = 0;
85 xmin = std::numeric_limits<double>::max();
86 xmax = std::numeric_limits<double>::lowest();
87 wmin = std::numeric_limits<double>::max();
88 wmax = std::numeric_limits<double>::lowest();
89 }
90
91
92 /**
93 * Add stats.
94 *
95 * \param Q stats
96 * \return this stats
97 */
98 JStats& add(const JStats& Q)
99 {
100 mean += Q.mean;
101 sigma += Q.sigma;
102 total += Q.total;
103 count += Q.count;
104 xmin = std::min(xmin, Q.xmin);
105 xmax = std::max(xmax, Q.xmax);
106 wmin = std::min(wmin, Q.wmin);
107 wmax = std::max(wmax, Q.wmax);
108
109 return *this;
110 }
111
112
113 /**
114 * Put value.
115 *
116 * \param x value
117 * \param w weight
118 */
119 void put(const double x, const double w = 1.0)
120 {
121 total += w;
122 count += 1;
123
124 if (count == 1) {
125
126 mean = x;
127 sigma = 0.0;
128
129 } else {
130
131 const double new_mean = mean + w * (x - mean) / total;
132 const double new_sigma = sigma + w * (x - mean) * (x - new_mean);
133
134 // set up for next iteration
135
136 mean = new_mean;
137 sigma = new_sigma;
138 }
139
140 xmin = std::min(xmin, x);
141 xmax = std::max(xmax, x);
142 wmin = std::min(wmin, w);
143 wmax = std::max(wmax, w);
144 }
145
146
147 /**
148 * Put data.
149 *
150 * \param buffer input data
151 * \param w weight
152 */
153 template<class JElement_t, class JAllocator_t>
155 const double w = 1.0)
156 {
157 for (typename array_type<JElement_t, JAllocator_t>::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
158 put(*i, w);
159 }
160 }
161
162
163 /**
164 * Get total count.
165 *
166 * \return count
167 */
168 long long int getCount() const
169 {
170 return count;
171 }
172
173
174 /**
175 * Get total weight.
176 *
177 * \return weight
178 */
179 double getTotal() const
180 {
181 return total;
182 }
183
184
185 /**
186 * Get minimum value.
187 *
188 * \return minimum value
189 */
190 double getXmin() const
191 {
192 return xmin;
193 }
194
195
196 /**
197 * Get maximum value.
198 *
199 * \return maximum value
200 */
201 double getXmax() const
202 {
203 return xmax;
204 }
205
206
207 /**
208 * Get minimum weight.
209 *
210 * \return minimum weight
211 */
212 double getWmin() const
213 {
214 return wmin;
215 }
216
217
218 /**
219 * Get maximum weight.
220 *
221 * \return maximum weight
222 */
223 double getWmax() const
224 {
225 return wmax;
226 }
227
228
229 /**
230 * Get mean value.
231 *
232 * \return mean value
233 */
234 double getMean() const
235 {
236 if (count != 0.0)
237 return mean;
238 else
239 THROW(JDivisionByZero, "JStats::getMean() count " << count);
240 }
241
242
243 /**
244 * Get mean value.
245 *
246 * \param value default value
247 * \return mean value
248 */
249 double getMean(const double value) const
250 {
251 if (count != 0.0)
252 return getMean();
253 else
254 return value;
255 }
256
257
258 /**
259 * Get standard deviation
260 *
261 * \return standard deviation
262 */
263 double getSTDev() const
264 {
265 if (count > 1)
266 return sqrt(count * sigma/(total * (count - 1)));
267 else
268 THROW(JDivisionByZero, "JStats::getSTDev() count " << count);
269 }
270
271
272 /**
273 * Get standard deviation
274 *
275 * \param value default value
276 * \return standard deviation
277 */
278 double getSTDev(const double value) const
279 {
280 if (count > 1)
281 return getSTDev();
282 else
283 return value;
284 }
285
286
287 /**
288 * Get maximal deviation from average.
289 *
290 * \param relative if true, relative to average, else absolute
291 * \return deviation
292 */
293 double getDeviation(const bool relative = true) const
294 {
295 if (relative)
296 return std::max(getXmax() - getMean(), getMean() - getXmin());
297 else
298 return getXmax() - getXmin();
299 }
300
301
302 /**
303 * Test relative accuracy.
304 *
305 * \param precision relative precision
306 * \return true if reached accuracy; else false
307 */
308 bool hasAccuracy(const double precision) const
309 {
310 return getCount() > 1 && getSTDev() < precision * getMean();
311 }
312
313
314 /**
315 * Print stats.
316 *
317 * \param out output stream
318 * \param lpr long print
319 */
320 std::ostream& print(std::ostream& out, bool lpr = true) const
321 {
322 using namespace std;
323
324 const int nc = getTitle().size();
325
326 if (lpr) {
327 out << setw(nc) << left << " " << ' '
328 << setw(12) << left << " mean" << ' '
329 << setw(12) << left << " STD" << ' '
330 << setw(12) << left << " deviation" << endl;
331 }
332
333 out << setw(nc) << left << getTitle() << ' '
334 << SCIENTIFIC(12,5) << getMean() << ' '
335 << SCIENTIFIC(12,5) << getSTDev() << ' '
336 << SCIENTIFIC(12,5) << getDeviation(false) << endl;
337
338 return out;
339 }
340
341
342 /**
343 * Print stats.
344 *
345 * \param out output stream
346 * \param stats stats
347 * \return output stream
348 */
349 friend inline std::ostream& operator<<(std::ostream& out, const JStats& stats)
350 {
351 return stats.print(out, getLongprint(out));
352 }
353
354 protected:
355 double mean;
356 double sigma;
357 double total;
358 long long int count;
359 double xmin;
360 double xmax;
361 double wmin;
362 double wmax;
363 };
364}
365
366#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
I/O manipulators.
bool getLongprint(std::ostream &out)
Get long print option.
Definition JManip.hh:121
Base class for data structures with artithmetic capabilities.
Auxiliary methods to convert data members or return values of member methods of a set of objects to a...
Exception for division by zero.
Auxiliary class for title.
Definition JTitle.hh:19
std::string title
Definition JTitle.hh:73
const std::string & getTitle() const
Get title.
Definition JTitle.hh:55
const array_type< JValue_t > & make_array(const JValue_t(&array)[N])
Method to create array of values.
Definition JVectorize.hh:54
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
Auxiliary data structure for return type of make methods.
Definition JVectorize.hh:28
Auxiliary base class for aritmetic operations of derived class types.
Definition JMath.hh:347
Auxiliary data structure for running average and standard deviation.
Definition JStats.hh:44
double sigma
Definition JStats.hh:356
double getDeviation(const bool relative=true) const
Get maximal deviation from average.
Definition JStats.hh:293
JStats(const JTitle &title="")
Constructor.
Definition JStats.hh:50
double getWmin() const
Get minimum weight.
Definition JStats.hh:212
double getXmax() const
Get maximum value.
Definition JStats.hh:201
double getWmax() const
Get maximum weight.
Definition JStats.hh:223
long long int getCount() const
Get total count.
Definition JStats.hh:168
double getTotal() const
Get total weight.
Definition JStats.hh:179
double getSTDev(const double value) const
Get standard deviation.
Definition JStats.hh:278
std::ostream & print(std::ostream &out, bool lpr=true) const
Print stats.
Definition JStats.hh:320
void put(const double x, const double w=1.0)
Put value.
Definition JStats.hh:119
double mean
Definition JStats.hh:355
double getMean(const double value) const
Get mean value.
Definition JStats.hh:249
JStats(const JTitle &title, const array_type< JElement_t, JAllocator_t > &buffer, const double w=1.0)
Constructor.
Definition JStats.hh:65
friend std::ostream & operator<<(std::ostream &out, const JStats &stats)
Print stats.
Definition JStats.hh:349
double wmin
Definition JStats.hh:361
double wmax
Definition JStats.hh:362
long long int count
Definition JStats.hh:358
double xmax
Definition JStats.hh:360
double getMean() const
Get mean value.
Definition JStats.hh:234
bool hasAccuracy(const double precision) const
Test relative accuracy.
Definition JStats.hh:308
double total
Definition JStats.hh:357
double xmin
Definition JStats.hh:359
void put(const array_type< JElement_t, JAllocator_t > &buffer, const double w=1.0)
Put data.
Definition JStats.hh:154
void reset()
Reset.
Definition JStats.hh:79
double getSTDev() const
Get standard deviation.
Definition JStats.hh:263
double getXmin() const
Get minimum value.
Definition JStats.hh:190
JStats & add(const JStats &Q)
Add stats.
Definition JStats.hh:98
Auxiliary data structure for floating point format specification.
Definition JManip.hh:488