1 Replies - 210 Views - Last Post: 15 November 2012 - 05:27 PM Rate Topic: -----

#1 kevinja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 29-September 10

calculating the standard deviation from a set of numbers in an array

Posted 15 November 2012 - 02:01 PM

Hello,

My program calculates/determines and displays various statistical data from a set of numbers stored in an array. One of the methods I need to implement is one that will calculate the standard deviation. I'm having a real heck of a time trying to get this to work correctly, because I am consistently getting incorrect results even after changing some of the code and looking at examples elsewhere on the Web. Could anyone please give me some suggestions/pointers? I have a method called mean as well, if that helps. My code for the stddev (standard deviation) is below:

public static double stddev(double[] nums)
	{
		double standdev=0;
		double x=0;
		double x2=0;
		for(int i=0; i<nums.length; i++)
		{
			x+=nums[i];
			x2+=Math.pow(nums[i], 2);
			}
		standdev=(Math.sqrt(x2-((x)*(x)/nums.length))/(nums.length-1));
		return standdev;
		}



Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: calculating the standard deviation from a set of numbers in an array

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,672
  • Joined: 20-September 08

Re: calculating the standard deviation from a set of numbers in an array

Posted 15 November 2012 - 05:27 PM

No - you need to subtract the mean for each number (i.e. in the loop)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1