Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)

 Jeremy 12-11-2005 07:06 PM

hello,

I am trying to calculate the standard deviation with my program, but I
ran into a small problem with squaring negative value's. Which cannot
be done.. Ehm let me explain let's say;

score=8 mean=9.67 and deviation=-1.67

now I want to square the deviation with sqrt but that doesn't really
work anymore since the value is negative, what function could I use
from the math.h to obtain the correct squared deviation value of 2.79?

Thanks you.

Jeremy

 osmium 12-11-2005 07:22 PM

"Jeremy" writes:

> I am trying to calculate the standard deviation with my program, but I
> ran into a small problem with squaring negative value's. Which cannot
> be done.. Ehm let me explain let's say;
>
> score=8 mean=9.67 and deviation=-1.67

A score is a point sample. It makes no sense to try to compute the std
deviation of a single sample.

> now I want to square the deviation with sqrt but that doesn't really
> work anymore since the value is negative, what function could I use
> from the math.h to obtain the correct squared deviation value of 2.79?

It is perfectly permissible to square a negative number, square roots are a
different matter. Note that the square of a negative number is always
positive. The equation for standard deviation will always result in taking
the square root of a *positive* number. Look more closely at the equation
you are using.

 A. Sinan Unur 12-11-2005 07:31 PM

Jeremy <Jeremy@sirkaukie.tk> wrote in
news:fhtop113555i3gnhk6u73m8p57pnlrdnsa@4ax.com:

> I am trying to calculate the standard deviation with my program, but I
> ran into a small problem with squaring negative value's. Which cannot
> be done..

Huh?

> Ehm let me explain

> score=8 mean=9.67 and deviation=-1.67
>
> now I want to square the deviation with sqrt

sqrt is for taking the square root of a number.

> but that doesn't really
> work anymore since the value is negative, what function could I use
> from the math.h to obtain the correct squared deviation value of 2.79?

You seem to be confused between "standard deviation" and z-score.

Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

 Markus Moll 12-11-2005 07:33 PM

Hi

Jeremy wrote:

> score=8 mean=9.67 and deviation=-1.67
>
> now I want to square the deviation with sqrt but that doesn't really
> work anymore since the value is negative, what function could I use
> from the math.h to obtain the correct squared deviation value of 2.79?

Ever thought about why there is a 't' at the end of 'sqrt'? ;-)

Firstly, x squared simply is x * x.
Secondly, if that is statistics, then you're probably doing something wrong.
The standard deviation (if that's what you mean by deviation) is defined to
be the square root of the variance. An unbiased estimator for the variance
is given by
\frac{1}{n-1} \sum_{i=1}^N E((x_i - Âµ)^2)

Markus

 12-11-2005 07:37 PM

Jeremy (Jeremy@sirkaukie.tk) wrote:
: hello,
:
: I am trying to calculate the standard deviation with my program, but I
: ran into a small problem with squaring negative value's. Which cannot
: be done.. Ehm let me explain let's say;

You can compute the square of a negativ value, and you can compute
the square root of a negative value, but the square root of a
negative value can not be represented in a double. Since C
does not provide a mechanism for a functoin to return either
a "double" or a "double _Complex" depending on the sign of the argument,
an implementation of "double sqrt(double);" is limited to
arguments that are not negative.

If you really need a function that computes square roots of
negative numbers, have a look at "double complex csqrt(double complex)"
from <complex.h>. If your implementation has signed zero representations,
you might want to think about the sign of the imarinary 0 you
use to expand your negative double to an equivaent double complex.

:
: score=8 mean=9.67 and deviation=-1.67
:
: now I want to square the deviation with sqrt but that doesn't really
: work anymore since the value is negative, what function could I use
: from the math.h to obtain the correct squared deviation value of 2.79?

"deviaton * deviation" might be the best solution for you, even if
"pow(deviation, 2.0)" should work as well. The function sqrt()
does not do what you seem to think it should do.

Kurt Watzka

 Jeremy 12-11-2005 08:00 PM

On Sun, 11 Dec 2005 20:33:37 +0100, Markus Moll

> Hi
>
> Jeremy wrote:
>
> > score=8 mean=9.67 and deviation=-1.67
> >
> > now I want to square the deviation with sqrt but that doesn't really
> > work anymore since the value is negative, what function could I use
> > from the math.h to obtain the correct squared deviation value of 2.79?

>
> Ever thought about why there is a 't' at the end of 'sqrt'? ;-)
>
> Firstly, x squared simply is x * x.
> Secondly, if that is statistics, then you're probably doing something wrong.
> The standard deviation (if that's what you mean by deviation) is defined to
> be the square root of the variance. An unbiased estimator for the variance
> is given by
> \frac{1}{n-1} \sum_{i=1}^N E((x_i - µ)^2)
>
> Markus

I think this is the language barriere kicking in with a lack of
education and a beginner hobby programmer....

I fixed the differences or mixup inbetween square and sqrt and it's
working ! Halleluja..

Thanks you.

 Gordon Burditt 12-11-2005 08:08 PM

>I am trying to calculate the standard deviation with my program, but I
>ran into a small problem with squaring negative value's.

Make up your mind what you want to do: SQUARE a value or TAKE THE
SQUARE ROOT of a value. They are different, in both mathematics
and C.

> Which cannot
>be done.. Ehm let me explain let's say;
>
>score=8 mean=9.67 and deviation=-1.67
>
>now I want to square the deviation with sqrt but that doesn't really

Do not attempt to square anything with sqrt(). It's like
constructing a skyscraper with explosives.

>work anymore since the value is negative, what function could I use
>from the math.h to obtain the correct squared deviation value of 2.79?

If the correct answer is 2.79, apparently you are trying to SQUARE
a value, and sqrt() is not the way to do it.

Gordon L. Burditt

 Martin Ambuhl 12-11-2005 08:28 PM

Jeremy wrote:
> hello,
>
> I am trying to calculate the standard deviation with my program, but I
> ran into a small problem with squaring negative value's. Which cannot
> be done..

Of course one can square negative values; it's taking their square roots
in that's a problem in the domain of real numbers.

> Ehm let me explain let's say;
>
> score=8 mean=9.67 and deviation=-1.67

This never happens. A sample variance is
V = sum(x*x)/n - (sum(x)/n)*(sum(x)/n)
and the estimate of the population variance
is V' = V * n /(n-1)

Those numbers for n > 1 and each x real are both always positive, and
the corresponding standard deviations are the positive square roots of
those variances. Neither the variance nor the standard deviations are
ever negative.

>
> now I want to square the deviation with sqrt

sqrt(x) yields the square root of x, not the square of x.
To square x, just multiply it by itself. If you tire of writing x*x
(where x is long or complicated) you can use a function or macro:

inline double sqr(double x) { return x*x; }

- or -

#define SQR(x) ((x)*(x))

> but that doesn't really
> work anymore since the value is negative,

You have made an error: the standard deviation is never negative for
measurements with real values.

> what function could I use
> from the math.h to obtain the correct squared deviation value of 2.79?

See above.

#include <stdio.h>
#include <float.h>

int main(void)
{
double deviation = 1.67, variance;
variance = deviation * deviation;
printf("[Output:]\n"
"If the standard deviation is %.*g,\n"
"then the variance is %.*g.\n",
DBL_DIG, deviation, DBL_DIG, variance);
return 0;
}

[Output:]
If the standard deviation is 1.67,
then the variance is 2.7889.

 All times are GMT. The time now is 12:50 PM.