commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 20250] New: - [math] Addition of skew and Kurt to Univariate and UnivariateImpl
Date Mon, 26 May 2003 19:11:20 GMT
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=20250>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=20250

[math] Addition of skew and Kurt to Univariate and UnivariateImpl

           Summary: [math] Addition of skew and Kurt to Univariate and
                    UnivariateImpl
           Product: Commons
           Version: unspecified
          Platform: Other
        OS/Version: Other
            Status: NEW
          Severity: Enhancement
          Priority: Other
         Component: Sandbox
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: mdiggory@latte.harvard.edu


This patch adds getSkewness and getKurtosis to Univariate and an implimentation
in UnivariateImpl. It also adds test for NaN and 0.0 to UnivariateImplTest

Start of patch...
Index: java/org/apache/commons/math/Univariate.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/Univariate.java,v
retrieving revision 1.7
diff -u -r1.7 Univariate.java
--- java/org/apache/commons/math/Univariate.java	26 May 2003 17:40:20 -0000	1.7
+++ java/org/apache/commons/math/Univariate.java	26 May 2003 19:03:18 -0000
@@ -132,6 +132,26 @@
      */
     abstract double getStandardDeviation();
 
+	/**
+     * Returns the skewness of a given distribution.  Skewness is a 
+     * measure of the assymetry of a given distribution. <p>
+	 * Double.NaN is returned for an empty set of values and 0.0 is 
+	 * returned for a value set &lt;=2. 
+	 * 
+	 * @return Value of property skewness.
+	 */
+	abstract double getSkewness();
+	
+	/**
+     * Returns the Kurtosis of the available values. Kurtosis is a 
+     * measure of the "peakedness" of a distribution <p>
+     * Double.NaN is returned for an empty set of values and 0.0 is 
+     * returned for a value set &lt;=3. 
+     * 
+     * @return Value of property kurtosis.
+	 */
+	abstract double getKurtosis();
+		
     /** 
      * Returns the maximum of the available values <p>
      * Double.NaN is returned in no values have been added
Index: java/org/apache/commons/math/UnivariateImpl.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/UnivariateImpl.java,v
retrieving revision 1.8
diff -u -r1.8 UnivariateImpl.java
--- java/org/apache/commons/math/UnivariateImpl.java	26 May 2003 17:40:20 -0000	1.8
+++ java/org/apache/commons/math/UnivariateImpl.java	26 May 2003 19:03:18 -0000
@@ -86,6 +86,12 @@
     /** running sum of squares that have been added */
     private double sumsq = 0.0;
 
+	/** running sum of 3rd powers that have been added */
+	private double sumCube = 0.0;
+	
+	/** running sum of 4th powers that have been added */
+	private double sumQuad = 0.0;
+	
     /** count of values that have been added */
     private int n = 0;
 
@@ -162,6 +168,42 @@
         }
     }
    
+   
+	/**
+	 * 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>.
+     * 
+	 * @return The skew of a set of values.  Double.NaN is returned for
+	 *         an empty set of values and 0.0 is returned for a &lt;= 2 value set.
+	 */
+	public double getSkewness() {
+		
+		if( n < 1) return Double.NaN;
+		if( n <= 2 ) return 0.0;                  
+			
+		return ( 2*Math.pow(sum,3) - 3*sum*sumsq + n*n*sumCube ) / ( n*(n-1)*(n-2));  
+	}
+	
+	/**
+	 * Returns the kurtosis of the values that have been added as described by
+     * <a href=http://mathworld.wolfram.com/k-Statistic.html>Equation (7) for
k-Statistics</a>.
+     * 
+	 * @return The kurtosis of a set of values.  Double.NaN is returned for
+	 *         an empty set of values and 0.0 is returned for a &lt;= 3 value set.
+	 */
+	public double getKurtosis() {
+		
+		if( n < 1) return Double.NaN;
+		if( n <= 3 ) return 0.0;
+		
+		double x1 = -6*Math.pow(sum,4);
+		double x2 = 12*n*Math.pow(sum,2)*sumsq;
+		double x3 = -3*n*(n-1)*Math.pow(sumsq,2);
+		double x4 = -4*n*(n+1)*sum*sumCube;
+		double x5 = Math.pow(n,2)*(n+1)*sumQuad;
+		return (x1 + x2 + x3 + x4 + x5) / (n*(n-1)*(n-2)*(n-3));
+	} 
+	
     private void insertValue(double v) {
 
         // The default value of product is NaN, if you
@@ -176,14 +218,15 @@
         }
 
         if( windowSize != Univariate.INFINITE_WINDOW ) {
-
             if( windowSize == n ) {
                 double discarded = doubleArray.addElementRolling( v );
 
                 // Remove the influence of the discarded
                 sum -= discarded;
                 sumsq -= discarded * discarded;
-
+				sumCube -= Math.pow(discarded,3);
+				sumQuad -= Math.pow(discarded,4); 
+				
                 if(discarded == min) {
                     min = doubleArray.getMin();
                 } else {
@@ -192,9 +235,6 @@
                     }
                 } 
                 
-                sum += v;
-                sumsq += v*v;
-
                 if(product != 0.0){
                     // can safely remove discarded value
                     product *= v/discarded;
@@ -203,7 +243,7 @@
                     product = 1.0;
                     double[] elements = doubleArray.getElements();
                     for( int i = 0; i < elements.length; i++ ) {
-                    product *= elements[i];
+                    	product *= elements[i];
                     }
                 } // else product = 0 and will still be 0 after discard
 
@@ -212,8 +252,6 @@
                 n += 1.0;
                 if (v < min) min = v;
                 if (v > max) max = v;
-                sum += v;
-                sumsq += v*v;
                 product *= v;
             }
         } else {
@@ -223,10 +261,13 @@
             n += 1.0;
             if (v < min) min = v;
             if (v > max) max = v;
-            sum += v;
-            sumsq += v*v;
             product *= v;
         }
