Apache Commons Math3 Percentile of Number

Ric*_*nka 7 java apache-commons

我正在尝试使用Apache Commons Math3库和Percentile类获取分发中特定数字的百分位数:

https://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/stat/descriptive/rank/Percentile.html

(我在Scala中使用它)

如果我做:

new Percentile().evaluate(Array(1,2,3,4,5), 80)

然后我4回来了.但是,我想转向另一个方向,并4作为输入,并返回80结果,即给定数字的百分位数,而不是给定百分位数的数字.

这个类中的所有方法似乎都不适合或给出我想要的结果.我是否在滥用课程?还有我应该使用的另一课吗?

jfc*_*edo 4

您可以使用加载了基线值的 EmpiricalDistribution:

@Test
public void testCalculatePercentile() {
    //given
    double[] values = new double[]{1,2,3,4,5};

    EmpiricalDistribution distribution = new EmpiricalDistribution(values.length);
    distribution.load(values);

    //when
    double percentile = distribution.cumulativeProbability(4);

    //then
    assertThat(percentile).isEqualTo(0.8);
}
Run Code Online (Sandbox Code Playgroud)