java – Apache Commons Math 2.2 Percentile bug?

I'm not 100% sure whether it's a mistake or I didn't do the right thing, but if you give percentile data of the same size (see the code below), the evaluate method takes a long time If you give percentile, the random value evaluate will take quite a short time

As described below, median is a child tag of percentile

Percentile java doc

private void testOne(){
  int size = 200000;
  int sameValue = 100;
  List<Double> list = new ArrayList<Double>();

  for (int i = 0; i < size; i++)
  {
    list.add((double)sameValue);
  }
  Median m = new Median();
  m.setData(ArrayUtils.toPrimitive(list.toArray(new Double[0])));

  long start = System.currentTimeMillis();
  System.out.println("Start:"+ start);

  double result = m.evaluate();

  System.out.println("Result:" + result);
  System.out.println("Time:"+ (System.currentTimeMillis()- start));
}


private void testTwo(){
  int size = 200000;
  List<Double> list = new ArrayList<Double>();

  Random r = new Random();

  for (int i = 0; i < size; i++)
  {
    list.add(r.nextDouble() * 100.0);
  }
  Median m = new Median();
  m.setData(ArrayUtils.toPrimitive(list.toArray(new Double[0])));

  long start = System.currentTimeMillis();
  System.out.println("Start:"+ start);

  double result = m.evaluate();

  System.out.println("Result:" + result);
  System.out.println("Time:"+ (System.currentTimeMillis()- start));
}

Solution

This is a known issue between versions 2.0 and 2.1 and has been fixed to version 3.1

Version 2.0 does involve sorting data, but in 2.1 they seem to have switched to selection algorithm However, a bug in their implementation leads to some bad behavior on a large number of data with the same value Basically, they use > = and < = instead of > and <

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>