java - 检测集合中不同元素的高效算法

标签 java algorithm collections statistics anova

假设您有一组五个元素 (A-E),其中包含一些测量属性的数值(每个元素的多个观察值,例如“心率”):

A = {100, 110, 120, 130}
B = {110, 100, 110, 120, 90}
C = { 90, 110, 120, 100}
D = {120, 100, 120, 110, 110, 120}
E = {110, 120, 120, 110, 120}

首先,我必须检测平均水平是否存在显着差异。所以我以一种方式运行 ANOVA使用 Statistical package provided by Apache Commons Math .到目前为止没有问题,我获得了一个 boolean 值,告诉我是否发现差异。

其次,如果发现差异,我需要知道与其余部分不同的元素(或多个元素)。我打算使用 unpaired t-tests ,比较每对元素(A 与 B,A 与 C .... D 与 E),以了解一个元素是否不同于另一个元素。所以,在这一点上,我得到了与其他元素存在显着差异的元素列表的信息,例如:

C is different than B
C is different than D

但我需要一种通用算法来利用该信息有效地确定哪些元素与其他元素不同(示例中为 C,但可能不止一个)。

撇开统计问题不谈,问题可能是(一般而言):“鉴于集合中每对元素的相等/不相等信息,您如何确定元素与其他人不同吗?”

这似乎是一个可以应用图论的问题。如果有用的话,我正在使用 Java 语言来实现。

编辑:元素是人,测量值是完成任务所需的时间。我需要在某种欺诈检测系统中检测谁花费过多或过少的时间来完成任务。

最佳答案

以防万一有人对最终代码感兴趣,使用 Apache Commons Math进行统计操作,Trove使用原始类型的集合。

它寻找度数最高的元素(这个想法基于@Pace 和@Aniko 的评论,谢谢)。

我认为最终的算法是 O(n^2),欢迎提出建议。它应该适用于涉及一个定量变量与一个定量变量的任何问题,假设观察结果是正态的。

import gnu.trove.iterator.TIntIntIterator;
import gnu.trove.map.TIntIntMap;
import gnu.trove.map.hash.TIntIntHashMap;
import gnu.trove.procedure.TIntIntProcedure;
import gnu.trove.set.TIntSet;
import gnu.trove.set.hash.TIntHashSet;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.math.MathException;
import org.apache.commons.math.stat.inference.OneWayAnova;
import org.apache.commons.math.stat.inference.OneWayAnovaImpl;
import org.apache.commons.math.stat.inference.TestUtils;


public class TestMath {
    private static final double SIGNIFICANCE_LEVEL = 0.001; // 99.9%

    public static void main(String[] args) throws MathException {
        double[][] observations = {
           {150.0, 200.0, 180.0, 230.0, 220.0, 250.0, 230.0, 300.0, 190.0 },
           {200.0, 240.0, 220.0, 250.0, 210.0, 190.0, 240.0, 250.0, 190.0 },
           {100.0, 130.0, 150.0, 180.0, 140.0, 200.0, 110.0, 120.0, 150.0 },
           {200.0, 230.0, 150.0, 230.0, 240.0, 200.0, 210.0, 220.0, 210.0 },
           {200.0, 230.0, 150.0, 180.0, 140.0, 200.0, 110.0, 120.0, 150.0 }
        };

        final List<double[]> classes = new ArrayList<double[]>();
        for (int i=0; i<observations.length; i++) {
            classes.add(observations[i]);
        }

        OneWayAnova anova = new OneWayAnovaImpl();
//      double fStatistic = anova.anovaFValue(classes); // F-value
//      double pValue = anova.anovaPValue(classes);     // P-value

        boolean rejectNullHypothesis = anova.anovaTest(classes, SIGNIFICANCE_LEVEL);
        System.out.println("reject null hipothesis " + (100 - SIGNIFICANCE_LEVEL * 100) + "% = " + rejectNullHypothesis);

        // differences are found, so make t-tests
        if (rejectNullHypothesis) {
            TIntSet aux = new TIntHashSet();
            TIntIntMap fraud = new TIntIntHashMap();

            // i vs j unpaired t-tests - O(n^2)
            for (int i=0; i<observations.length; i++) {
                for (int j=i+1; j<observations.length; j++) {
                    boolean different = TestUtils.tTest(observations[i], observations[j], SIGNIFICANCE_LEVEL);
                    if (different) {
                        if (!aux.add(i)) {
                            if (fraud.increment(i) == false) {
                                fraud.put(i, 1);
                            }
                        }
                        if (!aux.add(j)) {
                            if (fraud.increment(j) == false) {
                                fraud.put(j, 1);
                            }
                        }
                    }           
                }
            }

            // TIntIntMap is sorted by value
            final int max = fraud.get(0);
            // Keep only those with a highest degree
            fraud.retainEntries(new TIntIntProcedure() {
                @Override
                public boolean execute(int a, int b) {
                    return b != max;
                }
            });

            // If more than half of the elements are different
            // then they are not really different (?)
            if (fraud.size() > observations.length / 2) {
                fraud.clear();
            }

            // output
            TIntIntIterator it = fraud.iterator();
            while (it.hasNext()) {
                it.advance();
                System.out.println("Element " + it.key() + " has significant differences");             
            }
        }
    }
}

关于java - 检测集合中不同元素的高效算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2326399/

相关文章:

c# - 集合的一个字段上的模式匹配

java - JPA 标准 API : Aggregating on Multiple Columns with IF Condition

java - 如何在 Java 中比较字符串?

algorithm - 获得超过 2 个目标的帕累托前沿

c# - 在树结构上实现 IEnumerable

java - 通过单次查找初始化 Java Map 中的条目(就像在 C++ 中一样)

java - 字段authenticationManager LoginController需要一个类型为AuthenticationManager'的bean,但无法找到

java - 不断检查时间?

c# - 如何根据距离数据在面板上分配点?

算法:连接器的优化