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-2026 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 <iterator>
29#include <limits>
30#include <stdexcept>
31#include <vector>
32
33namespace Utils {
34
35template <typename T> struct AccumulatorData {
36 T mean = T{};
37 T m = T{};
38
39private:
40 // Allow serialization to access non-public data members.
42
43 template <typename Archive>
44 void serialize(Archive &ar, const unsigned /*version*/) {
45 ar & mean & m;
46 }
47};
48
50public:
51 explicit Accumulator(std::size_t N) : m_n(0u), m_acc_data(N) {}
52 void operator()(const std::vector<double> &);
53 std::vector<double> mean() const;
54 std::vector<double> variance() const;
55 std::vector<double> std_error() const;
56
57private:
58 std::size_t m_n;
59 std::vector<AccumulatorData<double>> m_acc_data;
60 // Allow serialization to access non-public data members.
62
63 template <typename Archive>
64 void serialize(Archive &ar, const unsigned /*version*/) {
65 ar & m_n & m_acc_data;
66 }
67};
68
69inline void Accumulator::operator()(const std::vector<double> &data) {
70 if (data.size() != m_acc_data.size())
71 throw std::runtime_error(
72 "The given data size does not fit the initialized size!");
73 ++m_n;
74 if (m_n == 1u) {
75 std::ranges::transform(
76 data, m_acc_data.begin(),
77 [](double d) -> AccumulatorData<double> { return {d, 0.0}; });
78 } else {
79 auto const denominator = static_cast<double>(m_n);
80 std::ranges::transform(m_acc_data, data, m_acc_data.begin(),
81 [denominator](auto const &a, double d) {
82 auto const old_mean = a.mean;
83 auto const new_mean =
84 old_mean + (d - old_mean) / denominator;
85 auto const new_m =
86 a.m + (d - old_mean) * (d - new_mean);
87 return AccumulatorData<double>{new_mean, new_m};
88 });
89 }
90}
91
92/**
93 * @brief Compute the sample mean.
94 */
95inline std::vector<double> Accumulator::mean() const {
96 std::vector<double> res{};
97 std::ranges::transform(m_acc_data, std::back_inserter(res),
98 [](auto const &acc_data) { return acc_data.mean; });
99 return res;
100}
101
102/**
103 * @brief Compute the Bessel-corrected sample variance,
104 * assuming uncorrelated data.
105 */
106inline std::vector<double> Accumulator::variance() const {
107 std::vector<double> res{};
108 if (m_n == 1u) {
109 res = std::vector<double>(m_acc_data.size(),
110 std::numeric_limits<double>::max());
111 } else {
112 auto const denominator = static_cast<double>(m_n) - 1.;
113 std::ranges::transform(m_acc_data, std::back_inserter(res),
114 [denominator](auto const &acc_data) {
115 return acc_data.m / denominator;
116 });
117 }
118 return res;
119}
120
121/**
122 * @brief Compute the standard error of the mean, assuming uncorrelated data.
123 */
124inline std::vector<double> Accumulator::std_error() const {
125 auto const var = variance();
126 std::vector<double> err{};
127 auto const denominator = static_cast<double>(m_n);
128 std::ranges::transform(var, std::back_inserter(err), [denominator](double d) {
129 return std::sqrt(d / denominator);
130 });
131 return err;
132}
133
134} // namespace Utils
135
136#endif
std::vector< double > std_error() const
Compute the standard error of the mean, assuming uncorrelated data.
Accumulator(std::size_t N)
std::vector< double > variance() const
Compute the Bessel-corrected sample variance, assuming uncorrelated data.
void operator()(const std::vector< double > &)
std::vector< double > mean() const
Compute the sample mean.
friend class boost::serialization::access
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
friend class boost::serialization::access