Results 1 to 2 of 2
  1. #1
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default [SOLVED] Calculating Std. Deviation using Java

    I'm sure this has been posted before, but I'm just having a little tough time with it. The sum and mean calculates correctly but for some reason the standard deviation doesn't calculate correctly, here's my code. Any help is great appreciated =D

    Java Code:
    	private static double stddev(double[] doubleArray) {
    		int i ;
    		double avg = 0 ;
    		double sum = 0 ;
    		double tempSum = 0 ;
    		
    		// sum of the array
    	    for(i = 0; i < doubleArray.length; i++) {
    	        sum = sum + doubleArray[i] ;
    	        System.out.println(sum) ;
    	    }
    	    
    	    // average of the array
    	    avg = sum / (doubleArray.length) ;
    	    System.out.println("Mean = " + avg) ;
    	    
    	    // the length of the array minus 1
    	    tempSum = (doubleArray.length) - 1 ;
    
    	    // (x - average of x's)^2
    	    for(i = 0; i < doubleArray.length; i++) {
    	    sum = sum + (Math.abs(doubleArray[i] - avg) * Math.abs(doubleArray[i] - avg)) ;
    	    }
    	    
    	    // (x - average of x's)^2 divided by length of the array minus 1
    	    sum = sum / tempSum ;
    
    
    	if (i == 0) {
    		return 0.0 ;				
    	}
    	
    	// sqrt of the above
        return Math.sqrt(sum) ;
    }
    
    
    
    public static void main(String[] args) {
    	//***********************************************************
    	// Testing standard deviation
    	System.out.println("Testing variance\n");
    
    	double[] testDoubleArray = { 2.0, -3, 5, 6.0, 0.0 };
    
    	// You should try these test cases too
    	//double[] testDoubleArray = { 2.0, 2.0 };
    	//double[] testDoubleArray = {}
    
    	System.out.println("Test array for standard deviation is: ");
    	for (i=0; i < testDoubleArray.length; i++) {
    		System.out.print(testDoubleArray[i] + " ");
    	}
    	System.out.println("\nStandard deviation is: " + stddev(testDoubleArray));
    
    	System.out.println("\n***************************");
    Just to let you know but I need to have it in this format, but what I'm stuck on is the calculations part. Thank you =D

  2. #2
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

Similar Threads

  1. standard deviation and mean for an array
    By peterdfl in forum New To Java
    Replies: 3
    Last Post: 05-29-2008, 04:09 AM
  2. Calculating per hour statistics
    By vipergt89 in forum New To Java
    Replies: 2
    Last Post: 05-01-2008, 06:25 AM
  3. Calculating trigonometric functions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:56 PM
  4. Calculating hyperbolic functions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:55 PM
  5. Calculating sin of a double value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •