commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdigg...@apache.org
Subject cvs commit: jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat StoreUnivariate.java AbstractStoreUnivariate.java
Date Wed, 04 Jun 2003 02:22:48 GMT
mdiggory    2003/06/03 19:22:48

  Modified:    math/src/java/org/apache/commons/math/stat
                        StoreUnivariate.java AbstractStoreUnivariate.java
  Log:
  PR: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=20377
  Submitted by:	Phil Steitz
  
  Revision  Changes    Path
  1.2       +31 -1     jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/StoreUnivariate.java
  
  Index: StoreUnivariate.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/StoreUnivariate.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- StoreUnivariate.java	29 May 2003 20:35:45 -0000	1.1
  +++ StoreUnivariate.java	4 Jun 2003 02:22:48 -0000	1.2
  @@ -113,12 +113,24 @@
   
       /**
        * Returns the current set of values in an array of double primitives.  
  -     * The order of addition is preserved
  +     * The order of addition is preserved.  The returned array is a fresh
  +     * copy of the underlying data -- i.e., it is not a reference to the
  +     * stored data.
        * 
        * @return returns the current set of numbers in the order in which they 
        *         were added to this set
        */
       public abstract double[] getValues();
  +    
  +    /**
  +     * Returns the current set of values in an array of double primitives,  
  +     * sorted in ascending order.  The returned array is a fresh
  +     * copy of the underlying data -- i.e., it is not a reference to the
  +     * stored data.
  +     * 
  +     * @return returns the current set of numbers sorted in ascending order        
  +     */
  +    public abstract double[] getSortedValues(); 
   
       /**
        * Returns the element at the specified index
  @@ -126,5 +138,23 @@
        * @return return the element at the specified index
        */
       public abstract double getElement(int index);
  +    
  +    /**
  +     * Returns an estimate for the pth percentile of the stored values,
  +     * following the interpolation-adjusted defintion presented 
  +     * <a href="http://www.utdallas.edu/~ammann/stat5311/node8.html">here</a><p>
  +     *
  +     * <strong>Preconditions</strong>:<ul>
  +     * <li><code>0 < p < 100</code> (otherwise an <code>IllegalArgumentException
  +     *     </code> is thrown)</li>
  +     * <li>at least one value must be stored (returns <code>Double.NaN
  +     *     </code> otherwise)</li>
  +     * </ul>
  +     * 
  +     * @param p the requested percentile (scaled from 0 - 100)
  +     * @return returns an estimate for the pth percentile of the stored data 
  +     * values
  +     */
  +    public abstract double getPercentile(double p);
   
   }
  
  
  
  1.2       +61 -0     jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/AbstractStoreUnivariate.java
  
  Index: AbstractStoreUnivariate.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/AbstractStoreUnivariate.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AbstractStoreUnivariate.java	29 May 2003 20:35:45 -0000	1.1
  +++ AbstractStoreUnivariate.java	4 Jun 2003 02:22:48 -0000	1.2
  @@ -58,6 +58,7 @@
    * 
    * @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
    * @author Mark Diggory
  + * @author <a href="mailto:phil@steitz.com">Phil Steitz</a>
    */
   public abstract class AbstractStoreUnivariate implements StoreUnivariate {
   
  @@ -290,6 +291,66 @@
               accum += Math.pow(getElement(i), 2.0);
           }
           return accum;
  +    }
  +   
  +    /**
  +     * Uses <a href="http://www.nist.gov/dads/HTML/shellsort.html">Shell sort
  +     * </a>
  +     * @see org.apache.commons.math.StoreUnivariate#getSortedValues()
  +     *
  +     */ 
  +    public double[] getSortedValues() {
  +        double[] values = getValues();
  +        int n = values.length;
  +        int j = n;
  +        while (j > 1) {
  +            j = j / 2;
  +            boolean done = false;
  +            while (!done) {
  +                done = true;
  +                for (int i = 0; i < n - j; i++) {
  +                    int k = i + j;
  +                    if (values[i] > values[k]) {
  +                        double temp = values[i];
  +                        values[i] = values[k];
  +                        values[k] = temp;
  +                        done = false;
  +                    }
  +                }
  +            }
  +        }
  +        return values;
  +    }
  +    
  +    /**
  +     * Returns an estimate for the pth percentile of the stored values
  +     * @see org.apache.commons.math.StoreUnivariate#getPercentile()
  +     */
  +    public double getPercentile(double p) {    
  +        if ((p > 100) || (p <= 0)) {
  +            throw new IllegalArgumentException("invalid percentile value");
  +        }
  +        double n = (double) getN();
  +        if (n == 0) {
  +            return Double.NaN;
  +        }
  +        if (n == 1) {
  +            return getElement(0);  // always return single value for n = 1
  +        }
  +        double pos = p * (n + 1) / 100;
  +        double fpos = Math.floor(pos);
  +        int intPos = (int) fpos;
  +        double d = pos - fpos;
  +        double[] sorted = getSortedValues();
  +        if (pos < 1) {
  +            return sorted[0];
  +        }
  +        if (pos > n) {
  +            return sorted[getN() - 1];
  +        }
  +        double lower = sorted[intPos - 1];
  +        double upper = sorted[intPos];
  +        return lower + d * (upper - lower);       
       }
   
   }
  
  
  

---------------------------------------------------------------------
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