mdiggory 2003/06/27 13:31:52
Modified: math/src/java/org/apache/commons/math/stat StatUtils.java
Log:
Eliminated extra pass to calc mean in std calculation.
Revision Changes Path
1.10 +38 -13
jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/StatUtils.java
Index: StatUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/StatUtils.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- StatUtils.java 24 Jun 2003 14:03:32 -0000 1.9
+++ StatUtils.java 27 Jun 2003 20:31:52 -0000 1.10
@@ -309,28 +309,40 @@
* @return the skewness of the values or Double.NaN if the array is empty
*/
public static double skewness(double[] values, int begin, int length) {
-
+
testInput(values, begin, length);
-
+
// Initialize the skewness
double skewness = Double.NaN;
// Get the mean and the standard deviation
double mean = mean(values, begin, length);
- double stdDev = standardDeviation(values, begin, length);
- // Sum the cubes of the distance from the mean divided by the
- // standard deviation
+ // Calc the std, this is implemented here instead of using the
+ // standardDeviation method eliminate a duplicate pass to get the mean
double accum = 0.0;
+ double accum2 = 0.0;
for (int i = begin; i < begin + length; i++) {
- accum += Math.pow((values[i] - mean) / stdDev, 3.0);
+ accum += Math.pow((values[i] - mean), 2.0);
+ accum2 += (values[i] - mean);
+ }
+ double stdDev =
+ Math.sqrt(
+ (accum - (Math.pow(accum2, 2) / ((double) length)))
+ / (double) (length - 1));
+
+ // Calculate the skew as the sum the cubes of the distance
+ // from the mean divided by the standard deviation.
+ double accum3 = 0.0;
+ for (int i = begin; i < begin + length; i++) {
+ accum3 += Math.pow((values[i] - mean) / stdDev, 3.0);
}
// Get N
double n = length;
// Calculate skewness
- skewness = (n / ((n - 1) * (n - 2))) * accum;
+ skewness = (n / ((n - 1) * (n - 2))) * accum3;
return skewness;
}
@@ -354,21 +366,33 @@
* @return the kurtosis of the values or Double.NaN if the array is empty
*/
public static double kurtosis(double[] values, int begin, int length) {
-
testInput(values, begin, length);
-
+
// Initialize the kurtosis
double kurtosis = Double.NaN;
// Get the mean and the standard deviation
double mean = mean(values, begin, length);
- double stdDev = standardDeviation(values, begin, length);
+
+ // Calc the std, this is implemented here instead of using the
+ // standardDeviation method eliminate a duplicate pass to get the mean
+ double accum = 0.0;
+ double accum2 = 0.0;
+ for (int i = begin; i < begin + length; i++) {
+ accum += Math.pow((values[i] - mean), 2.0);
+ accum2 += (values[i] - mean);
+ }
+
+ double stdDev =
+ Math.sqrt(
+ (accum - (Math.pow(accum2, 2) / ((double) length)))
+ / (double) (length - 1));
// Sum the ^4 of the distance from the mean divided by the
// standard deviation
- double accum = 0.0;
+ double accum3 = 0.0;
for (int i = begin; i < begin + length; i++) {
- accum += Math.pow((values[i] - mean) / stdDev, 4.0);
+ accum3 += Math.pow((values[i] - mean) / stdDev, 4.0);
}
// Get N
@@ -376,8 +400,9 @@
double coefficientOne = (n * (n + 1)) / ((n - 1) * (n - 2) * (n - 3));
double termTwo = ((3 * Math.pow(n - 1, 2.0)) / ((n - 2) * (n - 3)));
+
// Calculate kurtosis
- kurtosis = (coefficientOne * accum) - termTwo;
+ kurtosis = (coefficientOne * accum3) - termTwo;
return kurtosis;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]