如何使用JAVA计算标准差

我在这里很新,目前我正在尝试用Java计算标准偏差(我已经用谷歌搜索了哈哈)但是我有很多关于它的工作问题

我有十个值由用户输入,然后我必须计算我理解的标准偏差到目前为止,感谢回复的人是我找到数组的平均值然后完成计算

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); 

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

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

  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 

编辑:

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

 // 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; 

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) } 

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

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

有一个简单的公式可以用来在每次添加数字时快速计算标准偏差。 下面是一些实现该公式的代码,假设已经声明并填充了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. 

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

我不会为你解决你的问题,因为这看起来像howework,但我会尝试通过给你一些伪代码来帮助你指出正确的方向:

 Loop over array i=1 to 10 Add each element to some other variable Total End of Loop Average = Total / 10 (required for your std. dev. equation) 

现在你需要从平均值中找出元素的距离。 简单

 Loop over array i = 1 to 10 Replace each element with its distance from Average Squared Add to some variable differenceTotal End of Loop 

然后你有你的分子和分母术语,你的解决方案应该是显而易见的。 希望这有点清楚,有帮助。

试试这个

 import java.io.IOException; public class StandardDeviationCalc { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { double [] values = {9, 2, 5, 4, 12, 7, 8, 11, 9, 3, 7, 4, 12, 5, 4, 10, 9, 6, 9, 4 }; //change input values here double sum=0; double finalsum = 0; double average = 0; for( double i : values) { finalsum = (sum += i); } average = finalsum/(values.length); System.out.println("Average: "+ average); double sumX=0; double finalsumX=0; double[] x1_average = new double[2000]; for (int i = 0; i 

你可以调整你想要添加用户输入的方式。代码很粗糙,因为我认为这是一个功课。 尽量以自己的方式让它变得美好。 希望它能帮助你。

你试过什么?…

您需要循环值,例如打印所有值

 for(int i = 0; i < yourArray.length; i++) { System.out.println(yourArray[i]); // Add your code to calculate the values you need for standard dev }