mdiggory 2003/06/03 21:05:39
Modified: math/src/java/org/apache/commons/math/stat
UnivariateImpl.java
Log:
Improved Variance calculation, test for negative variance and added some more
javadoc.
Revision Changes Path
1.2 +65 -42
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.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- UnivariateImpl.java 29 May 2003 20:35:45 -0000 1.1
+++ UnivariateImpl.java 4 Jun 2003 04:05:39 -0000 1.2
@@ -68,7 +68,7 @@
*
* @author Phil Steitz
* @author <a href="mailto:[EMAIL PROTECTED]">Tim O'Brien</a>
- * @author Mark Diggory
+ * @author <a href="mailto:[EMAIL PROTECTED]">Mark Diggory</a>
* @author Brent Worden
* @version $Revision$ $Date$
*
@@ -79,7 +79,7 @@
private int windowSize = Univariate.INFINITE_WINDOW;
/** Just in case, the windowSize is not inifinite, we need to
- * keep an array to remember values 0 to N
+ * keep an array to remember values 0 to N
*/
private DoubleArray doubleArray;
@@ -107,25 +107,30 @@
/** product of values that have been added */
private double product = Double.NaN;
- /** Creates new univariate */
+ /** Creates new univariate with an inifinite window */
public UnivariateImpl() {
clear();
}
- /** Create a new univariate with a fixed window **/
+ /** Creates a new univariate with a fixed window **/
public UnivariateImpl(int window) {
windowSize = window;
doubleArray = new FixedDoubleArray( window );
}
- public void addValue(double v) {
-
+ /**
+ * @see org.apache.commons.math.stat.Univariate#addValue(double)
+ */
+ public void addValue(double v) {
insertValue(v);
}
- public double getMean() {
+ /**
+ * @see org.apache.commons.math.stat.Univariate#getMean()
+ */
+ public double getMean() {
if (n == 0) {
return Double.NaN;
} else {
@@ -134,7 +139,10 @@
}
- public double getGeometricMean() {
+ /**
+ * @see org.apache.commons.math.stat.Univariate#getGeometricMean()
+ */
+ public double getGeometricMean() {
if ((product <= 0.0) || (n == 0)) {
return Double.NaN;
} else {
@@ -142,36 +150,44 @@
}
}
-
- public double getProduct() {
+ /**
+ * @see org.apache.commons.math.stat.Univariate#getProduct()
+ */
+ public double getProduct() {
return product;
}
-
- public double getVariance() {
- double variance = Double.NaN;
-
- if( n == 1 ) {
- variance = 0.0;
- } else if( n > 1 ) {
- double xbar = getMean();
- variance = (sumsq - xbar*xbar*((double) n))/(((double) n)-1);
- }
+ /**
+ * @see org.apache.commons.math.stat.Univariate#getStandardDeviation()
+ */
+ public double getStandardDeviation() {
+ double variance = getVariance();
+ if ((variance == 0.0) || (variance == Double.NaN)) {
+ return variance;
+ } else {
+ return Math.sqrt(variance);
+ }
+ }
+
+ /**
+ * Returns the variance of the values that have been added as described by
+ * <a href=http://mathworld.wolfram.com/k-Statistic.html>Equation (5) for
k-Statistics</a>.
+ *
+ * @return The variance of a set of values. Double.NaN is returned for
+ * an empty set of values and 0.0 is returned for a <= 1 value set.
+ */
+ public double getVariance() {
+ double variance = Double.NaN;
- return variance;
- }
+ if( n == 1 ) {
+ variance = 0.0;
+ } else if( n > 1 ) {
+ variance = (((double)n)*sumsq - (sum * sum)) / (double) (n *
(n - 1));
+ }
-
- public double getStandardDeviation() {
- double variance = getVariance();
- if ((variance == 0.0) || (variance == Double.NaN)) {
- return variance;
- } else {
- return Math.sqrt(variance);
- }
- }
-
-
+ return variance < 0 ? 0.0 : variance;
+ }
+
/**
* Returns the skewness of the values that have been added as described by
* <a href=http://mathworld.wolfram.com/k-Statistic.html>Equation (6) for
k-Statistics</a>.
@@ -184,8 +200,8 @@
if( n < 1) return Double.NaN;
if( n <= 2 ) return 0.0;
- return ( 2*Math.pow(sum,3) - 3*sum*sumsq +
((double)n)*((double)n)*sumCube ) /
- ( ((double)n)*(((double)n)-1)*(((double)n)-2));
+ return ( 2*Math.pow(sum,3) - 3*sum*sumsq + ((double)(n*n))*sumCube ) /
+ ( (double)(n*(n-1)*(n-2)) ) ;
}
/**
@@ -202,14 +218,19 @@
double x1 = -6*Math.pow(sum,4);
double x2 = 12*((double)n)*Math.pow(sum,2)*sumsq;
- double x3 = -3*((double)n)*(((double)n)-1)*Math.pow(sumsq,2);
- double x4 = -4*((double)n)*(((double)n)+1)*sum*sumCube;
- double x5 = Math.pow(((double)n),2)*(((double)n)+1)*sumQuad;
+ double x3 = -3*((double)(n*(n-1)))*Math.pow(sumsq,2);
+ double x4 = -4*((double)(n*(n+1)))*sum*sumCube;
+ double x5 = Math.pow(((double)n),2)*((double)(n+1))*sumQuad;
+
return (x1 + x2 + x3 + x4 + x5) /
- (((double)n)*(((double)n)-1)*(((double)n)-2)*(((double)n)-3));
+ ( (double)(n*(n-1)*(n-2)*(n-3)) );
}
- private void insertValue(double v) {
+ /**
+ * Called in "addValue" to insert a new value into the statistic.
+ * @param v The value to be added.
+ */
+ private void insertValue(double v) {
// The default value of product is NaN, if you
// try to retrieve the product for a univariate with
@@ -351,7 +372,9 @@
return outBuffer.toString();
}
- /** Resets all sums to 0, resets min and max */
+ /**
+ * Resets all sums to 0, resets min and max
+ */
public void clear() {
this.sum = this.sumsq = this.sumCube = this.sumQuad = 0.0;
this.n = 0;
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]