Visual Computing Library
Loading...
Searching...
No Matches
distribution.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_MATH_DISTRIBUTION_H
24#define VCL_MATH_DISTRIBUTION_H
25
26#include <vclib/types.h>
27
28#include <cmath>
29#include <limits>
30#include <set>
31
32namespace vcl {
33
41template<typename Scalar>
43{
44 std::set<Scalar> mSet;
45
46 Scalar mMin = std::numeric_limits<Scalar>::max();
47 Scalar mMax = std::numeric_limits<Scalar>::lowest();
48
49 Scalar mSum = 0;
50 Scalar mSqrdSum = 0;
51 Scalar mAvg = 0;
52 Scalar mSqrdAvg = 0;
53 Scalar mRMS = 0;
54
55public:
59 Distribution() = default;
60
64 void clear()
65 {
66 mSet.clear();
67 mMin = std::numeric_limits<Scalar>::max();
68 mMax = std::numeric_limits<Scalar>::lowest();
69 mSum = 0;
70 mSqrdSum = 0;
71 mAvg = 0;
72 mSqrdAvg = 0;
73 mRMS = 0;
74 }
75
80 void add(Scalar v)
81 {
82 mSet.insert(v);
83 if (v < mMin)
84 mMin = v;
85 if (v > mMax)
86 mMax = v;
87 mSum += v;
88 mSqrdSum += v * v;
89 mAvg = mSum / mSet.size();
90 mSqrdAvg = mSqrdSum / mSet.size();
91 mRMS = std::sqrt(mSqrdAvg);
92 }
93
100 Scalar min() const { return mMin; }
101
108 Scalar max() const { return mMax; }
109
114 uint size() const { return mSet.size(); }
115
120 Scalar sum() const { return mSum; }
121
126 Scalar average() const { return mAvg; }
127
132 Scalar rootMeanSquare() const { return mRMS; }
133
138 Scalar variance() const { return mSqrdAvg - mAvg * mAvg; }
139
144 Scalar standardDeviation() const { return std::sqrt(variance()); }
145
152 Scalar percentile(Scalar perc) const
153 {
154 assert(!mSet.empty());
155 assert(perc >= 0 && perc <= 1);
156
157 int index = mSet.size() * perc - 1;
158 if (index < 0)
159 index = 0;
160 auto it = mSet.begin();
161 std::advance(it, index);
162 return *it;
163 }
164};
165
166} // namespace vcl
167
168#endif // VCL_MATH_DISTRIBUTION_H
The Distribution class allows to collect a set of values and then compute some statistics like averag...
Definition distribution.h:43
Scalar min() const
Returns the minimum value of the distribution.
Definition distribution.h:100
Scalar variance() const
Returns the variance of the values of the distribution.
Definition distribution.h:138
uint size() const
Returns the number of values of the distribution.
Definition distribution.h:114
void clear()
Clears the distribution, removing all its values.
Definition distribution.h:64
Distribution()=default
Creates an empty distribution.
Scalar rootMeanSquare() const
Returns the root mean square of the values of the distribution.
Definition distribution.h:132
Scalar average() const
Returns the average of the values of the distribution.
Definition distribution.h:126
void add(Scalar v)
Adds a value to the distribution.
Definition distribution.h:80
Scalar sum() const
Returns the sum of the values of the distribution.
Definition distribution.h:120
Scalar max() const
Returns the maximum value of the distribution.
Definition distribution.h:108
Scalar percentile(Scalar perc) const
Returns the perc percentile of the values of the distribution.
Definition distribution.h:152
Scalar standardDeviation() const
Returns the standard deviation of the values of the distribution.
Definition distribution.h:144
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43