Java 计算列表中每个值的百分位分数

Java 计算列表中每个值的百分位分数,java,statistics,percentile,apache-commons-math,Java,Statistics,Percentile,Apache Commons Math,我一直在寻找一种方法来计算给定列表中每个值的百分位排名,但到目前为止我还没有成功 org.apache.commons.math3提供了一种从值列表中获取第pth百分位数的方法,但我想要的恰恰相反。我想对列表中的每个值进行排名。有人知道Apache commons math中的库或方法可以实现这一点吗 例如:给定一个值列表{1,2,3,4,5},我希望每个值都有百分位数排名,最大百分位数为99或100,最小为0或1 更新代码: public class TestPercentile { pub

我一直在寻找一种方法来计算给定列表中每个值的百分位排名,但到目前为止我还没有成功

org.apache.commons.math3
提供了一种从值列表中获取第pth百分位数的方法,但我想要的恰恰相反。我想对列表中的每个值进行排名。有人知道Apache commons math中的库或方法可以实现这一点吗

例如:给定一个值列表
{1,2,3,4,5}
,我希望每个值都有百分位数排名,最大百分位数为99或100,最小为0或1

更新代码:

public class TestPercentile {

public static void main(String args[]) {
    double x[] = { 10, 11, 12, 12, 12, 12, 15, 18, 19, 20 };
    calculatePercentiles(x);
}

public static void calculatePercentiles(double[] arr) {
    for (int i = 0; i < arr.length; i++) {
        int count = 0;
        int start = i;
        if (i > 0) {
            while (i > 0 && arr[i] == arr[i - 1]) {
                count++;
                i++;
            }
        }
        double perc = ((start - 0) + (0.5 * count));
        perc = perc / (arr.length - 1);
        for (int k = 0; k < count + 1; k++)
            System.out.println("Percentile for value " + (start + k + 1)
                    + " = " + perc * 100);
    }
}}

Sample Output: 
Percentile for value 1 = 0.0
Percentile for value 2 = 11.11111111111111
Percentile for value 3 = 22.22222222222222
Percentile for value 4 = 50.0
Percentile for value 5 = 50.0
Percentile for value 6 = 50.0
Percentile for value 7 = 50.0
Percentile for value 8 = 77.77777777777779
Percentile for value 9 = 88.88888888888889
Percentile for value 10 = 100.0
公共类测试百分比{
公共静态void main(字符串参数[]){
双x[]={10,11,12,12,12,12,15,18,19,20};
计算百分位数(x);
}
公共静态无效计算百分比(双[]arr){
对于(int i=0;i0){
而(i>0&&arr[i]==arr[i-1]){
计数++;
i++;
}
}
double perc=((开始-0)+(0.5*计数));
perc=perc/(阵列长度-1);
对于(int k=0;k
有人能告诉我这是否正确,是否有一个更干净的图书馆


谢谢大家!

这取决于你对百分位数的定义。下面是使用并重新缩放为0-1间隔的解决方案。很好,NaturalRanking已经实现了一些处理相等值和NAN的策略

import java.util.Arrays;
import org.apache.commons.math3.stat.ranking.NaNStrategy;
import org.apache.commons.math3.stat.ranking.NaturalRanking;
import org.apache.commons.math3.stat.ranking.TiesStrategy;

public class Main {

    public static void main(String[] args) {
        double[] arr = {Double.NaN, 10, 11, 12, 12, 12, 12, 15, 18, 19, 20};

        PercentilesScaledRanking ranking = new PercentilesScaledRanking(NaNStrategy.REMOVED, TiesStrategy.MAXIMUM);
        double[] ranks = ranking.rank(arr);

        System.out.println(Arrays.toString(ranks));
        //prints:
        //[0.1, 0.2, 0.6, 0.6, 0.6, 0.6, 0.7, 0.8, 0.9, 1.0]
    }
}

class PercentilesScaledRanking extends NaturalRanking {

    public PercentilesScaledRanking(NaNStrategy nanStrategy, TiesStrategy tiesStrategy) {
        super(nanStrategy, tiesStrategy);
    }

    @Override
    public double[] rank(double[] data) {
        double[] rank = super.rank(data);
        for (int i = 0; i < rank.length; i++) {
            rank[i] = rank[i] / rank.length;
        }
        return rank;
    }
}
导入java.util.array;
导入org.apache.commons.math3.stat.ranking.NaNStrategy;
导入org.apache.commons.math3.stat.ranking.NaturalRanking;
导入org.apache.commons.math3.stat.ranking.Strategy;
公共班机{
公共静态void main(字符串[]args){
double[]arr={double.NaN,10,11,12,12,12,15,18,19,20};
PercentilesCaledRanking排名=新的PercentilesCaledRanking(NansStrategy.REMOVED,TiesStrategy.MAXIMUM);
双[]等级=等级。等级(arr);
System.out.println(array.toString(ranks));
//印刷品:
//[0.1, 0.2, 0.6, 0.6, 0.6, 0.6, 0.7, 0.8, 0.9, 1.0]
}
}
类百分比ScaleDranking扩展了自然存储{
公共百分位数统计(南战略南战略,TIES战略){
超级(南战略、战略);
}
@凌驾
公共双[]等级(双[]数据){
双[]秩=超级秩(数据);
for(int i=0;i
你怎么办?展示你做了哪些更新!