Skip to content

Commit

Permalink
Datafilter - quantile()
Browse files Browse the repository at this point in the history
.. quantile(vector, quantiles) - returns quantile values for the vector
   passed. quantiles can be a single value, or vector of values. The
   quantile is expressed as a value between 0 and 1, where 0.5 would
   represent the median. Values outside this range are truncated to
   0 or 1.
  • Loading branch information
liversedge committed May 11, 2020
1 parent 73f11db commit 670698f
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/Core/DataFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ static struct {
// date range, or for the given date range.
{ "daterange", 0 }, // daterange(start|stop) or daterange(from,to,expression) - first form gets the
// currently selected start/stop, second form sets from and to when executing the
// expression.}
// expression.
{ "quantile", 2 }, // quantile(vector, quantiles) - quantiles can be a number or a vector of numbers
// the vector does not need to be sorted as it will be sorted internally.

// add new ones above this line
{ "", -1 }
Expand Down Expand Up @@ -467,6 +469,11 @@ DataFilter::builtins()

returning << "bests(POWER|WPK|HR|CADENCE|SPEED, duration [,start [,stop] ])";

// 88 - daterange
} else if (i == 89) {

returning << "quantile(vector, quantiles)";

} else {

QString function;
Expand Down Expand Up @@ -3870,6 +3877,39 @@ Result Leaf::eval(DataFilterRuntime *df, Leaf *leaf, float x, long it, RideItem
return Result(i - list.vector.begin());
}

// quantile
if (leaf->function == "quantile") {

Result v= eval(df, leaf->fparms[0],x, it, m, p, c, s, d);
Result quantiles= eval(df, leaf->fparms[1],x, it, m, p, c, s, d);
Result returning(0);

#ifdef GC_WANT_GSL
if (v.vector.count() > 0) {
// sort the vector first
qSort(v.vector);

if (quantiles.vector.count() ==0) {
if (quantiles.number < 0) quantiles.number=0;
if (quantiles.number > 1) quantiles.number=1;

returning.number = gsl_stats_quantile_from_sorted_data(v.vector.constData(), 1, v.vector.count(), quantiles.number);

} else {
for (int it=0; it<quantiles.vector.count(); it++) {
double quantile= quantiles.vector.at(it);
if (quantile < 0) quantile=0;
if (quantile > 1) quantile=1;
double value = gsl_stats_quantile_from_sorted_data(v.vector.constData(), 1, v.vector.count(), quantile);
returning.number += value;
returning.vector << value;
}
}
}
#endif
return returning;
}

// sort
if (leaf->function == "sort") {

Expand Down

0 comments on commit 670698f

Please sign in to comment.