8 #ifndef META_OCEAN_MATH_VARIANCE_H
9 #define META_OCEAN_MATH_VARIANCE_H
18 template <
typename T>
class VarianceT;
75 inline void add(
const T& value);
82 inline void add(
const size_t size,
const T& value);
89 inline void add(
const T* values,
const size_t size);
96 inline void remove(
const T& value);
121 inline size_t size()
const;
127 explicit inline operator bool()
const;
141 template <
typename T>
147 template <
typename T>
155 squaredSum_ += value * value;
160 template <
typename T>
167 ocean_assert(size >= 1);
169 sum_ += value * T(size);
170 squaredSum_ += value * value * T(size);
175 template <
typename T>
178 for (
size_t n = 0; n < size; ++n)
184 template <
typename T>
187 ocean_assert(size_ >= 1);
191 ocean_assert(!(std::is_same<T, float>::value) || squaredSum_ + T(0.1) >= value * value);
192 ocean_assert(!(std::is_same<T, double>::value) || squaredSum_ + T(0.0001) >= value * value);
194 squaredSum_ -= value * value;
199 template <
typename T>
202 ocean_assert(size_ > 0);
212 const T varianceSquaredSize = T(size_) * T(size_);
213 const T varianceSquaredSize_2 = varianceSquaredSize / 2;
216 return (squaredSum_ * T(size_) - sum_ * sum_ + varianceSquaredSize_2) / varianceSquaredSize;
226 ocean_assert(size_ > 0);
232 return squaredSum_ / double(size_) - (sum_ * sum_) / (
double(size_) * double(size_));
242 ocean_assert(size_ > 0);
248 return squaredSum_ / float(size_) - (sum_ * sum_) / (
float(size_) * float(size_));
251 template <
typename T>
254 const T tempVariance = variance();
256 ocean_assert(!(std::is_same<T, float>::value) || tempVariance >= T(-0.1));
257 ocean_assert(!(std::is_same<T, double>::value) || tempVariance >= T(-0.0001));
267 template <
typename T>
270 ocean_assert(size_ > 0);
272 return sum_ / T(size_);
275 template <
typename T>
281 template <
typename T>
This class provides basic numeric functionalities.
Definition: Numeric.h:57
static T sqrt(const T value)
Returns the square root of a given value.
Definition: Numeric.h:1533
This class allows to determine the variance in a given data set.
Definition: Variance.h:56
void remove(const T &value)
Removes a previously added value.
Definition: Variance.h:185
VarianceT()=default
Creates a new variance object.
T deviation() const
Returns the deviation of the data set.
Definition: Variance.h:252
T sum_
Stores the data sum.
Definition: Variance.h:132
void add(const T &value)
Adds a new value.
Definition: Variance.h:148
size_t size() const
Returns the size of the data set.
Definition: Variance.h:276
T squaredSum_
Stores the squared data sum.
Definition: Variance.h:135
T variance() const
Returns the variance of the data set.
Definition: Variance.h:200
T average() const
Returns the average of the data set.
Definition: Variance.h:268
size_t size_
Size of the data set.
Definition: Variance.h:138
VarianceT< float > VarianceF
Definition of a variance object with float values.
Definition: Variance.h:39
VarianceT< Scalar > Variance
Definition of a variance object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with single ...
Definition: Variance.h:18
std::vector< Variance > Variances
Definition of a vector holding variance objects.
Definition: Variance.h:46
VarianceT< double > VarianceD
Definition of a variance object with double values.
Definition: Variance.h:32
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15