Signbit MPSCQ  0.5.0
stats.h
Go to the documentation of this file.
1 
7 /*
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef SBITS_STATS_H_INCLUDED
22 #define SBITS_STATS_H_INCLUDED
23 
24 #include <cstdlib>
25 
26 namespace sbit
27 {
28 
52 class Stats
53 {
54 public:
57  void reset() noexcept
58  {
59  m_count = 0;
60  }
61 
66  void observe(double value) noexcept
67  {
68  ++m_count;
69 
70  if (1 == m_count)
71  {
72  m_mean = value;
73  }
74  else
75  {
76  const double oldMean = m_mean;
77  const double oldVariance = m_variance;
78 
79  m_mean = oldMean + (value - oldMean) / m_count;
80  m_variance = oldVariance + (value - oldMean) * (value - m_mean);
81  }
82  }
83 
86  size_t getCount() const noexcept
87  {
88  return m_count;
89  }
90 
93  double getMean() const noexcept
94  {
95  return m_mean;
96  }
97 
100  double getVariance() const noexcept
101  {
102  if (m_count > 1)
103  {
104  return m_variance / static_cast<double>(m_count - 1);
105  }
106 
107  return 0.0;
108  }
109 
110 private:
111  size_t m_count = 0;
112 
113  double m_mean = 0.0;
114  double m_variance = 0.0;
115 };
116 
119 } // namespace sbit
120 
121 #endif // SBITS_STATS_H_INCLUDED
sbit::Stats::getVariance
double getVariance() const noexcept
Definition: stats.h:100
sbit::Stats::getCount
size_t getCount() const noexcept
Definition: stats.h:86
sbit::Stats::getMean
double getMean() const noexcept
Definition: stats.h:93
sbit::Stats::reset
void reset() noexcept
Definition: stats.h:57
sbit::Stats::observe
void observe(double value) noexcept
Definition: stats.h:66
sbit::Stats
Definition: stats.h:52