计算Java中的标准偏差,方差

时间:2017-04-28 08:24:16

标签: java math statistics

为许多不同的聚合函数构建测试用例。

尝试手动创建公式来计算标准差,方差。

到目前为止,

我有这些方程式,但我的计算有点偏执

//Calculate Standard Deviation
double Sum1 = 0;
double Sum2 = 0;
long count = 0;



public Object compute()
{
if (count > 0)
      return Math.sqrt(count*Sum2 - Math.pow(Sum1. 2))/ count;
else
    return null;
}

//Calculate Variance
double sum = 0;
double count = 0;

public Object compute()
{
if(count>0)
    return (Math.pow(sum-(sum/count),1))/count;
else
    return null;

我最困难的地方是我知道在尝试计算方差时我可以通过以下方式计算平均值:

Math.pow(sum, 1)/count;

我在尝试计算所有这些数字的偏差的下一部分时遇到了麻烦。

我已经设置了以这种方式检索所有数字以使这些计算有效的设置。只是没有正确的方程式。如果我能用一个方程式而不是单独进行,那将是完美的。如果有人可以提供帮助,将不胜感激。 谢谢

2 个答案:

答案 0 :(得分:0)

为什么不使用数组,以便您可以拥有任意数量的数字,而不必担心还会说明数据的数量。

double[] data = {10.0,20.0,30.0,40.0,50.0,60.0,70.0,80,0,90.0,100.0};

// The mean average
double mean = 0.0;
for (int i = 0; i < data.length; i++) {
        mean += data[i];
}
mean /= data.length;

// The variance
double variance;
for (int i = 0; i < data.length; i++) {
    variance += (data[i] - mean) * (data[i] - mean);
}
variance /= data.length;

// Standard Deviation
double std = Math.sqrt(variance);

答案 1 :(得分:0)

这是一个为长值而不需要数组或列表的类。根据需要修改。

package statistics;

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * Calculate statistics without having to maintain arrays or lists in memory
 * @link http://stackoverflow.com/questions/43675485/calculating-standard-deviation-variance-in-java
 */
public class StatisticsUtils {
    private static final String DEFAULT_FORMAT = "0.###";
    private static final NumberFormat FORMATTER = new DecimalFormat(DEFAULT_FORMAT);

    private long sum;
    private long squares;
    private long count;
    private long max;
    private long min;
    private long last;
    private long failureCount;
    private long resetCount;
    private String lastFailureReason;

    public StatisticsUtils() {
        reset();
    }

    public synchronized void addFailure(String reason) {
        this.lastFailureReason = reason;
        this.failureCount++;
    }

    public synchronized void addValue(long x) {
        sum += x;
        squares += x * x;
        min = ((x < min) ? x : min);
        max = ((x > max) ? x : max);
        last = x;
        ++count;

        // If the sum of squares exceeds Long.MAX_VALUE, this means the
        // value has overflowed; reset the state back to zero and start again.
        // All previous calculations are lost.  (Better as all doubles?)
        if (squares < 0L) {
            reset();
        }
    }

    public synchronized void reset() {
        sum = 0L;
        squares = 0L;
        count = 0L;
        max = Long.MIN_VALUE;
        min = Long.MAX_VALUE;
        last = 0L;
        this.resetCount++;
    }

    public synchronized double getMean() {
        double mean = 0.0;
        if (count > 0L) {
            mean = (double) sum/count;
        }
        return mean;
    }

    public synchronized double getVariance() {
        double variance = 0.0;
        if (count > 1L) {
            variance = (squares-(double)sum*sum/count)/(count-1);
        }
        return variance;
    }

    public synchronized double getStdDev() {
        return Math.sqrt(this.getVariance());
    }

    public synchronized long getCount() {
        return count;
    }

    public synchronized long getSum() {
        return sum;
    }

    public synchronized long getMax() {
        return max;
    }

    public synchronized long getMin() {
        return min;
    }

    public synchronized long getLast() {
        return last;
    }

    public synchronized String getLastFailureReason() {
        return lastFailureReason;
    }

    public synchronized long getFailureCount() {
        return failureCount;
    }

    public synchronized long getResetCount() {
        return resetCount;
    }

    public String toString() {
        return "StatisticsUtils{" +
                "sum=" + sum +
                ", min=" + min +
                ", max=" + max +
                ", last=" + last +
                ", squares=" + squares +
                ", count=" + count +
                ", mean=" + FORMATTER.format(getMean()) +
                ", dev=" + FORMATTER.format(getStdDev()) +
                '}';
    }
}