59 lines
1.1 KiB
C++
59 lines
1.1 KiB
C++
/*
|
||
* © Copyright 2014 – Urheberrechtshinweis
|
||
* Alle Rechte vorbehalten / All Rights Reserved
|
||
*
|
||
* Programmcode ist urheberrechtlich geschuetzt.
|
||
* Das Urheberrecht liegt, soweit nicht ausdruecklich anders gekennzeichnet, bei Frank Ebner.
|
||
* Keine Verwendung ohne explizite Genehmigung.
|
||
* (vgl. § 106 ff UrhG / § 97 UrhG)
|
||
*/
|
||
|
||
#ifndef STATS_VARIANCE_H
|
||
#define STATS_VARIANCE_H
|
||
|
||
#include "../../Assertions.h"
|
||
#include "Average.h"
|
||
|
||
namespace Stats {
|
||
|
||
template <typename Scalar> class Variance {
|
||
|
||
Average<Scalar> avg;
|
||
Average<Scalar> avg2;
|
||
|
||
public:
|
||
|
||
/** ctor */
|
||
Variance() {
|
||
;
|
||
}
|
||
|
||
/** contains valid data? */
|
||
bool isValid() const {
|
||
return avg.isValid();
|
||
}
|
||
|
||
/** add a new value */
|
||
void add(const Scalar val) {
|
||
avg.add(val);
|
||
avg2.add(val*val);
|
||
}
|
||
|
||
/** get the current variance */
|
||
Scalar get() const {
|
||
Assert::isTrue(avg.isValid(), "add() values first!");
|
||
return avg2.get() - (avg.get()*avg.get());
|
||
}
|
||
|
||
/** get the current stadard-deviation */
|
||
Scalar getStdDev() const {
|
||
return std::sqrt(get());
|
||
}
|
||
|
||
|
||
};
|
||
|
||
}
|
||
|
||
#endif // STATS_VARIANCE_H
|