LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
running_statistics.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
26 #ifndef LBANN_UTILS_RUNNING_STATISTICS_HPP_INCLUDED
27 #define LBANN_UTILS_RUNNING_STATISTICS_HPP_INCLUDED
28 
29 #include <algorithm>
30 #include <cmath>
31 #include <cstdint>
32 #include <limits>
33 
34 namespace lbann {
35 
41 {
42 public:
46  constexpr static auto default_min = std::numeric_limits<double>::max();
50  constexpr static auto default_max = std::numeric_limits<double>::lowest();
51 
52 public:
54 
61  void reset() noexcept;
62 
67  void insert(double val) noexcept;
68 
70 
71 
73  size_t samples() const noexcept;
77  double min() const noexcept;
81  double max() const noexcept;
83  double mean() const noexcept;
85  double total() const noexcept;
91  double variance() const noexcept;
97  double stddev() const noexcept;
99 
100 private:
101  size_t m_count = 0UL;
102  double m_min = default_min;
103  double m_max = default_max;
104  double m_mean = 0.;
105  double m_diff_sq = 0.;
106 }; // class RunningStats
107 
108 inline void RunningStats::reset() noexcept { *this = RunningStats{}; }
109 
110 inline void RunningStats::insert(double val) noexcept
111 {
112  // Welford
113  ++m_count;
114  m_min = std::min(m_min, val);
115  m_max = std::max(m_max, val);
116  double diff1 = val - m_mean;
117  m_mean += diff1 / m_count;
118  m_diff_sq += diff1 * (val - m_mean);
119 }
120 
121 inline size_t RunningStats::samples() const noexcept { return m_count; }
122 
123 inline double RunningStats::min() const noexcept { return m_min; }
124 
125 inline double RunningStats::max() const noexcept { return m_max; }
126 
127 inline double RunningStats::mean() const noexcept { return m_mean; }
128 
129 inline double RunningStats::total() const noexcept
130 {
131  return m_mean * static_cast<double>(m_count);
132 }
133 
134 inline double RunningStats::variance() const noexcept
135 {
136  return m_count > 1 ? m_diff_sq / static_cast<double>(m_count - 1) : 0.;
137 }
138 
139 inline double RunningStats::stddev() const noexcept
140 {
141  return std::sqrt(this->variance());
142 }
143 
144 } // namespace lbann
145 #endif // LBANN_UTILS_RUNNING_STATISTICS_HPP_INCLUDED
void reset() noexcept
All values return to their defaults.
double mean() const noexcept
Running mean of observed samples.
double min() const noexcept
Get the minimum observed value.
static constexpr auto default_min
The default minimum value, chosen to ensure that any observed sample is less-than this...
size_t samples() const noexcept
Number of observed samples.
static constexpr auto default_max
The default maximum value, chosen to ensure that any observed sample is greater-than this...
double variance() const noexcept
Running (unbiased) sample variance of the observed samples.
Accumulate mean, stddev, min, and max over a streaming data set.
double stddev() const noexcept
Running (unbiased) sample standard deviation of the observed samples.
void insert(double val) noexcept
Add a new value to the data set.
double total() const noexcept
Running sum of observed samples.
double max() const noexcept
Get the minimum observed value.