commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tobr...@apache.org
Subject cvs commit: jakarta-commons-sandbox/math/src/test/org/apache/commons/math UnivariateImplTest.java
Date Thu, 29 May 2003 19:49:18 GMT
tobrien     2003/05/29 12:49:18

  Modified:    math/src/java/org/apache/commons/math Univariate.java
                        UnivariateImpl.java
               math/src/test/org/apache/commons/math
                        UnivariateImplTest.java
  Log:
  Patch adds getSkewness and getKurtosis to Univariate and an implimentation
  in UnivariateImpl. It also adds test for NaN and 0.0 to UnivariateImplTest
  
  PR: Issue 20250
  Obtained from: Bugzilla
  Submitted by: Mark Diggory
  Reviewed by: Tim O'Brien
  
  Revision  Changes    Path
  1.8       +21 -1     jakarta-commons-sandbox/math/src/java/org/apache/commons/math/Univariate.java
  
  Index: Univariate.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/Univariate.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Univariate.java	26 May 2003 17:40:20 -0000	1.7
  +++ Univariate.java	29 May 2003 19:49:18 -0000	1.8
  @@ -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
  
  
  
  1.9       +71 -13    jakarta-commons-sandbox/math/src/java/org/apache/commons/math/UnivariateImpl.java
  
  Index: UnivariateImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/UnivariateImpl.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- UnivariateImpl.java	26 May 2003 17:40:20 -0000	1.8
  +++ UnivariateImpl.java	29 May 2003 19:49:18 -0000	1.9
  @@ -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,44 @@
           }
       }
      
  +   
  +	/**
  +	 * 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 + ((double)n)*((double)n)*sumCube ) / 
  +			   ( ((double)n)*(((double)n)-1)*(((double)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*((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;
  +		return (x1 + x2 + x3 + x4 + x5) / 
  +		       (((double)n)*(((double)n)-1)*(((double)n)-2)*(((double)n)-3));
  +	} 
  +	
       private void insertValue(double v) {
   
           // The default value of product is NaN, if you
  @@ -176,14 +220,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 +237,6 @@
                       }
                   } 
                   
  -                sum += v;
  -                sumsq += v*v;
  -
                   if(product != 0.0){
                       // can safely remove discarded value
                       product *= v/discarded;
  @@ -203,7 +245,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 +254,6 @@
                   n += 1.0;
                   if (v < min) min = v;
                   if (v > max) max = v;
  -                sum += v;
  -                sumsq += v*v;
                   product *= v;
               }
           } else {
  @@ -223,10 +263,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 +315,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 +343,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;
  
  
  
  1.4       +47 -16    jakarta-commons-sandbox/math/src/test/org/apache/commons/math/UnivariateImplTest.java
  
  Index: UnivariateImplTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/test/org/apache/commons/math/UnivariateImplTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- UnivariateImplTest.java	23 May 2003 17:33:18 -0000	1.3
  +++ UnivariateImplTest.java	29 May 2003 19:49:18 -0000	1.4
  @@ -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