如何使用JAVA计算标准偏差
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18390548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to calculate standard deviation using JAVA
提问by Noobprogrammer1234
I'm very new here, at the moment I am trying to calculate standard deviation with Java (I have googled it haha) but I am having a lot of issues on getting it working
我在这里很新,目前我正在尝试用 Java 计算标准偏差(我已经用谷歌搜索了它哈哈)但是我在让它工作时遇到了很多问题
I have ten values that are inputed by a user which I then have to calculate the standard deviation of my understanding so far thanks to people who have replied is I find the mean of the array then complete the calculations
我有十个由用户输入的值,然后我必须计算到目前为止我理解的标准偏差感谢回答我找到数组的平均值然后完成计算的人
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);
I already have my data that is stored in an array of 10 values but I am not too sure how to print the data out of the array then do the calculations with out having to store the enter code here data some where else that I have manipulated
我已经将我的数据存储在一个包含 10 个值的数组中,但我不太确定如何从数组中打印出数据然后进行计算,而不必将输入代码存储在此处的数据其他一些我已经操作过的地方
Thank you for your time, much appreciated :)
感谢您的时间,非常感谢:)
采纳答案by progrenhard
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
edited:
编辑:
I'll show you how to loop through the array and everything is pretty much this same step just with a different calculation.
我将向您展示如何遍历数组,所有内容几乎都是相同的步骤,只是计算方式不同。
// 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:
编辑2:
After reading your code, the part you are doing wrong is that you are not looping through the values and subtracting it with average correctly.
阅读您的代码后,您做错的部分是您没有遍历这些值并正确地用平均值减去它。
aka this part.
又名这部分。
eleven = average_total - mean;
eleven = Math.pow(average_total,average_total);
十一 =average_total - 平均值;
十一 = Math.pow(average_total,average_total);
you need to do this.
你需要这样做。
for(int i = 0; i < array.length; i++){
array[i] = Math.pow((array[i]-mean),2)
}
essentially you need to change every value in the array with newvalue = oldvalue - mean(average).
本质上,您需要使用 newvalue = oldvalue - mean(average) 更改数组中的每个值。
then calculate the sum... then square root that.
然后计算总和...然后平方根。
回答by Java Devil
What have you tried?...
你试过什么?...
You will need to loop the values for example to print all the values
您将需要循环值,例如打印所有值
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
}
回答by Kon
I won't solve your problem for you since this looks like howework, but I'll try to help you out a little by giving you some pseudocode to point you in the right direction:
我不会为你解决你的问题,因为这看起来像 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)
Now you need to find the distances of the elements from the mean. Easy
现在您需要找到元素与平均值的距离。简单
Loop over array i = 1 to 10
Replace each element with its distance from Average Squared
Add to some variable differenceTotal
End of Loop
Then you have your numerator and denominator terms, and your solution should be obvious. Hopefully that was somewhat clear and helpful.
然后你有你的分子和分母项,你的解决方案应该是显而易见的。希望这有点清楚和有帮助。
回答by Constablebrew
There is a simple formula that can be used to quickly calculate standard deviation every time a number is added. Here is some code that implements that formula, assuming total[]
has been declared and populated already:
有一个简单的公式可用于在每次添加数字时快速计算标准偏差。下面是一些实现该公式的代码,假设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.
The beauty of this formula is that you don't have to reprocess the entire array each time you add a new value and you don't have to store any of the old values of the array, just the three variables declared in the code above.
这个公式的美妙之处在于每次添加新值时不必重新处理整个数组,也不必存储数组的任何旧值,只需存储上面代码中声明的三个变量.
回答by ImGeorge
Try this
尝试这个
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<values.length; i++){
double fvalue = (Math.pow((values[i] - average), 2));
x1_average[i]= fvalue;
System.out.println("test: "+ fvalue);
}
for(double i : x1_average) {
finalsumX = (sumX += i);
}
Double AverageX = finalsumX/(values.length);
System.out.println("E(X1-x1_average)^2/AverageX: "+ AverageX);
double SquareRoot = Math.sqrt(AverageX);
System.out.println("Standard Deviation: "+ SquareRoot);
}
}
You can tweak to how u want like adding user input.The code is rough becose I assume this is a homework. try to make it nice in your own way. hope it helps you.
你可以调整你想要添加用户输入的方式。代码很粗糙,因为我认为这是一个家庭作业。尝试以你自己的方式让它变得美好。希望对你有帮助。