Adding tests for kurtosis and skew to UnivariateImpl testcase. Correcting initialization of moments in UnivariateImpl.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140933 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark R. Diggory 2003-06-21 02:08:23 +00:00
parent e3ea7f7638
commit 7e2a7b5027
2 changed files with 45 additions and 23 deletions

View File

@ -71,7 +71,7 @@ import org.apache.commons.math.FixedDoubleArray;
* @author <a href="mailto:mdiggory@apache.org">Mark Diggory</a>
* @author Brent Worden
* @author <a href="mailto:HotFusionMan@Yahoo.com">Albert Davidson Chou</a>
* @version $Revision: 1.11 $ $Date: 2003/06/18 13:57:24 $
* @version $Revision: 1.12 $ $Date: 2003/06/21 02:08:23 $
*
*/
public class UnivariateImpl implements Univariate, Serializable {
@ -106,13 +106,13 @@ public class UnivariateImpl implements Univariate, Serializable {
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 @@ public class UnivariateImpl implements Univariate, Serializable {
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 @@ public class UnivariateImpl implements Univariate, Serializable {
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));
double n0 = (double) n;
double n1 = (double) (n - 1);
double n2 = (double) (n - 2);
double n3 = (double) (n - 3);
s3 += v
* (-3.0 * s2 + (double) n * n1 * (n - 2) * Math.pow(v, 2));
s2 += n1 * dev * v;
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 @@ public class UnivariateImpl implements Univariate, Serializable {
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);
}

View File

@ -62,7 +62,7 @@ import junit.framework.TestSuite;
*
* @author Phil Steitz
* @author Tim Obrien
* @version $Revision: 1.1 $ $Date: 2003/05/29 20:35:46 $
* @version $Revision: 1.2 $ $Date: 2003/06/21 02:08:23 $
*/
public final class UnivariateImplTest extends TestCase {
@ -235,4 +235,19 @@ public final class UnivariateImplTest extends TestCase {
//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);
}
}