+        
+		sum += v;
+		sumsq += v*v;
+		sumCube += Math.pow(v,3);
+		sumQuad += Math.pow(v,4);
     }
 
     /** Getter for property max.
@@ -272,6 +313,20 @@
         return sumsq;
     }
 
+	/** Getter for property sumCube.
+	 * @return Value of property sumCube.
+	 */
+	public double getSumCube() {
+		return sumCube;
+	}
+	
+	/** Getter for property sumQuad.
+	 * @return Value of property sumQuad.
+	 */
+	public double getSumQuad() {
+		return sumQuad;
+	}
+	
     /**
      * Generates a text report displaying 
      * univariate statistics from values that
@@ -286,13 +341,14 @@
         outBuffer.append("max: " + max + "\n");
         outBuffer.append("mean: " + getMean() + "\n");
         outBuffer.append("std dev: " + getStandardDeviation() + "\n");
+		outBuffer.append("skewness: " + getSkewness() + "\n");
+		outBuffer.append("kurtosis: " + getKurtosis() + "\n");
         return outBuffer.toString();
     }
     
     /** Resets all sums to 0, resets min and max */
     public void clear() {
-        this.sum = 0.0;
-        this.sumsq = 0.0;
+        this.sum = this.sumsq = this.sumCube = this.sumQuad = 0.0;
         this.n = 0;
         this.min = Double.MAX_VALUE;
         this.max = Double.MIN_VALUE;
Index: test/org/apache/commons/math/UnivariateImplTest.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons-sandbox/math/src/test/org/apache/commons/math/UnivariateImplTest.java,v
retrieving revision 1.3
diff -u -r1.3 UnivariateImplTest.java
--- test/org/apache/commons/math/UnivariateImplTest.java	23 May 2003 17:33:18
-0000	1.3
+++ test/org/apache/commons/math/UnivariateImplTest.java	26 May 2003 19:03:19 -0000
@@ -117,19 +117,54 @@
     	UnivariateImpl u = new UnivariateImpl();
         assertTrue("Mean of n = 0 set should be NaN", 
             Double.isNaN( u.getMean() ) );
-	assertTrue("Standard Deviation of n = 0 set should be NaN", 
+		assertTrue("Standard Deviation of n = 0 set should be NaN", 
             Double.isNaN( u.getStandardDeviation() ) );
-	assertTrue("Variance of n = 0 set should be NaN", 
+		assertTrue("Variance of n = 0 set should be NaN", 
             Double.isNaN(u.getVariance() ) );
+		assertTrue("skew of n = 0 set should be NaN",
+			Double.isNaN(u.getSkewness() ) );	
+		assertTrue("kurtosis of n = 0 set should be NaN", 
+			Double.isNaN(u.getKurtosis() ) );		
+		
 	
-        u.addValue(one);
-
-	assertTrue( "Mean of n = 1 set should be value of single item n1", 
-            u.getMean() == one);
-	assertTrue( "Mean of n = 1 set should be zero", 
-            u.getStandardDeviation() == 0);
-	assertTrue( "Variance of n = 1 set should be zero",
-            u.getVariance() == 0);	
+		/* n=1 */
+		u.addValue(one);
+		assertTrue("mean should be one (n = 1)", 
+			u.getMean() == one);
+		assertTrue("geometric should be one (n = 1)", 
+			u.getGeometricMean() == one);
+		assertTrue("Std should be zero (n = 1)", 
+			u.getStandardDeviation() == 0.0);
+		assertTrue("variance should be zero (n = 1)", 
+			u.getVariance() == 0.0);
+		assertTrue("skew should be zero (n = 1)", 
+			u.getSkewness() == 0.0);
+		assertTrue("kurtosis should be zero (n = 1)", 
+			u.getKurtosis() == 0.0);		
+					
+		/* n=2 */				
+		u.addValue(twoF);
+		assertTrue("Std should not be zero (n = 2)", 
+			u.getStandardDeviation() != 0.0);
+		assertTrue("variance should not be zero (n = 2)", 
+			u.getVariance() != 0.0);
+		assertTrue("skew should not be zero (n = 2)", 
+			u.getSkewness() == 0.0);
+		assertTrue("kurtosis should be zero (n = 2)", 
+			u.getKurtosis() == 0.0);
+
+		/* n=3 */
+		u.addValue(twoL);
+		assertTrue("skew should not be zero (n = 3)", 
+			u.getSkewness() != 0.0);
+		assertTrue("kurtosis should be zero (n = 3)", 
+			u.getKurtosis() == 0.0);
+        
+		/* n=4 */
+		u.addValue(three);
+		assertTrue("kurtosis should not be zero (n = 4)", 
+			u.getKurtosis() != 0.0);        
+            
     }
 
     public void testProductAndGeometricMean() throws Exception {
@@ -199,9 +234,5 @@
         
         //FiXME: test all other NaN contract specs
     }
-        
-        
-        
-
-}
 
+}
\ No newline at end of file

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message