1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
//-----------------------------------------------------------------------------
/** @file libboardgame_base/Statistics.h
@author Markus Enzenberger
@copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------
#ifndef LIBBOARDGAME_BASE_STATISTICS_H
#define LIBBOARDGAME_BASE_STATISTICS_H
#include <atomic>
#include <cmath>
#include <iomanip>
#include <limits>
#include <sstream>
#include <string>
#include "FmtSaver.h"
namespace libboardgame_base {
using namespace std;
//-----------------------------------------------------------------------------
template<typename FLOAT = double>
class StatisticsBase
{
public:
/** Constructor.
@param init_val The value to return in get_mean() if count is 0. This
value does not affect the mean returned if count is greater 0. */
explicit StatisticsBase(FLOAT init_val = 0) { clear(init_val); }
void add(FLOAT val);
void clear(FLOAT init_val = 0);
FLOAT get_count() const { return m_count; }
FLOAT get_mean() const { return m_mean; }
void write(ostream& out, bool fixed = false, int precision = 6) const;
private:
FLOAT m_count;
FLOAT m_mean;
};
template<typename FLOAT>
void StatisticsBase<FLOAT>::add(FLOAT val)
{
FLOAT count = m_count;
++count;
val -= m_mean;
m_mean += val / count;
m_count = count;
}
template<typename FLOAT>
inline void StatisticsBase<FLOAT>::clear(FLOAT init_val)
{
m_count = 0;
m_mean = init_val;
}
template<typename FLOAT>
void StatisticsBase<FLOAT>::write(ostream& out, bool fixed,
int precision) const
{
FmtSaver saver(out);
if (fixed)
out << std::fixed;
out << setprecision(precision) << m_mean;
}
//-----------------------------------------------------------------------------
template<typename FLOAT = double>
class Statistics
{
public:
explicit Statistics(FLOAT init_val = 0) { clear(init_val); }
void add(FLOAT val);
void clear(FLOAT init_val = 0);
FLOAT get_mean() const { return m_statistics_base.get_mean(); }
FLOAT get_count() const { return m_statistics_base.get_count(); }
FLOAT get_deviation() const;
FLOAT get_error() const;
FLOAT get_variance() const { return m_variance; }
void write(ostream& out, bool fixed = false, int precision = 6) const;
private:
StatisticsBase<FLOAT> m_statistics_base;
FLOAT m_variance;
};
template<typename FLOAT>
void Statistics<FLOAT>::add(FLOAT val)
{
if (get_count() > 0)
{
FLOAT count_old = get_count();
FLOAT mean_old = get_mean();
m_statistics_base.add(val);
FLOAT mean = get_mean();
FLOAT count = get_count();
m_variance = (count_old * (m_variance + mean_old * mean_old)
+ val * val) / count - mean * mean;
}
else
{
m_statistics_base.add(val);
m_variance = 0;
}
}
template<typename FLOAT>
inline void Statistics<FLOAT>::clear(FLOAT init_val)
{
m_statistics_base.clear(init_val);
m_variance = 0;
}
template<typename FLOAT>
inline FLOAT Statistics<FLOAT>::get_deviation() const
{
// m_variance can become negative (due to rounding errors?)
return m_variance < 0 ? 0 : sqrt(m_variance);
}
template<typename FLOAT>
FLOAT Statistics<FLOAT>::get_error() const
{
auto count = get_count();
return count == 0 ? 0 : get_deviation() / sqrt(count);
}
template<typename FLOAT>
void Statistics<FLOAT>::write(ostream& out, bool fixed, int precision) const
{
FmtSaver saver(out);
if (fixed)
out << std::fixed;
out << setprecision(precision) << get_mean() << " dev=" << get_deviation();
}
//-----------------------------------------------------------------------------
template<typename FLOAT = double>
class StatisticsExt
{
public:
explicit StatisticsExt(FLOAT init_val = 0) { clear(init_val); }
void add(FLOAT val);
void clear(FLOAT init_val = 0);
FLOAT get_mean() const { return m_statistics.get_mean(); }
FLOAT get_error() const { return m_statistics.get_error(); }
FLOAT get_count() const { return m_statistics.get_count(); }
FLOAT get_max() const { return m_max; }
FLOAT get_min() const { return m_min; }
FLOAT get_deviation() const { return m_statistics.get_deviation(); }
FLOAT get_variance() const { return m_statistics.get_variance(); }
void write(ostream& out, bool fixed = false, int precision = 6,
bool integer_values = false, bool with_error = false) const;
string to_string(bool fixed = false, int precision = 6,
bool integer_values = false,
bool with_error = false) const;
private:
Statistics<FLOAT> m_statistics;
FLOAT m_max;
FLOAT m_min;
};
template<typename FLOAT>
void StatisticsExt<FLOAT>::add(FLOAT val)
{
m_statistics.add(val);
if (val > m_max)
m_max = val;
if (val < m_min)
m_min = val;
}
template<typename FLOAT>
inline void StatisticsExt<FLOAT>::clear(FLOAT init_val)
{
m_statistics.clear(init_val);
m_min = numeric_limits<FLOAT>::max();
m_max = -numeric_limits<FLOAT>::max();
}
template<typename FLOAT>
string StatisticsExt<FLOAT>::to_string(bool fixed, int precision,
bool integer_values,
bool with_error) const
{
ostringstream s;
write(s, fixed, precision, integer_values, with_error);
return s.str();
}
template<typename FLOAT>
void StatisticsExt<FLOAT>::write(ostream& out, bool fixed, int precision,
bool integer_values, bool with_error) const
{
FmtSaver saver(out);
out << setprecision(precision);
if (fixed)
out << std::fixed;
out << get_mean();
if (with_error)
out << "+/-" << get_error();
out << " dev=" << get_deviation();
if (m_min != numeric_limits<FLOAT>::max()
&& m_max != -numeric_limits<FLOAT>::max() && m_min != m_max)
{
if (integer_values)
out << setprecision(0);
out << " [" << m_min << "..." << m_max << ']';
}
}
//-----------------------------------------------------------------------------
/** Like StatisticsBase, but for lock-free multithreading with potentially
lost updates.
Updates and accesses of the moving average and the count are atomic but
not synchronized and use memory_order_relaxed. Therefore, updates can be
lost. Initializing via the constructor, operator= or clear() uses
memory_order_seq_cst */
template<typename FLOAT = double>
class StatisticsDirty
{
public:
/** Constructor.
@param init_val See StatisticBase::StatisticBase() */
explicit StatisticsDirty(FLOAT init_val = 0) { clear(init_val); }
StatisticsDirty& operator=(const StatisticsDirty& s);
void add(FLOAT val, FLOAT weight = 1);
void clear(FLOAT init_val = 0) { init(init_val, 0); }
void init(FLOAT mean, FLOAT count);
FLOAT get_count() const { return m_count.load(memory_order_relaxed); }
FLOAT get_mean() const { return m_mean.load(memory_order_relaxed); }
void write(ostream& out, bool fixed = false, int precision = 6) const;
private:
atomic<FLOAT> m_count;
atomic<FLOAT> m_mean;
};
template<typename FLOAT>
StatisticsDirty<FLOAT>&
StatisticsDirty<FLOAT>::operator=(const StatisticsDirty& s)
{
m_count = s.m_count.load();
m_mean = s.m_mean.load();
return *this;
}
template<typename FLOAT>
void StatisticsDirty<FLOAT>::add(FLOAT val, FLOAT weight)
{
FLOAT count = m_count.load(memory_order_relaxed);
FLOAT mean = m_mean.load(memory_order_relaxed);
count += weight;
mean += weight * (val - mean) / count;
m_mean.store(mean, memory_order_relaxed);
m_count.store(count, memory_order_relaxed);
}
template<typename FLOAT>
inline void StatisticsDirty<FLOAT>::init(FLOAT mean, FLOAT count)
{
m_count = count;
m_mean = mean;
}
template<typename FLOAT>
void StatisticsDirty<FLOAT>::write(ostream& out, bool fixed,
int precision) const
{
FmtSaver saver(out);
if (fixed)
out << std::fixed;
out << setprecision(precision) << get_mean();
}
//-----------------------------------------------------------------------------
template<typename FLOAT>
inline ostream& operator<<(ostream& out, const StatisticsExt<FLOAT>& s)
{
s.write(out);
return out;
}
//-----------------------------------------------------------------------------
} // namespace libboardgame_base
#endif // LIBBOARDGAME_BASE_STATISTICS_H
|