Question

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

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

Thank you for your time, much appreciated :)

Was it helpful?

Solution 2

  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:

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

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

then calculate the sum... then square root that.

OTHER TIPS

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:

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.

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:

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.

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.

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top