ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
RunningAverage.hpp
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2010-2022 The ESPResSo project
3
* Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4
* Max-Planck-Institute for Polymer Research, Theory Group
5
*
6
* This file is part of ESPResSo.
7
*
8
* ESPResSo is free software: you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation, either version 3 of the License, or
11
* (at your option) any later version.
12
*
13
* ESPResSo is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
17
*
18
* You should have received a copy of the GNU General Public License
19
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
*/
21
22
#ifndef STATISTICS_RUNING_AVERAGE_HPP
23
#define STATISTICS_RUNING_AVERAGE_HPP
24
25
#include <algorithm>
26
#include <cmath>
27
#include <limits>
28
29
namespace
Utils
{
30
namespace
Statistics {
31
32
/**
33
* \brief Keep running average and variance.
34
* The average should be numerically stable.
35
*/
36
template
<
typename
Scalar>
class
RunningAverage
{
37
public
:
38
RunningAverage
()
39
: m_n(0), m_old_avg(0), m_new_avg(0), m_old_var(0), m_new_var(0.0),
40
m_min(std::numeric_limits<Scalar>::
max
()),
41
m_max(-std::numeric_limits<Scalar>::
max
()) {}
42
void
add_sample
(Scalar s) {
43
m_n++;
44
45
if
(m_n == 1) {
46
m_old_avg = m_new_avg = s;
47
}
else
{
48
m_new_avg = m_old_avg + (s - m_old_avg) / m_n;
49
m_new_var = m_old_var + (s - m_old_avg) * (s - m_new_avg);
50
51
m_old_avg = m_new_avg;
52
m_old_var = m_new_var;
53
}
54
55
m_min = std::min(m_min, s);
56
m_max = std::max(m_max, s);
57
}
58
59
void
clear
() { m_n = 0; }
60
61
int
n
()
const
{
return
m_n; }
62
63
/** Average of the samples */
64
Scalar
avg
()
const
{
65
if
(m_n > 0)
66
return
m_new_avg;
67
68
return
0.0;
69
}
70
/** Variance of the samples */
71
Scalar
var
()
const
{
72
if
(m_n > 1)
73
return
m_new_var / m_n;
74
75
return
0.0;
76
}
77
78
/** Standard deviation of the samples */
79
Scalar
sig
()
const
{
return
std::sqrt(
var
()); }
80
81
/** Minimum */
82
Scalar
min
()
const
{
return
m_min; }
83
84
/** Minimum */
85
Scalar
max
()
const
{
return
m_max; }
86
87
private
:
88
int
m_n;
89
Scalar m_old_avg, m_new_avg;
90
Scalar m_old_var, m_new_var;
91
Scalar m_min, m_max;
92
};
93
}
// namespace Statistics
94
}
// namespace Utils
95
96
#endif
Utils::Statistics::RunningAverage
Keep running average and variance.
Definition
RunningAverage.hpp:36
Utils::Statistics::RunningAverage::clear
void clear()
Definition
RunningAverage.hpp:59
Utils::Statistics::RunningAverage::min
Scalar min() const
Minimum.
Definition
RunningAverage.hpp:82
Utils::Statistics::RunningAverage::avg
Scalar avg() const
Average of the samples.
Definition
RunningAverage.hpp:64
Utils::Statistics::RunningAverage::sig
Scalar sig() const
Standard deviation of the samples.
Definition
RunningAverage.hpp:79
Utils::Statistics::RunningAverage::RunningAverage
RunningAverage()
Definition
RunningAverage.hpp:38
Utils::Statistics::RunningAverage::n
int n() const
Definition
RunningAverage.hpp:61
Utils::Statistics::RunningAverage::var
Scalar var() const
Variance of the samples.
Definition
RunningAverage.hpp:71
Utils::Statistics::RunningAverage::add_sample
void add_sample(Scalar s)
Definition
RunningAverage.hpp:42
Utils::Statistics::RunningAverage::max
Scalar max() const
Minimum.
Definition
RunningAverage.hpp:85
Utils
Definition
Variant.hpp:40
src
utils
include
utils
statistics
RunningAverage.hpp
Generated on Thu Dec 19 2024 02:13:16 for ESPResSo by
1.9.8