Apache Commons Math3 数字百分位数

Apache Commons Math3 Percentile of Number

我正在尝试使用 Apache Commons Math3 库和百分位数 class:

获取分布中特定数字的百分位数

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 作为结果,即给定数字的百分位数,而不是给定百分位数的数字。

此 class 上的

None 方法似乎适合或给出我想要的结果。我在滥用 class 吗?我应该使用另一个 class 吗?

您可以使用加载了基线值的 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);
}

似乎不​​起作用,因为这个测试应该通过(但实际上没有)

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

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

    //when
    double percentile = distribution.cumulativeProbability(2.7d);

    //then
    assertThat(percentile).isEqualTo(0.25);
}