RunningStatistics.hpp
Go to the documentation of this file.
1/*
2 Copyright 2024 Equinor ASA.
3
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM 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 OPM 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 OPM. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef OPM_RUNNING_STATISTICS_HPP
21#define OPM_RUNNING_STATISTICS_HPP
22
23#include <cmath>
24#include <limits>
25#include <optional>
26
27namespace Opm {
28
34template <typename Scalar>
36{
37public:
43 template <class Serializer>
44 void serializeOp(Serializer& serializer)
45 {
46 serializer(this->sampleSize_);
47 serializer(this->min_);
48 serializer(this->max_);
49 serializer(this->mean_);
50 serializer(this->totalVariance_);
51 }
52
55 {
56 auto stat = RunningStatistics{};
57
58 stat.sampleSize_ = 12;
59 stat.min_ = -static_cast<Scalar>(1);
60 stat.max_ = static_cast<Scalar>(2);
61 stat.mean_ = static_cast<Scalar>(0.03);
62 stat.totalVariance_ = static_cast<Scalar>(0.4);
63
64 return stat;
65 }
66
73 bool operator==(const RunningStatistics& that) const
74 {
75 return (this->sampleSize_ == that.sampleSize_)
76 && (this->min_ == that.min_)
77 && (this->max_ == that.max_)
78 && (this->mean_ == that.mean_)
79 && (this->totalVariance_ == that.totalVariance_)
80 ;
81 }
82
85 void reset()
86 {
87 this->sampleSize_ = 0;
88 this->min_ = std::numeric_limits<Scalar>::max();
89 this->max_ = std::numeric_limits<Scalar>::lowest();
90 this->mean_ = Scalar{};
91 this->totalVariance_ = Scalar{};
92 }
93
99 void addSamplePoint(const Scalar x)
100 {
101 if (x < this->min_) { this->min_ = x; }
102 if (x > this->max_) { this->max_ = x; }
103
104 // https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
105
106 ++this->sampleSize_;
107
108 const auto d1 = x - this->mean();
109 this->mean_ += d1 / this->sampleSize_;
110
111 const auto d2 = x - this->mean();
112 this->totalVariance_ += d1 * d2;
113 }
114
119 std::size_t sampleSize() const { return this->sampleSize_; }
120
122 Scalar min() const { return this->min_; }
123
125 Scalar max() const { return this->max_; }
126
128 Scalar mean() const { return this->mean_; }
129
134 std::optional<Scalar> stdev() const
135 {
136 if (this->sampleSize_ < 2) {
137 return {};
138 }
139
140 using std::sqrt;
141 return sqrt(this->totalVariance_ / (this->sampleSize_ - 1));
142 }
143
144private:
146 std::size_t sampleSize_{};
147
149 Scalar min_ { std::numeric_limits<Scalar>::max() };
150
152 Scalar max_ { std::numeric_limits<Scalar>::lowest() };
153
155 Scalar mean_{};
156
158 Scalar totalVariance_{};
159};
160
161} // namespace Opm
162
163#endif // OPM_RUNNING_STATISTICS_HPP
Definition: RunningStatistics.hpp:36
Scalar max() const
Retrieve largest sample value seen so far.
Definition: RunningStatistics.hpp:125
std::size_t sampleSize() const
Definition: RunningStatistics.hpp:119
void reset()
Definition: RunningStatistics.hpp:85
Scalar min() const
Retrieve smallest sample value seen so far.
Definition: RunningStatistics.hpp:122
void addSamplePoint(const Scalar x)
Definition: RunningStatistics.hpp:99
Scalar mean() const
Retrieve arithmetic average of all sample points seen so far.
Definition: RunningStatistics.hpp:128
std::optional< Scalar > stdev() const
Definition: RunningStatistics.hpp:134
void serializeOp(Serializer &serializer)
Definition: RunningStatistics.hpp:44
static RunningStatistics serializationTestObject()
Create a serialisation test object.
Definition: RunningStatistics.hpp:54
bool operator==(const RunningStatistics &that) const
Definition: RunningStatistics.hpp:73
Definition: blackoilboundaryratevector.hh:39