psteitz     2004/07/17 15:01:39

  Modified:    math/src/java/org/apache/commons/math/stat StatUtils.java
               math/src/test/org/apache/commons/math/stat
                        StatUtilsTest.java
  Log:
  Added geometric mean to StatUtils.
  
  Revision  Changes    Path
  1.31      +46 -2     
jakarta-commons/math/src/java/org/apache/commons/math/stat/StatUtils.java
  
  Index: StatUtils.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/stat/StatUtils.java,v
  retrieving revision 1.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- StatUtils.java    11 Jul 2004 18:41:19 -0000      1.30
  +++ StatUtils.java    17 Jul 2004 22:01:39 -0000      1.31
  @@ -16,6 +16,7 @@
   package org.apache.commons.math.stat;
   
   import org.apache.commons.math.stat.univariate.UnivariateStatistic;
  +import org.apache.commons.math.stat.univariate.moment.GeometricMean;
   import org.apache.commons.math.stat.univariate.moment.Mean;
   import org.apache.commons.math.stat.univariate.moment.Variance;
   import org.apache.commons.math.stat.univariate.rank.Max;
  @@ -58,8 +59,11 @@
       /** variance */
       private static Variance variance = new Variance();
   
  -    /** variance */
  +    /** percentile */
       private static Percentile percentile = new Percentile();
  +    
  +    /** geometric mean */
  +    private static GeometricMean geometricMean = new GeometricMean();
   
       /**
        * Private Constructor
  @@ -246,6 +250,46 @@
               final int length) {
           return mean.evaluate(values, begin, length);
       }
  +    
  +    /**
  +     * Returns the geometric mean of the entries in the input array, or 
  +     * <code>Double.NaN</code> if the array is empty.
  +     * <p>
  +     * Throws <code>IllegalArgumentException</code> if the array is null.
  +     * <p>
  +     * See [EMAIL PROTECTED] 
org.apache.commons.math.stat.univariate.moment.GeometricMean}
  +     * for details on the computing algorithm.
  +     * 
  +     * @param values the input array
  +     * @return the geometric mean of the values or Double.NaN if the array is empty
  +     * @throws IllegalArgumentException if the array is null
  +     */
  +    public static double geometricMean(final double[] values) {
  +        return geometricMean.evaluate(values);
  +    }
  +
  +    /**
  +     * Returns the geometric mean of the entries in the specified portion of
  +     * the input array, or <code>Double.NaN</code> if the designated subarray
  +     * is empty.
  +     * <p>
  +     * Throws <code>IllegalArgumentException</code> if the array is null.
  +     * <p>
  +     * See [EMAIL PROTECTED] 
org.apache.commons.math.stat.univariate.moment.GeometricMean}
  +     * for details on the computing algorithm.
  +     * 
  +     * @param values the input array
  +     * @param begin index of the first array element to include
  +     * @param length the number of elements to include
  +     * @return the geometric mean of the values or Double.NaN if length = 0
  +     * @throws IllegalArgumentException if the array is null or the array index
  +     * parameters are not valid
  +     */
  +    public static double geometricMean(final double[] values, final int begin,
  +            final int length) {
  +        return geometricMean.evaluate(values, begin, length);
  +    }
  +    
   
       /**
        * Returns the variance of the entries in the input array, or 
  
  
  
  1.19      +14 -1     
jakarta-commons/math/src/test/org/apache/commons/math/stat/StatUtilsTest.java
  
  Index: StatUtilsTest.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/stat/StatUtilsTest.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- StatUtilsTest.java        11 Jul 2004 18:42:07 -0000      1.18
  +++ StatUtilsTest.java        17 Jul 2004 22:01:39 -0000      1.19
  @@ -372,4 +372,17 @@
               // expected
           }
       }
  +    
  +    public void testGeometricMean() throws Exception {
  +        double[] test = null;
  +        try {
  +            double x = StatUtils.geometricMean(test);
  +            fail("Expecting IllegalArgumentException");
  +        } catch (IllegalArgumentException ex) {
  +            // expected
  +        }
  +        test = new double[] {2, 4, 6, 8};
  +        assertEquals(Math.exp(0.25d * StatUtils.sumLog(test)), 
  +                StatUtils.geometricMean(test), Double.MIN_VALUE);
  +    }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to