cgv
Loading...
Searching...
No Matches
statistics.cxx
1#include "statistics.h"
2
3#include <iostream>
4#include <cmath>
5
6namespace cgv {
7 namespace utils {
8
16statistics::statistics(const double& v) { init(v); }
18statistics::statistics(const double& v, unsigned int n) { init(v,n); }
20void statistics::init() { cnt = 0; }
22void statistics::init(const double& v) { min = max = sum = v; sms = v*v; cnt = 1; }
24void statistics::init(const double& v, unsigned int n) { min = max = v; sum = n*v; sms = n*v*v; cnt = n; }
26void statistics::update(const double& v) {
27 if (cnt) {
28 if (v > max) max = v;
29 if (v < min) min = v;
30 sum += v;
31 sms += v*v;
32 ++cnt;
33 }
34 else
35 init(v);
36}
38void statistics::update(const double& v, unsigned int n) {
39 if (cnt) {
40 if (v > max) max = v;
41 if (v < min) min = v;
42 sum += n*v;
43 sms += n*v*v;
44 cnt += n;
45 }
46 else
47 init(v,n);
48}
50double statistics::get_average() const { return sum/cnt; }
53 double E = get_average();
54 double E2 = sms / cnt;
55 return E2 - E*E;
56}
59 return sqrt(get_variance());
60}
62double statistics::get_sum() const { return sum; }
64double statistics::get_sum_of_squares() const { return sms; }
66double statistics::get_min() const { return min; }
68double statistics::get_max() const { return max; }
70unsigned int statistics::get_count() const { return cnt; }
71
72std::ostream& operator << (std::ostream& os, const statistics& s)
73{
74 return os << s.get_min() << ".." << s.get_max()
75 << ":<" << s.get_sum() << "/" << s.get_count()
76 << "=" << s.get_average() << "+-" << s.get_standard_deviation() << ">";
77}
78
79 }
80}
incrementally accumulate statistical information
Definition statistics.h:13
double get_sum_of_squares() const
get the sum of the squares of the considered variables
void update(const double &v)
consider another value
unsigned int get_count() const
get the number of considered variables
double get_average() const
compute average of the considered values
double get_max() const
get the maximum of the considered variables
void init()
initialize
double get_standard_deviation() const
compute standard deviation of the considered values
double get_sum() const
get the sum of the considered variables
double get_min() const
get the minimum of the considered variables
statistics()
initialize with no value considered yet
double get_variance() const
compute variance of the considered values
the cgv namespace
Definition print.h:11