Added a method for computing the standard deviation of a set of values.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3201 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2004-11-04 04:52:31 +00:00
parent a7a04da6df
commit 0c5f723046
@@ -1,5 +1,5 @@
// //
// $Id: MathUtil.java,v 1.11 2004/08/27 02:12:47 mdb Exp $ // $Id: MathUtil.java,v 1.12 2004/11/04 04:52:31 mdb Exp $
// //
// Narya library - tools for developing networked games // Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved // Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
@@ -109,4 +109,31 @@ public class MathUtil
{ {
return ((dividend >= 0) ? dividend : (dividend - divisor + 1))/divisor; return ((dividend >= 0) ? dividend : (dividend - divisor + 1))/divisor;
} }
/**
* Computes the standard deviation of the supplied values.
*
* @return an array of three values: the mean, variance and standard
* deviation, in that order.
*/
public static float[] stddev (int[] values, int start, int length)
{
// first we need the mean
float mean = 0f;
for (int ii = start, end = start + length; ii < end; ii++) {
mean += values[ii];
}
mean /= length;
// next we compute the variance
float variance = 0f;
for (int ii = start, end = start + length; ii < end; ii++) {
float value = values[ii] - mean;
variance += value * value;
}
variance /= (length - 1);
// the standard deviation is the square root of the variance
return new float[] { mean, variance, (float)Math.sqrt(variance) };
}
} }