statistic helper classes

test-cases
modified grid importance for better trap-detection
This commit is contained in:
2016-10-01 13:17:14 +02:00
parent 729340031d
commit 51c0945e12
15 changed files with 343 additions and 64 deletions

View File

@@ -91,6 +91,15 @@ public:
}
/** get a random element based on its probability */
T get() {
double tmp; // ignored
return get(tmp);
}
/**
* get a random element based on its probability.
* the probability of the picked element is returned using the out parameter
*/
T get(double& elemProbability) {
// generate random number between [0:cumProbability]

View File

@@ -1,43 +0,0 @@
#ifndef MEDIAN_H
#define MEDIAN_H
#include <vector>
#include <algorithm>
template <typename Scalar> class Median {
private:
/** all scalar values in a sorted order (ascending) */
std::vector<Scalar> sorted;
public:
/** add the given scalar value to the median calculation */
void add(const Scalar value) {
const auto idx = std::upper_bound( sorted.begin(), sorted.end(), value );
sorted.insert( idx, value );
}
/** get the median of all added values */
float get() const {
if (sorted.size() % 2 == 1) { // odd
const int idx = sorted.size()/2;
return sorted[idx];
} else { // even
const int idx0 = sorted.size()/2 - 1;
return (sorted[idx0] + sorted[idx0+1]) / 2;
}
}
};
#endif // MEDIAN_H

9
math/Stats.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef MATH_STATS_H
#define MATH_STATS_H
#include "stats/Average.h"
#include "stats/Median.h"
#include "stats/Minimum.h"
#include "stats/Maximum.h"
#endif // MATH_STATS_H

43
math/stats/Average.h Normal file
View File

@@ -0,0 +1,43 @@
#ifndef AVERAGE_H
#define AVERAGE_H
#include "../../Assertions.h"
namespace Stats {
template <typename Scalar> class Average {
private:
int cnt;
Scalar sum;
public:
/** ctor */
Average() : cnt(0), sum(0) {
;
}
/** contains a valid average? */
bool isValid() const {
return cnt > 0;
}
/** add a new value */
void add(const Scalar val) {
sum += val;
++cnt;
}
/** get the current value */
Scalar get() const {
Assert::isNot0(cnt, "add() values first!");
return sum / (Scalar)cnt;
}
};
}
#endif // AVERAGE_H

42
math/stats/Maximum.h Normal file
View File

@@ -0,0 +1,42 @@
#ifndef STATS_MAXIMUM_H
#define STATS_MAXIMUM_H
#include "../../Assertions.h"
namespace Stats {
template <typename Scalar> class Maximum {
private:
const Scalar START = -99999999;
Scalar curMax;
public:
/** ctor */
Maximum() : curMax(START) {
;
}
/** is a valid maximum available? */
inline bool isValid() const {
return curMax != START;
}
/** add a new value */
inline void add(const Scalar val) {
if (val > curMax) {curMax = val;}
}
/** get the current value */
inline Scalar get() const {
Assert::notEqual(curMax, START, "add() values first!");
return curMax;
}
};
}
#endif // STATS_MAXIMUM_H

52
math/stats/Median.h Normal file
View File

@@ -0,0 +1,52 @@
#ifndef STATS_MEDIAN_H
#define STATS_MEDIAN_H
#include <vector>
#include <algorithm>
#include "../../Assertions.h"
namespace Stats {
template <typename Scalar> class Median {
private:
/** all scalar values in a sorted order (ascending) */
std::vector<Scalar> sorted;
public:
/** add the given scalar value to the median calculation */
void add(const Scalar value) {
const auto idx = std::upper_bound( sorted.begin(), sorted.end(), value );
sorted.insert( idx, value );
}
/** get the median of all added values */
float get() const {
// sanity check
Assert::isNot0(sorted.size(), "add elements first!");
if (sorted.size() % 2 == 1) { // odd
const int idx = sorted.size()/2;
return sorted[idx];
} else { // even
const int idx0 = sorted.size()/2 - 1;
return (sorted[idx0] + sorted[idx0+1]) / 2;
}
}
};
}
#endif // STATS_MEDIAN_H

40
math/stats/Minimum.h Normal file
View File

@@ -0,0 +1,40 @@
#ifndef STATS_MINIMUM_H
#define STATS_MINIMUM_H
namespace Stats {
template <typename Scalar> class Minimum {
private:
const Scalar START = +999999999;
Scalar curMin;
public:
/** ctor */
Minimum() : curMin(START) {
;
}
/** is a valid minimum available? */
inline bool isValid() const {
return curMin != START;
}
/** add a new value */
void add(const Scalar val) {
if (val < curMin) {curMin = val;}
}
/** get the current value */
Scalar get() const {
Assert::notEqual(curMin, START, "add() values first!");
return curMin;
}
};
}
#endif // STATS_MINIMUM_H