ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
Accumulator.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19#ifndef CORE_UTILS_ACCUMULATOR
20#define CORE_UTILS_ACCUMULATOR
21
22#include <boost/serialization/access.hpp>
23#include <boost/serialization/vector.hpp>
24
25#include <algorithm>
26#include <cmath>
27#include <cstddef>
28#include <limits>
29#include <stdexcept>
30#include <vector>
31
32namespace Utils {
33
34template <typename T> struct AccumulatorData {
35 T mean = T{};
36 T m = T{};
37
38private:
39 // Allow serialization to access non-public data members.
41
42 template <typename Archive>
43 void serialize(Archive &ar, const unsigned /*version*/) {
44 ar & mean & m;
45 }
46};
47
49public:
50 explicit Accumulator(std::size_t N) : m_n(0), m_acc_data(N) {}
51 void operator()(const std::vector<double> &);
52 std::vector<double> mean() const;
53 std::vector<double> variance() const;
54 std::vector<double> std_error() const;
55
56private:
57 std::size_t m_n;
58 std::vector<AccumulatorData<double>> m_acc_data;
59 // Allow serialization to access non-public data members.
61
62 template <typename Archive>
63 void serialize(Archive &ar, const unsigned /*version*/) {
64 ar & m_n & m_acc_data;
65 }
66};
67
68inline void Accumulator::operator()(const std::vector<double> &data) {
69 if (data.size() != m_acc_data.size())
70 throw std::runtime_error(
71 "The given data size does not fit the initialized size!");
72 ++m_n;
73 if (m_n == 1) {
74 std::transform(
75 data.begin(), data.end(), m_acc_data.begin(),
76 [](double d) -> AccumulatorData<double> { return {d, 0.0}; });
77 } else {
78 std::transform(m_acc_data.begin(), m_acc_data.end(), data.begin(),
79 m_acc_data.begin(),
81 double d) -> AccumulatorData<double> {
82 auto const old_mean = a.mean;
83 auto const new_mean =
84 old_mean + (d - old_mean) / static_cast<double>(m_n);
85 auto const new_m = a.m + (d - old_mean) * (d - new_mean);
86 return {new_mean, new_m};
87 });
88 }
89}
90
91inline std::vector<double> Accumulator::mean() const {
92 std::vector<double> res;
93 std::transform(
94 m_acc_data.begin(), m_acc_data.end(), std::back_inserter(res),
95 [](const AccumulatorData<double> &acc_data) { return acc_data.mean; });
96 return res;
97}
98
99inline std::vector<double> Accumulator::variance() const {
100 std::vector<double> res;
101 if (m_n == 1) {
102 res = std::vector<double>(m_acc_data.size(),
103 std::numeric_limits<double>::max());
104 } else {
105 std::transform(m_acc_data.begin(), m_acc_data.end(),
106 std::back_inserter(res),
107 [this](const AccumulatorData<double> &acc_data) {
108 return acc_data.m / (static_cast<double>(m_n) - 1);
109 });
110 }
111 return res;
112}
113
114/**
115 * Returns the standard error of the mean assuming uncorrelated samples.
116 */
117inline std::vector<double> Accumulator::std_error() const {
118 auto const var = variance();
119 std::vector<double> err(var.size());
120 std::transform(var.begin(), var.end(), err.begin(), [this](double d) {
121 return std::sqrt(d / static_cast<double>(m_n));
122 });
123 return err;
124}
125
126} // namespace Utils
127
128#endif
std::vector< double > std_error() const
Returns the standard error of the mean assuming uncorrelated samples.
Accumulator(std::size_t N)
std::vector< double > variance() const
void operator()(const std::vector< double > &)
std::vector< double > mean() const
friend class boost::serialization::access
friend class boost::serialization::access