How to calculate the standard divination of an array in Java? As you can see I have already calculated the mean, and I know that at the end I will have to divide by the sample size minus 1 (n-1) and square that number. The problem I'm having is how to take every number and calculate how far it is away from the mean, then square that number. I know I could do every number from the data set separately but there has to be an easier way. Any help would be appreciated, here's my code.
public class CalculateArray
{
public static void main(String[] args)
{
int [] numbers = new int[]{1,2,3,4,5,6,7,8,9};
int sum = 0;
int max = 0;
int min = numbers[0];
double sd = 0;
for(int i=0; i<numbers.length; i++)
{
sum = sum + numbers[i];
}
double average = sum / numbers.length;
System.out.println("Average value is : " + average);
for(int i=0; i<numbers.length; i++)
{
if(numbers[i] > max)
{
max = numbers[i];
}
}
System.out.println("max number is : " + max);
for(int i=0; i<numbers.length; i++)
{
if(numbers[i] < min)
{
min = numbers[i];
}
}
System.out.println("min number is : " + min);
for (int i=0; i<numbers.length;i++)
{
//this is where im having problems
sd = ???
}
double standardDeviation = math.sqrt(sd/(numbers.length-1));
System.out.println("The standard deviation is : " + standardDeviation);
}
}
To calculate how far a number is from the mean you use the -
operator. For calculating the square you can use Math.pow
. So, given that you have already calculated the average
earlier in the program:
for (int i=0; i<numbers.length;i++)
{
sd = sd + Math.pow(numbers[i] - average, 2);
}
By the way, the way you calculate the mean currently is broken. You should define sum
as double, not as int
.
In addition to the two pass algorithm described by others (calculate mean in the first pass and std dev in the next) please see this link for an example of how it can be done in a single pass. The algorithm is as follows:
double std_dev2(double a[], int n) {
if(n == 0)
return 0.0;
double sum = 0;
double sq_sum = 0;
for(int i = 0; i < n; ++i) {
sum += a[i];
sq_sum += a[i] * a[i];
}
double mean = sum / n;
double variance = sq_sum / n - mean * mean;
return sqrt(variance);
}
UPDATE
Don't do this. As Joni explains in his comment below there is a high risk of error when implementing this is a computer program. For a stable online algorithm, Joni directs us to this Wikipedia article, which as mentioned has been thoroughly analyzed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With