You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by md...@apache.org on 2003/06/21 04:08:23 UTC
cvs commit: jakarta-commons-sandbox/math/src/test/org/apache/commons/math/stat UnivariateImplTest.java
mdiggory 2003/06/20 19:08:23
Modified: math/src/java/org/apache/commons/math/stat
UnivariateImpl.java
math/src/test/org/apache/commons/math/stat
UnivariateImplTest.java
Log:
Adding tests for kurtosis and skew to UnivariateImpl testcase. Correcting initialization of moments in UnivariateImpl.
Revision Changes Path
1.12 +30 -23 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/UnivariateImpl.java
Index: UnivariateImpl.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/UnivariateImpl.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- UnivariateImpl.java 18 Jun 2003 13:57:24 -0000 1.11
+++ UnivariateImpl.java 21 Jun 2003 02:08:23 -0000 1.12
@@ -106,13 +106,13 @@
private double mean = Double.NaN;
/** second moment of values that have been added */
- private double s2 = Double.NaN;
+ private double m2 = Double.NaN;
/** third moment of values that have been added */
- private double s3 = Double.NaN;
+ private double m3 = Double.NaN;
/** fourth moment of values that have been added */
- private double s4 = Double.NaN;
+ private double m4 = Double.NaN;
/** variance of values that have been added */
private double variance = Double.NaN;
@@ -307,9 +307,9 @@
sumLog = 0.0;
sum = min = max = mean = value;
sumsq = Math.pow(value, 2);
- variance = s2 = 0.0;
+ variance = m2 = 0.0;
skewness = kurtosis = 0.0;
-
+ m2 = m3 = m4 = 0.0;
} else {
/* otherwise calc these values */
sumLog += Math.log(value);
@@ -321,29 +321,36 @@
double dev = value - mean;
double v = dev / ((double) n);
double v2 = Math.pow(v, 2);
- double n1 = ((double) n - 1);
- s4 += v
- * (
- - 4.0 * s3
- + v
- * (6.0 * s2
- + n1 * (1 + Math.pow((double) n, 3)) * v2));
-
- s3 += v
- * (-3.0 * s2 + (double) n * n1 * (n - 2) * Math.pow(v, 2));
- s2 += n1 * dev * v;
+ double n0 = (double) n;
+ double n1 = (double) (n - 1);
+ double n2 = (double) (n - 2);
+ double n3 = (double) (n - 3);
+
+ m4 =
+ m4
+ - (4.0 * v * m3)
+ + (6.0 * v2 * m2)
+ + ((n0 * n0) - 3 * n1) * (v2 * v2 * n1 * n0);
+
+ m3 = m3 - (3.0 * v * m2) + (n0 * n1 * n2 * v2 * v);
+
+ m2 += n1 * dev * v;
mean += v;
- variance = (n <= 1) ? 0.0 : s2 / n1;
+
+ variance = (n <= 1) ? 0.0 : m2 / n1;
+
skewness =
- (n <= 2)
+ (n <= 2 || variance < 10E-20)
? 0.0
- : s3 / ((double) n * Math.sqrt(variance) * variance);
+ : (n0 * m3) / (n1 * n2 * Math.sqrt(variance) * variance);
+
kurtosis =
- (n <= 3)
+ (n <= 3 || variance < 10E-20)
? 0.0
- : s4 / ((double) n * Math.pow(variance, 2)) - 3;
+ : (n0 * (n0 + 1) * m4 - 3 * m2 * m2 * n1)
+ / (n1 * n2 * n3 * variance * variance);
}
}
}
@@ -375,7 +382,7 @@
this.min = this.max = Double.NaN;
this.sumLog = this.mean = Double.NaN;
this.variance = this.skewness = this.kurtosis = Double.NaN;
- this.s2 = this.s3 = this.s4 = Double.NaN;
+ this.m2 = this.m3 = this.m4 = Double.NaN;
if (doubleArray != null)
doubleArray = new FixedDoubleArray(windowSize);
}
1.2 +16 -1 jakarta-commons-sandbox/math/src/test/org/apache/commons/math/stat/UnivariateImplTest.java
Index: UnivariateImplTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/test/org/apache/commons/math/stat/UnivariateImplTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- UnivariateImplTest.java 29 May 2003 20:35:46 -0000 1.1
+++ UnivariateImplTest.java 21 Jun 2003 02:08:23 -0000 1.2
@@ -235,4 +235,19 @@
//FiXME: test all other NaN contract specs
}
+ public void testSkewAndKurtosis() {
+ Univariate u = new UnivariateImpl();
+
+ double[] testArray =
+ { 12.5, 12, 11.8, 14.2, 14.9, 14.5, 21, 8.2, 10.3, 11.3, 14.1,
+ 9.9, 12.2, 12, 12.1, 11, 19.8, 11, 10, 8.8, 9, 12.3 };
+ for( int i = 0; i < testArray.length; i++) {
+ u.addValue( testArray[i]);
+ }
+
+ assertEquals("mean", 12.40455, u.getMean(), 0.0001);
+ assertEquals("variance", 10.00236, u.getVariance(), 0.0001);
+ assertEquals("skewness", 1.437424, u.getSkewness(), 0.0001);
+ assertEquals("kurtosis", 2.37719, u.getKurtosis(), 0.0001);
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org