【问题标题】:Finding the standard deviation from a list of numbers (user input)从数字列表中查找标准差(用户输入)
【发布时间】:2015-02-06 15:17:02
【问题描述】:

我尝试寻找一个示例,说明如何从用户输入的数量列表中找到标准偏差。我想知道是否有人可以解释如何从扫描仪中找到数字列表的标准偏差。任何建议都会很棒。

-提前致谢

【问题讨论】:

  • 您是否在从用户输入中获取数字或从数字列表中获取标准偏差时遇到问题?这是两个不同的问题,需要在这里分开。
  • 如果您要保持运行计算(即每次扫描新数字时更新) - 请查看标准偏差的指数加权移动平均 (EWMA) 公式,因为这些公式通常有一个形式更容易“在线”更新。
  • 我想知道如何从用户输入中获取它。我希望能解释一下程序是如何工作的,而不仅仅是代码。

标签: arrays math java.util.scanner standard-deviation


【解决方案1】:

当然——这样就可以了。

package statistics;

/**
 * Statistics
 * @author Michael
 * @link http://stackoverflow.com/questions/11978667/online-algorithm-for-calculating-standrd-deviation/11978689#11978689
 * @link http://mathworld.wolfram.com/Variance.html
 * @since 8/15/12 7:34 PM
 */
public class Statistics {

    private int n;
    private double sum;
    private double sumsq;

    public void reset() {
        this.n = 0;
        this.sum = 0.0;
        this.sumsq = 0.0;
    }

    public synchronized void addValue(double x) {
        ++this.n;
        this.sum += x;
        this.sumsq += x*x;
    }

    public synchronized double calculateMean() {
        double mean = 0.0;
        if (this.n > 0) {
            mean = this.sum/this.n;
        }
        return mean;
    }

    public synchronized double calculateVariance() {
        double variance = 0.0;
        if (this.n > 0) {
            variance = Math.sqrt(this.sumsq-this.sum*this.sum/this.n)/this.n;
        }
        return variance;
    }

    public synchronized double calculateStandardDeviation() {
        double deviation = 0.0;
        if (this.n > 1) {
            deviation = Math.sqrt((this.sumsq-this.sum*this.sum/this.n)/(this.n-1));
        }
        return deviation;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 2016-07-24
    • 2011-09-05
    • 1970-01-01
    • 2018-04-28
    • 2021-06-26
    • 2015-07-22
    相关资源
    最近更新 更多