You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ps...@apache.org on 2004/10/11 08:54:05 UTC

cvs commit: jakarta-commons/math/src/test/org/apache/commons/math/stat/descriptive/moment StandardDeviationTest.java

psteitz     2004/10/10 23:54:05

  Modified:    math/src/java/org/apache/commons/math/stat/descriptive/moment
                        StandardDeviation.java
               math/src/test/org/apache/commons/math/stat/descriptive/moment
                        StandardDeviationTest.java
  Log:
  Added support for population standard deviation.
  
  Revision  Changes    Path
  1.2       +53 -6     jakarta-commons/math/src/java/org/apache/commons/math/stat/descriptive/moment/StandardDeviation.java
  
  Index: StandardDeviation.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/stat/descriptive/moment/StandardDeviation.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- StandardDeviation.java	8 Oct 2004 05:08:17 -0000	1.1
  +++ StandardDeviation.java	11 Oct 2004 06:54:05 -0000	1.2
  @@ -21,9 +21,13 @@
   
   /**
    * Computes the sample standard deviation.  The standard deviation
  - * is the positive square root of the variance.  See {@link Variance} for
  - * more information.  This implementation wraps a {@link Variance}
  - * instance.
  + * is the positive square root of the variance.  This implementation wraps a
  + * {@link Variance} instance.  The <code>isBiasCorrected</code> property of the
  + * wrapped Variance instance is exposed, so that this class can be used to
  + * compute both the "sample standard deviation" (the square root of the 
  + * bias-corrected "sample variance") or the "population standard deviation"
  + * (the square root of the non-bias-corrected "population variance"). See 
  + * {@link Variance} for more information.  
    * <p>
    * <strong>Note that this implementation is not synchronized.</strong> If 
    * multiple threads access an instance of this class concurrently, and at least
  @@ -42,7 +46,8 @@
       private Variance variance = null;
   
       /**
  -     * Constructs a StandardDeviation
  +     * Constructs a StandardDeviation.  Sets the underlying {@link Variance}
  +     * instance's <code>isBiasCorrected</code> property to true.
        */
       public StandardDeviation() {
           variance = new Variance();
  @@ -56,6 +61,35 @@
       public StandardDeviation(final SecondMoment m2) {
           variance = new Variance(m2);
       }
  +    
  +    /**
  +     * Contructs a StandardDeviation with the specified value for the
  +     * <code>isBiasCorrected</code> property.  If this property is set to 
  +     * <code>true</code>, the {@link Variance} used in computing results will
  +     * use the bias-corrected, or "sample" formula.  See {@link Variance} for
  +     * details.
  +     * 
  +     * @param isBiasCorrected  whether or not the variance computation will use
  +     * the bias-corrected formula
  +     */
  +    public StandardDeviation(boolean isBiasCorrected) {
  +        variance = new Variance(isBiasCorrected);
  +    }
  +    
  +    /**
  +     * Contructs a StandardDeviation with the specified value for the
  +     * <code>isBiasCorrected</code> property and the supplied external moment.
  +     * If <code>isBiasCorrected</code> is set to <code>true</code>, the
  +     * {@link Variance} used in computing results will use the bias-corrected,
  +     * or "sample" formula.  See {@link Variance} for details.
  +     * 
  +     * @param isBiasCorrected  whether or not the variance computation will use
  +     * the bias-corrected formula
  +      * @param m2 the external moment
  +     */
  +    public StandardDeviation(boolean isBiasCorrected, SecondMoment m2) {
  +        variance = new Variance(isBiasCorrected, m2);
  +    }
   
       /**
        * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
  @@ -103,7 +137,6 @@
           return Math.sqrt(variance.evaluate(values));
       }
       
  -    
       /**
        * Returns the Standard Deviation of the entries in the specified portion of
        * the input array, or <code>Double.NaN</code> if the designated subarray
  @@ -178,5 +211,19 @@
        */
       public double evaluate(final double[] values, final double mean)  {
           return Math.sqrt(variance.evaluate(values, mean));
  +    }
  +    
  +    /**
  +     * @return Returns the isBiasCorrected.
  +     */
  +    public boolean isBiasCorrected() {
  +        return variance.isBiasCorrected();
  +    }
  +
  +    /**
  +     * @param isBiasCorrected The isBiasCorrected to set.
  +     */
  +    public void setBiasCorrected(boolean isBiasCorrected) {
  +        variance.setBiasCorrected(isBiasCorrected);
       }
   }
  
  
  
  1.2       +34 -1     jakarta-commons/math/src/test/org/apache/commons/math/stat/descriptive/moment/StandardDeviationTest.java
  
  Index: StandardDeviationTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/stat/descriptive/moment/StandardDeviationTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- StandardDeviationTest.java	8 Oct 2004 05:08:20 -0000	1.1
  +++ StandardDeviationTest.java	11 Oct 2004 06:54:05 -0000	1.2
  @@ -67,5 +67,38 @@
           std.increment(1d);
           assertEquals(0d, std.getResult(), 0);
       }
  +    
  +    /**
  +     * Test population version of variance
  +     */ 
  +    public void testPopulation() {
  +        double[] values = {-1.0d, 3.1d, 4.0d, -2.1d, 22d, 11.7d, 3d, 14d};
  +        double sigma = populationStandardDeviation(values);
  +        SecondMoment m = new SecondMoment();
  +        m.evaluate(values);  // side effect is to add values
  +        StandardDeviation s1 = new StandardDeviation();
  +        s1.setBiasCorrected(false);
  +        assertEquals(sigma, s1.evaluate(values), 1E-14);
  +        s1.incrementAll(values);
  +        assertEquals(sigma, s1.getResult(), 1E-14);
  +        s1 = new StandardDeviation(false, m);
  +        assertEquals(sigma, s1.getResult(), 1E-14);     
  +        s1 = new StandardDeviation(false);
  +        assertEquals(sigma, s1.evaluate(values), 1E-14);
  +        s1.incrementAll(values);
  +        assertEquals(sigma, s1.getResult(), 1E-14);     
  +    }
  +    
  +    /**
  +     * Definitional formula for population standard deviation
  +     */
  +    protected double populationStandardDeviation(double[] v) {
  +        double mean = new Mean().evaluate(v);
  +        double sum = 0;
  +        for (int i = 0; i < v.length; i++) {
  +            sum += (v[i] - mean) * (v[i] - mean); 
  +        }
  +        return Math.sqrt(sum / (double) v.length);
  +    }
   
   }
  
  
  

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