Statistics
1.0.0
A C++17 for profiling and constant time statistical calculation
|
Go to the documentation of this file.
43 #ifndef statistics_h__
44 #define statistics_h__
51 #include <initializer_list>
66 using T2 =
decltype(T{} * T{});
75 , m_max(std::numeric_limits<T>::lowest())
76 , m_min(std::numeric_limits<T>::max())
78 , m_sumOfSquares(T2{ 0 })
93 , m_sumOfSquares(measurement * measurement)
106 for (
const auto& measurement : init)
107 *
this += measurement;
115 template<
class InputIt>
119 for (InputIt itr = first; itr != last; ++itr)
127 inline void clear()
noexcept
137 inline size_t
count()
const noexcept
150 *
this += measurement;
161 template<
class InputIt>
164 for(InputIt itr = first; itr != last; ++itr)
177 return m_sum / std::max<size_t>(m_count, 1);
184 inline T
min()
const noexcept
193 inline T
max()
const noexcept
203 inline T
sum()
const noexcept
215 return m_sumOfSquares;
225 auto denom = std::max<size_t>(m_count, 1);
226 return ((m_sumOfSquares / denom) - (m_sum / denom) * (m_sum / denom));
247 if (measurement < m_min)
249 else if (measurement > m_max)
252 m_sum += measurement;
253 m_sumOfSquares += measurement * measurement;
268 m_count += rhs.m_count;
269 m_max = std::max(m_max, rhs.m_max);
270 m_min = std::min(m_min, rhs.m_min);
272 m_sumOfSquares += rhs.m_sumOfSquares;
281 template<
class U>
friend Statistics<U> operator+(
const Statistics<U>& lhs,
const T& rhs)
noexcept;
283 template<
class U>
friend bool operator==(
const Statistics<U>& lhs,
const Statistics<U>& rhs)
noexcept;
284 template<
class U>
friend bool operator!=(
const Statistics<U>& lhs,
const Statistics<U>& rhs)
noexcept;
285 template<
class U>
friend bool operator<(
const Statistics<U>& lhs,
const Statistics<U>& rhs)
noexcept;
286 template<
class U>
friend bool operator<=(
const Statistics<U>& lhs,
const Statistics<U>& rhs)
noexcept;
287 template<
class U>
friend bool operator>(
const Statistics<U>& lhs,
const Statistics<U>& rhs)
noexcept;
288 template<
class U>
friend bool operator>=(
const Statistics<U>& lhs,
const Statistics<U>& rhs)
noexcept;
340 if (lhs.m_count != rhs.m_count)
342 else if (lhs.m_min != rhs.m_min)
344 else if (lhs.m_max != rhs.m_max)
346 else if (lhs.m_sum != rhs.m_sum)
361 return !(lhs == rhs);
373 if (lhs.m_count < rhs.m_count)
375 else if (lhs.m_min < rhs.m_min)
377 else if (lhs.m_max < rhs.m_max)
379 else if (lhs.m_sum < rhs.m_sum)
Statistics(std::initializer_list< T > init)
Construct from initializer list.
Definition: statistics.h:103
Statistics & insert(const T &measurement) noexcept
Insert a measurement into the population.
Definition: statistics.h:148
Statistics(T measurement)
Constructor.
Definition: statistics.h:88
Statistics()
Default Constructor.
Definition: statistics.h:73
T standardDeviation() const noexcept
Calculates the standard deviation of the population.
Definition: statistics.h:234
Statistics(InputIt first, InputIt last)
Constructor from input iterators.
Definition: statistics.h:116
auto variance() const noexcept
Calculates the variance of the population.
Definition: statistics.h:223
T sum() const noexcept
Sum of the samples.
Definition: statistics.h:203
Statistics & insert(const InputIt &first, const InputIt &last) noexcept
Insert measurements into the population.
Definition: statistics.h:162
bool operator!=(const Statistics< T > &lhs, const Statistics< T > &rhs) noexcept
Inequality operator.
Definition: statistics.h:359
T min() const noexcept
Returns minimum sampled value.
Definition: statistics.h:184
Keeps a simple average of a series of measurements.
Definition: statistics.h:62
Statistics & operator+=(const Statistics &rhs) noexcept
Returns the average updated with the given measurement.
Definition: statistics.h:266
Statistics & operator+=(const T &measurement) noexcept
Returns the average updated with the given measurement.
Definition: statistics.h:245
bool operator<=(const Statistics< T > &lhs, const Statistics< T > &rhs) noexcept
Less than or equal to operator.
Definition: statistics.h:392
bool operator==(const Statistics< T > &lhs, const Statistics< T > &rhs) noexcept
Equality operator.
Definition: statistics.h:338
T mean() const noexcept
Value of the mean.
Definition: statistics.h:175
T max() const noexcept
Returns maximum sampled value.
Definition: statistics.h:193
bool operator<(const Statistics< T > &lhs, const Statistics< T > &rhs) noexcept
Less than operator.
Definition: statistics.h:371
size_t count() const noexcept
Measurement count.
Definition: statistics.h:137
T2 sumOfSquares() const noexcept
Returns the sum of squares of all measurements.
Definition: statistics.h:213
bool operator>(const Statistics< T > &lhs, const Statistics< T > &rhs) noexcept
Greater than operator.
Definition: statistics.h:404
Statistics< T > operator+(const Statistics< T > &lhs, const T &rhs) noexcept
Adds a measurement to the population.
Definition: statistics.h:307
Statistics< T > & operator+(const Statistics< T > &lhs, const Statistics< T > &rhs) noexcept
Combine two populations.
Definition: statistics.h:323
bool operator>=(const Statistics< T > &lhs, const Statistics< T > &rhs) noexcept
Greater than or equal to operator.
Definition: statistics.h:416