9 #include <nlohmann/json.hpp> 17 template <std::floating_point value_type = double,
18 std::unsigned_integral counter_type =
unsigned long int>
27 [[nodiscard]]
bool empty()
const;
33 explicit operator value_type()
const {
return avg(); }
37 explicit operator bool()
const {
return number_of_samples > 0; }
44 void add(
const value_type value)
46 if (number_of_samples == std::numeric_limits<counter_type>::max()) {
47 throw std::overflow_error(
"max. number of samples reached");
64 bool operator==(
const Average& other)
const 87 throw std::overflow_error(
"maximum number of samples reached");
89 if (std::numeric_limits<value_type>::max() - other.
value_sum < value_sum) {
90 throw std::overflow_error(
"value too large");
95 return summed_average;
98 friend std::ostream& operator<<(std::ostream& stream,
const Average& average)
104 Average& operator<<(std::istream& stream)
106 stream >> number_of_samples >>
value_sum;
110 template <
class Archive>
void serialize(Archive& archive)
112 archive(value_sum, number_of_samples);
116 template <std::
floating_po
int value_type, std::
unsigned_
integral counter_type>
126 template <std::floating_point value_type = double,
127 std::unsigned_integral counter_type =
unsigned long int>
131 value_type squared_value_sum = 0.0;
133 using base::number_of_samples;
134 using base::value_sum;
144 return std::sqrt(squared_value_sum / static_cast<value_type>(
number_of_samples));
149 return empty() ? 0.0 : stdev() /
avg();
157 const auto mean =
avg();
158 return std::sqrt((squared_value_sum +
169 void add(
const value_type value)
172 squared_value_sum += value * value;
189 (squared_value_sum == other.squared_value_sum);
200 friend std::ostream& operator<<(std::ostream& stream,
const AverageStdev& average)
203 << average.squared_value_sum;
213 template <
class Archive>
void serialize(
AverageStdev& archive)
239 template <std::floating_point value_type = double,
240 std::unsigned_integral counter_type =
unsigned long int>
245 unsigned int nsamples = 0;
246 std::vector<Statistic> blocked_statistics = {
Statistic()};
247 std::vector<value_type> waiting_sample = {0.0};
248 std::vector<bool> waiting_sample_exists = {
false};
254 if (nsamples >= static_cast<unsigned int>(std::pow(2, blocked_statistics.size()))) {
255 blocked_statistics.emplace_back();
256 waiting_sample.push_back(0.0);
257 waiting_sample_exists.push_back(
false);
259 std::floating_point
auto carry = new_sample;
260 blocked_statistics.at(0) += new_sample;
262 for (
size_t i = 1; i < blocked_statistics.size(); i++) {
263 if (waiting_sample_exists.at(i)) {
264 new_sample = 0.5 * (waiting_sample.at(i) + carry);
266 blocked_statistics.at(i) += new_sample;
267 waiting_sample_exists.at(i) =
false;
270 waiting_sample_exists.at(i) =
true;
271 waiting_sample.at(i) = carry;
278 auto size()
const {
return nsamples; }
280 [[nodiscard]]
bool empty()
const {
return nsamples == 0; }
282 [[maybe_unused]]
void to_disk(
const std::string& filename)
const 284 if (
auto stream = std::ofstream(filename); stream) {
285 for (
size_t i = 0; i < blocked_statistics.size(); i++) {
286 stream << std::format(
"{} {} {}\n", i, blocked_statistics[i].
avg(),
287 blocked_statistics[i].stdev());
292 void to_json(nlohmann::json& j)
const 294 j = nlohmann::json::array();
295 for (
size_t i = 0; i < blocked_statistics.size(); i++) {
296 j.push_back(nlohmann::json::array(
297 {blocked_statistics[i].avg(), blocked_statistics[i].stdev()}));
320 template <Averageable T,
typename counter_type =
unsigned long int>
class AverageObj 330 : number_of_samples(1)
336 if (number_of_samples == std::numeric_limits<counter_type>::max()) {
337 throw std::overflow_error(
"maximum samples reached");
347 if (number_of_samples > 0) {
354 operator T()
const {
return avg(); }
360 [[nodiscard]]
bool empty()
const {
return number_of_samples == 0; }
374 template <
typename T,
typename int_t =
unsigned long int>
385 : sum_squared(
T()) {};
389 , sum_squared(value * value) {};
395 sum_squared += std::pow(value, 2);
405 return std::pow(sum_squared * (1.0 / static_cast<double>(
number_of_samples)), 0.5);
415 const auto mean =
avg();
416 return std::pow((sum_squared + N * mean * mean - 2.0 * sum * mean) * (1.0 / (N - 1.0)),
counter_type number_of_samples
number of values in average
Definition: average.h:22
bool empty() const
True if empty.
Definition: average.h:117
Class to collect averages.
Definition: average.h:19
auto rsd() const
Relative standard deviation or coefficient of variation.
Definition: average.h:147
Definition: average.h:375
double T
floating point size
Definition: units.h:73
T stdev() const
Standard deviation.
Definition: average.h:409
Average & operator+=(const value_type value)
Add value to average.
Definition: average.h:58
Class to collect averages and standard deviation.
Definition: average.h:128
AverageStdev & operator+=(const value_type value)
Add value to average.
Definition: average.h:180
bool operator<(const Average &other) const
Compare means.
Definition: average.h:35
Average & operator=(const value_type value)
Clear and assign a new value.
Definition: average.h:70
void clear()
Clear all data.
Definition: average.h:140
auto operator+(const Average &other) const
Merge two averages with correct weights.
Definition: average.h:83
void clear()
Clear all data.
Definition: average.h:25
AverageStdev & operator=(const value_type value)
Clear and assign a new value.
Definition: average.h:193
value_type value_sum
Sum of all recorded values.
Definition: average.h:23
auto avg() const
Average.
Definition: average.h:31
void add(const value_type value)
Add value to average.
Definition: average.h:169
AverageObj(const T &value)
Construct from empty object.
Definition: average.h:329
Cell list class templates.
Definition: actions.cpp:11
bool empty() const
True if empty.
Definition: average.h:360
AverageObj & operator+=(const T &value)
Add to average.
Definition: average.h:334
auto size() const
Number of samples.
Definition: average.h:366
T rms() const
Root-mean-square.
Definition: average.h:400
concept Averageable
Requirements for types used with AverageObj
Definition: average.h:304
void add(const value_type value)
Add value to average.
Definition: average.h:44
value_type stdev() const
Standard deviation.
Definition: average.h:152
AverageObjStdev(const T &value)
Construct from empty object.
Definition: average.h:387
auto size() const
Number of samples.
Definition: average.h:29
Simple class to average data contained in objects.
Definition: average.h:320
AverageObjStdev & operator+=(const T &value)
Add to average.
Definition: average.h:392
bool operator<(const AverageObj &other) const
Compare operator.
Definition: average.h:357
T avg() const
Calculate average.
Definition: average.h:345
"Decorrelation" class from https://dx.doi.org/10.1002/jcc.20746
Definition: average.h:241
auto rms() const
Root-mean-square.
Definition: average.h:142