simple_stats.cc
Go to the documentation of this file.
1 // ======================================================================
2 //
3 // simple_stats: yield simple statistics from individually-presented data
4 //
5 // ======================================================================
6 
7 #include "cetlib/simple_stats.h"
8 
9 #include "cetlib/pow.h" // square
10 
11 #include <algorithm> // max, min
12 #include <cmath> // abs, sqrt
13 #include <limits> // numeric_limits
14 
15 using cet::simple_stats;
16 
17 // ----------------------------------------------------------------------
18 // c'tors:
19 
20 simple_stats::simple_stats() noexcept
21  : n_(0u)
22  , min_(+std::numeric_limits<double>::infinity())
23  , max_(-std::numeric_limits<double>::infinity())
24  , small_(+std::numeric_limits<double>::infinity())
25  , sum_(0.0)
26  , sumsq_(0.0)
27 {
28  ;
29 }
30 
31 simple_stats::simple_stats(double x) noexcept
32  : n_(0u)
33  , min_(+std::numeric_limits<double>::infinity())
34  , max_(-std::numeric_limits<double>::infinity())
35  , small_(+std::numeric_limits<double>::infinity())
36  , sum_(0.0)
37  , sumsq_(0.0)
38 {
39  sample(x);
40 }
41 
42 // ----------------------------------------------------------------------
43 // statistics calculators:
44 
45 double
47 {
48  return n_ == 0u ? std::numeric_limits<double>::quiet_NaN() :
49  sum_ / double(n_);
50 }
51 
52 double
54 {
55  return max_ - min_;
56 }
57 
58 double
59 simple_stats::err_mean(std::size_t nparams) const noexcept
60 {
61  return n_ == 0u ? std::numeric_limits<double>::quiet_NaN() :
62  rms(nparams) / std::sqrt(double(n_));
63 }
64 
65 double
66 simple_stats::rms(std::size_t nparams) const noexcept
67 {
68  if (n_ <= nparams)
69  return std::numeric_limits<double>::quiet_NaN();
70 
71  double diff = sumsq_ / double(n_) - square(mean());
72  if (diff < 0.0)
73  return 0.0;
74 
75  double factor = double(n_) / double(n_ - nparams);
76  return std::sqrt(factor * diff);
77 }
78 
79 double
80 simple_stats::rms0(std::size_t nparams) const noexcept
81 {
82  if (n_ <= nparams)
83  return std::numeric_limits<double>::quiet_NaN();
84 
85  double diff = sumsq_ / double(n_);
86  if (diff < 0.0)
87  return 0.0;
88 
89  double factor = double(n_) / double(n_ - nparams);
90  return std::sqrt(factor * diff);
91 }
92 
93 double
94 simple_stats::err_rms(std::size_t nparams) const noexcept
95 {
96  return n_ == 0u ? std::numeric_limits<double>::quiet_NaN() :
97  rms(nparams) / std::sqrt(double(2u * n_));
98 }
99 
100 // ----------------------------------------------------------------------
101 // mutators:
102 
103 void
105 {
106  *this = simple_stats();
107 }
108 
109 void
110 simple_stats::sample(double x) noexcept
111 {
112  ++n_;
113  min_ = std::min(x, min_);
114  max_ = std::max(x, max_);
116  sum_ += x;
117  sumsq_ += square(x);
118 }
119 
120 // ======================================================================
double mean() const noexcept
Definition: simple_stats.cc:46
simple_stats() noexcept
Definition: simple_stats.cc:20
STL namespace.
double err_mean(std::size_t nparams=1u) const noexcept
Definition: simple_stats.cc:59
constexpr T square(T x)
Definition: pow.h:21
void sample(double) noexcept
double rms0(std::size_t nparams=0u) const noexcept
Definition: simple_stats.cc:80
T abs(T value)
void reset() noexcept
static int max(int a, int b)
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
list x
Definition: train.py:276
double rms(std::size_t nparams=1u) const noexcept
Definition: simple_stats.cc:66
std::size_t n_
Definition: simple_stats.h:85
double range() const noexcept
Definition: simple_stats.cc:53
double err_rms(std::size_t nparams=1u) const noexcept
Definition: simple_stats.cc:94