insaf021 opened a new pull request, #350:
URL: https://github.com/apache/commons-math/pull/350
With the default percentile implementation, on master:
DescriptiveStatistics stats = new DescriptiveStatistics(5); // add 5,
4, 3, 2, 1
stats.getPercentile(50); // 3.0
stats.getValues(); // [1.0, 2.0, 3.0, 4.0, 5.0] expected
[5.0, 4.0, 3.0, 2.0, 1.0]
stats.addValue(100);
stats.getValues(); // [2.0, 3.0, 4.0, 5.0, 100.0] expected
[4.0, 3.0, 2.0, 1.0, 100.0]
double[] x = {5, 4, 3, 2, 1};
StatUtils.percentile(x, 50); // x is now [1.0, 2.0, 3.0, 4.0, 5.0]
`Statistics.Percentile` and `StatUtils` build their Commons Statistics
`Quantile` with `withCopy(false)`, so it partially sorts the array it is given.
`DescriptiveStatistics.getPercentile` evaluates it through `apply`, which
passes the stored window itself, so a read-only query reorders the data. From
then on `getValues()` and `getElement(i)` no longer follow insertion order, and
a rolling window discards the smallest value on the next `addValue` rather than
the oldest, so later statistics are computed over the wrong values (mean 22.8
instead of 22.0 above). `toString()` reports the median and reorders the window
the same way. The `Percentile` class replaced in MATH-1671 (fb1c36b) worked on
a copy, so both call sites changed behaviour in that commit.
Both quantiles now use `withCopy(true)`; `Quantile.withDefaults()` is in
place, so the flag has to be explicit.
`DescriptiveStatisticsTest.testPercentileDoesNotReorderValues` (also run by
`SynchronizedDescriptiveStatisticsTest`) and
`StatUtilsTest.testPercentileDoesNotModifyInput` fail on master and pass with
the fix, and the default `mvn` build is green.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]