Help with Stats Method
As I guess is standard in today's undergrad education, I've got to code a stats class with various statistical methods. I have them all under control except the one for standard deviation. We're supposed to use a specific formula, the one that take the square root of everything. I don't know how to put it in here like my teacher has it on her website, so hopefully this makes sense:
Sqrt [ (E (x - x) ^2) / n]
where E is the summation symbol, x is an array value, x is the mean and n is the number of values in the array.
I have tried to write this myself so don't think I'm wanting somebody out there to write the whole thing for me. I did however just get frustrated and deleted everything I had. So my code currently looks like this:
float StandardDeviation(int array[],int n)
{
}
I have the mean method coded already, if that helps. I tried to bring it down and use it and it didn't work. Any help at all would be so appreciated!!
[1145 byte] By [
jane507a] at [2007-11-26 23:40:23]

I don't know maths much (I wikipedia'd standard devation, so don't hurt me if it's wrong).
You're method doesn't need to take the array and the length of the array, just the array then you can call .length to find n.
public class StandardDeviation {
public static void main(String[] args) {
int array[] = {1,2,3,4};
System.out.println(standardDeviation(array));
}
private static float standardDeviation( int array[]) {
float sumTerms = 0;
for (int i = 0; i < array.length; i++) {
sumTerms += Math.pow((array[i] - mean(array)), 2);
}
return new Float(Math.sqrt(sumTerms/array.length));
}
private static float mean(int array[]) {
float total = 0;
for (int i = 0; i < array.length; i++) {
total += array[i];
}
return total/array.length;
}
}
Get the answer and walk away, you learn nothing.You know the rule of mean, standard deviation, variance, summation, etc.You would not find it hard to make it except finding the correct API you need.
rym82a at 2007-7-11 15:06:55 >

Just curious to know if you were able to get the correct code for your program on the standard deviation? Would like to see how you ended up coding it. I am new to this and still learning!
See my reply, I think it works.
The following article is the answer I gave to jane507 in an entirely
different forum. I guess that she just wanted to be spoonfed instead.
Here's my answer again:
Remember those old calculators? Some of them could even do some
stats functions (such as the standard deviation). You could find those
numbers for thousands of samples but you can bet on it that those old
calculators didn't have many kilo/mega-bytes of ram avaialble to store
those big arrays.
This is how they did it:
the square of the standard deviation is defined as:
sum(((x-m)^2)/n) where m is the average or mean of all values x
and n is the number of samples x. (identical to your formula).
the value m itself is defined as sum(x)/n
Let's fiddle a bit with that formula:
sum(((x-m)^2)/n) --> sum((x^2-2.x.m+m^2)/n) -->
sum(x^2/n)-sum(2.x.m/n)+sum(m^2/n) -->
sum(x^2/n)-m.sum(2.x)/n+sum(m^2/n) -->
sum(x^2/n)-2.m.m+m.m --> sum(x^2/n)-m.m -->
sum(x^2)/n-(sum(x)/n)^2
The latest form requires just sum(x^2) and the mean of all those x's
squared. Suppose you have three variables:
double sx; // sum of x's
double sxx; // sum of squares of x's
int n; // number of sampes
you can add a new x value as follows:
sx+= x;
sxx+= x*x;
n++;
And you can find the square of the standard deviation as follows:
double ssd= ssx/n-(sx/n)*(sx/n);
All you need are those three variables sx, sxx and n, keep on adding
new x values and you can have the ssd at any time you want.
kind regards,
Jos
JosAHa at 2007-7-11 15:06:55 >

How could you find the median of this code given an array of numbers? I have seen several code examples that finds the median for odd and even numbers.
// returns the median value for the array "ar"
float Median(int ar[ ], int n)
:
:
:
return (ar [n/2] + ar[n/2 - 1] /2)
Or is this completely off for finding the even and odd numbers for median. Any help would much appreciated.
If you have odd numbers in the array, do you still do the addition and division ?n is the number of elements in array ? You can use ar.length
rym82a at 2007-7-11 15:06:55 >

Thanks for the reply. Okay without the addition and subtraction and being concerned with whether the array is even or odd. Would this work? If not, what am I missing? Thanks. float Median (int ar[ ], n)::return ar.length/2
> Thanks for the reply. Okay without the addition and
> subtraction and being concerned with whether the
> array is even or odd. Would this work? If not, what
> am I missing? Thanks.
>
> float Median (int ar[ ], n)
> :
> :
> return ar.length/2
You were right but the design was not very good.
But now you are wrong.
Why the median is the number of elements divided by 2 ?
rym82a at 2007-7-11 15:06:55 >

I am new at this java programming and trying to find the correct code for finding the Median. I find different code examples for calculting the Median. I am not sure as to the correct way to code it. I am not sure what I need to do to be able to find the Median for even and odd numbers. I am just confused!!!! If I was on the right track before, what do I need to do to clean it up? See below:
// returns the median value for the array "ar"
float Median(int ar[ ], int n)
:
:
:
return (ar [ar.lentgth/2] + ar[ar.length/2 - 1] /2)
here is the pseudocode
if total numbers is odd
just return the middle of elements
else
return (ar [ar.lentgth/2] + ar[ar.length/2 - 1] /2)
because you have used ar.length, you don't need parameter n
rym82a at 2007-7-11 15:06:55 >

Thanks a zillion! I figured it out after I posted and then logged off. It 's just a matter of putting it all together. I think I have it now. Hope it will work. Will keep you posted on the outcome. Oh for my clarification, so the odd number of array elements calculation would be
ar[ar.length/2], which means to take the length of the array and divide by two (or half) to find the middle of elements. I think the light bulb has gone off..I hope! Thanks again.