Datafilter vectors - stddev() and variance()

.. both take a single parameter (a vector) and compute the standard
   deviation and variance accordingly. sigma and sigma^2.
This commit is contained in:
Mark Liversedge
2020-03-24 20:01:17 +00:00
parent 74c8fddb1a
commit 8ee52edeee
2 changed files with 29 additions and 10 deletions

View File

@@ -17,6 +17,7 @@
*/
#include "Utils.h"
#include "Statistic.h"
#include "DataFilter.h"
#include "Context.h"
#include "Athlete.h"
@@ -200,6 +201,9 @@ static struct {
// not need the data to be sorted, as it uses argsort internally. As
// you can pass multiple vectors they are uniqued in sync with the first list.
{ "variance", 1 }, // variance(v) - calculates the variance for the elements in the vector.
{ "stddev", 1 }, // stddev(v) - calculates the standard deviation for elements in the vector.
// add new ones above this line
{ "", -1 }
@@ -2948,6 +2952,7 @@ Result Leaf::eval(DataFilterRuntime *df, Leaf *leaf, float x, long it, RideItem
return returning;
}
// uniq
if (leaf->function == "uniq") {
// evaluate all the lists
@@ -2968,7 +2973,7 @@ Result Leaf::eval(DataFilterRuntime *df, Leaf *leaf, float x, long it, RideItem
// diff length?
if (current.vector.count() != len) {
fprintf(stderr, "sort list '%s': not the same length, ignored\n", symbol.toStdString().c_str()); fflush(stderr);
fprintf(stderr, "uniq list '%s': not the same length, ignored\n", symbol.toStdString().c_str()); fflush(stderr);
continue;
}
@@ -3211,6 +3216,21 @@ Result Leaf::eval(DataFilterRuntime *df, Leaf *leaf, float x, long it, RideItem
return returning;
}
// stddev
if (leaf->function == "variance") {
// array
Result v = eval(df,leaf->fparms[0],x, it, m, p, c, s, d);
Statistic calc;
return calc.variance(v.vector, v.vector.count());
}
if (leaf->function == "stddev") {
// array
Result v = eval(df,leaf->fparms[0],x, it, m, p, c, s, d);
Statistic calc;
return calc.standarddeviation(v.vector, v.vector.count());
}
// pmc
if (leaf->function == "pmc") {

View File

@@ -40,15 +40,6 @@ class Statistic
double corr(QVector<double> &Xi, QVector<double> &Yi,int n);
double average(QVector<double> &Xi,int n);
protected:
long points;
double sumX, sumY;
double sumXsquared,
sumYsquared;
double sumXY;
double a, b, c; // a = intercept, b = slope, c = r2
private:
// Maths functions used by the plots
QVector<double> array_temp; //Déclaration d'un tableau temporaire
@@ -70,6 +61,14 @@ class Statistic
double variance(QVector<double> &val,int n);
double standarddeviation(QVector<double> &val,int n);
protected:
long points;
double sumX, sumY;
double sumXsquared,
sumYsquared;
double sumXY;
double a, b, c; // a = intercept, b = slope, c = r2
};
#endif