如何使用JAVA计算标准偏差

Noo*_*234 1 java arrays standard-deviation

我在这里很新,目前我正在尝试用Java计算标准偏差(我已经用谷歌搜索了哈哈)但是我在使用它时遇到了很多问题

我有一个由用户inputed十个值,然后我要计算我的理解标准偏差到目前为止感谢谁也说是我找到阵列的平均再完成计算人

                double two = total[2];
                double three = total[3];
                double four = total[3];
                double five = total[4];
                double six = total[6];
                double seven = total[7];
                double eight = total[8];
                double nine = total[9];
                double ten = total[10];

                double eleven = average_total;


mean = one + two + three + four + five + six + seven + eight + nine + ten + eleven;

mean = mean/11;
//one = one - mean;
//System.out.println("I really hope this prints out a value:" +one);
*/
 //eleven = average_total - mean;
 //eleven = Math.pow(average_total,average_total);
 //stand_dev = (one + two + three + four + five + six + seven + eight + nine + ten + eleven);
 //stand_dev = stand_dev - mean;
// stand_dev = (stand_dev - mean) * (stand_dev - mean);
// stand_dev = (stand_dev/11);
// stand_dev = Math.sqrt(stand_dev);
Run Code Online (Sandbox Code Playgroud)

我已经将数据存储在10个值的数组中,但是我不太确定如何从数组中打印数据然后进行计算而不必将输入代码存储在此处数据中我操作过的其他数据

谢谢你的时间,非常感谢:)

Con*_*rew 11

有一个简单的公式可用于在每次添加数字时快速计算标准偏差.下面是一些实现该公式的代码,假设total[]已经声明并已填充:

double powerSum1 = 0;
double powerSum2 = 0;
double stdev = 0;

for i = 0 to total.length {
    powerSum1 += total[i];
    powerSum2 += Math.pow(total[i], 2);
    stdev = Math.sqrt(i*powerSum2 - Math.pow(powerSum1, 2))/i;
    System.out.println(total[i]); // You specified that you needed to print 
                                  // each value of the array
}
System.out.println(stdev); // This could also be placed inside the loop 
                           // for updates with each array value.
Run Code Online (Sandbox Code Playgroud)

这个公式的优点在于,每次添加新值时都不必重新处理整个数组,并且不必存储数组的任何旧值,只需要在上面的代码中声明的三个变量.


pro*_*ard 5

  calculate mean of array.

  loop through values

       array value = (indexed value - mean)^2    

  calculate sum of the new array.

  divide the sum by the array length

  square root it 
Run Code Online (Sandbox Code Playgroud)

编辑:

我将向您展示如何循环遍历数组,并且只需使用不同的计算,所有内容都是相同的步骤.

// calculating mean.

int total = 0;

for(int i = 0; i < array.length; i++){
   total += array[i]; // this is the calculation for summing up all the values
}

double mean = total / array.length;
Run Code Online (Sandbox Code Playgroud)

EDIT2:

在阅读完代码之后,您所做错的部分就是您没有循环读取值并正确地减去它.

又称这一部分.

eleven = average_total - mean;
eleven = Math.pow(average_total,average_total);

你需要这样做.

for(int i = 0; i < array.length; i++){
   array[i] = Math.pow((array[i]-mean),2)
}
Run Code Online (Sandbox Code Playgroud)

基本上你需要用newvalue = oldvalue - mean(average)来改变数组中的每个值.

然后计算总和...然后平方根.