Need help with standard deviation

I cant get the math right

Page 1 of 1

7 Replies - 1521 Views - Last Post: 03 May 2007 - 07:39 PM Rate Topic: -----

#1 00Joe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-May 07

Need help with standard deviation

Posted 02 May 2007 - 09:42 AM

I wrote a smaller c++ program to test out the math for my standard deviation function for a much larger program.

Basically, you input a bunch of numbers, and end it with a negative number. It should output the standard deviation of that set of numbers. The problem is, that I keep getting I.#INF . What is that, and how do I fix my code to get the math right?

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
void main ()
{
	int pos, cnt;
	float avg, data[50], tot, num;

	cout.setf(ios::fixed);
	cout.precision(1);

	pos=0;
	cnt=0;
	cout << "Num? ";
	cin >> num;
	while (num >=0)
	{
		pos++;
		cnt++;
		data[pos]=num;
		cout << "Num? ";
		cin >> num;
	}

	tot=0;
	for (pos=0; pos<cnt-1; pos++)
		tot+=(data[pos+1]-data[pos])*(data[pos+1]-data[pos]);

	avg=tot/cnt;
	cout << "The standard deviation is " << sqrt(avg) << endl; 


once I get this right, I'll integrate this code into my larger program.

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with standard deviation

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Need help with standard deviation

Posted 02 May 2007 - 09:53 AM

What platform/compiler are you using...that code runs fine for me, in that it outputs a number...I've not checked to see if it's actually the standard dev or not.
Was This Post Helpful? 0
  • +
  • -

#3 00Joe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-May 07

Re: Need help with standard deviation

Posted 02 May 2007 - 09:55 AM

View PostAmadeus, on 2 May, 2007 - 09:53 AM, said:

What platform/compiler are you using...that code runs fine for me, in that it outputs a number...I've not checked to see if it's actually the standard dev or not.


I'm running this on Windows xp, if that's what you were asking... I also typed it up in visual c++ 2005 express edition
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Need help with standard deviation

Posted 02 May 2007 - 10:18 AM

Hmm...well, it really runs for me...can you give an example of the input you enter, then the exact line displayed in the output?
Was This Post Helpful? 0
  • +
  • -

#5 00Joe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-May 07

Re: Need help with standard deviation

Posted 02 May 2007 - 10:25 AM

View PostAmadeus, on 2 May, 2007 - 10:18 AM, said:

Hmm...well, it really runs for me...can you give an example of the input you enter, then the exact line displayed in the output?


sure:

I punch in 95, 80, 82.5, 95, 70, 75, 73, 89, 66, 62, 78, 51, -1
I should get 13.2. Instead, I get 100000(20 lines later)0000.$
I think that I got the old answer before I changed something, but that's still the same code for this answer.
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Need help with standard deviation

Posted 02 May 2007 - 10:29 AM

Hmm....I got 30.4, but got an answer.
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Need help with standard deviation

Posted 03 May 2007 - 04:09 PM

My first observation is that this is not the std deviation.
Posted Image

would be the ussual formula. To calculate this you would first find the average -- this is generally the first step in any version of the standard deviation.

So step 1 find the average.
Step 2 find the average of the square of distance from the average.
Step 3 Take the sqrt of the "average of the square of the distance from the average"

Having said that why does Amadeus get a small answer and you get an ugly one... Well my bet would be your compiler or platform. There is a logic error in your program that gets you to use some uninitialized data. This confused me at first because what I calculated by hand and what I was looking at were not the same... translation -- either C++ has forgotten how to preform arithmatic, I had forgotten how to read C++, or we were not using the same numbers.

When you collect the data you don't use data[0] becuase you places your pos++ before you assigned the data. But later you use pos=0 as the begining of your for loop. Change that loop to start with pos=1 and your program will calculate the "standard (unsigned) differential" -- not the standard deviation.
Was This Post Helpful? 0
  • +
  • -

#8 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,079
  • Joined: 01-December 05

Re: Need help with standard deviation

Posted 03 May 2007 - 07:39 PM

This might not be relevent, but do you want the sample standard deviation, or the population standard deviation?
Population: Ask every single eligible person in the USA who they will vote for in the 08 election
Sample: ask a few thousand people who they will vote for instead.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1