在 Java 编程中使用标准正态分布查找异常值数据

find a outlier data using standard normal distribution in Java programming

您好,我想使用标准正态分布查找异常值数据

我不习惯统计数据。所以如果我的问题有什么错误,请多多指教。

我有两个要求。

  1. 我想使用 [15, 13, 18, 20, 22, 17, 16, 16, 30, 18, 15, 16] 等整数数据列表制作标准正态分布

  2. 当像 32 这样的新数据出现时,我想检查新数据是否在标准正态分布范围内,平均值为 +- 1 西格玛

感谢阅读我的问题。

Wikipedia 所述,标准偏差 σ 可以计算为:

σ = sqrt(E[X^2] - E[X]^2)

其中 E 是预期值(平均值)。下面的简单 class 就是这样做的:

class Statistics {

    int count = 0; // num values so far
    double mean = Double.NaN; // E[X]
    double meanOfSquares = Double.NaN; // E[X^2]

    public void add(final double value) {
        if (count == 0) {
            mean = value;
            meanOfSquares = value  * value;
        } else {
            mean = (mean * count + value) / (count + 1);
            meanOfSquares = (meanOfSquares * count + value * value) / (count + 1);
        }
        count++;
    }

    public double getMean() {
        // sum of all values divided by count
        return mean;
    }

    public double getVariance() {
        // σ^2 = E[X^2] - E[X]^2;
        return meanOfSquares - mean * mean;
    }

    public double getStandardDeviation() {
        // variance is square of standard deviance
        return Math.sqrt(getVariance());
    }
}

您现在可以检查值 x 是否在一个 σ 内,如下所示:

Statistics statistics = new Statistics(): 
statistics.add(10); 
statistics.add(12); 

int x = 5; 
double mean = statistics.getMean();
double sigma = statistics.getStandardDeviation();
if (x < mean - sigma || x > mean + sigma) {
    System.out.println(x + " not within one standard deviation");
}