commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject cvs commit: jakarta-commons/math/src/java/org/apache/commons/math/stat TestStatistic.java TestStatisticImpl.java
Date Mon, 08 Mar 2004 04:22:13 GMT
psteitz     2004/03/07 20:22:13

  Modified:    math/src/java/org/apache/commons/math/stat
                        TestStatistic.java TestStatisticImpl.java
  Log:
  Fixed code and javadoc to advertise the right exceptions, cleaned up formatting.
  
  Revision  Changes    Path
  1.13      +15 -5     jakarta-commons/math/src/java/org/apache/commons/math/stat/TestStatistic.java
  
  Index: TestStatistic.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/stat/TestStatistic.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- TestStatistic.java	21 Feb 2004 21:35:15 -0000	1.12
  +++ TestStatistic.java	8 Mar 2004 04:22:12 -0000	1.13
  @@ -48,7 +48,7 @@
        * @throws IllegalArgumentException if preconditions are not met
        */
       double chiSquare(double[] expected, double[] observed) 
  -        throws IllegalArgumentException, MathException;
  +        throws IllegalArgumentException;
       
       /**
        * Returns the <i>observed significance level</i>, or <a href=
  @@ -77,6 +77,7 @@
        * @param expected array of exptected frequency counts
        * @return p-value
        * @throws IllegalArgumentException if preconditions are not met
  +     * @throws MathException if an error occurs computing the p-value
        */
       double chiSquareTest(double[] expected, double[] observed) 
           throws IllegalArgumentException, MathException;
  @@ -111,6 +112,7 @@
        * @return true iff null hypothesis can be rejected with confidence
        * 1 - alpha
        * @throws IllegalArgumentException if preconditions are not met
  +     * @throws MathException if an error occurs performing the test
        */
       boolean chiSquareTest(double[] expected, double[] observed, double alpha) 
           throws IllegalArgumentException, MathException;
  @@ -132,7 +134,7 @@
        * @throws IllegalArgumentException if input array length is less than 2
        */
       double t(double mu, double[] observed) 
  -        throws IllegalArgumentException, MathException;
  +        throws IllegalArgumentException;
       
       /**
        * Computes a <a href="http://www.itl.nist.gov/div898/handbook/prc/section3
  @@ -183,6 +185,7 @@
        * @param sample2 array of sample data values
        * @return p-value for t-test
        * @throws IllegalArgumentException if the precondition is not met
  +     * @throws MathException if an error occurs computing the p-value
        */
       double tTest(double[] sample1, double[] sample2)
           throws IllegalArgumentException, MathException;
  @@ -230,6 +233,7 @@
        * @return true if the null hypothesis can be rejected with 
        * confidence 1 - alpha
        * @throws IllegalArgumentException if the preconditions are not met
  +     * @throws MathException if an error occurs performing the test
        */
       boolean tTest(double[] sample1, double[] sample2, double alpha)
           throws IllegalArgumentException, MathException;
  @@ -269,6 +273,7 @@
        * @param alpha significance level of the test
        * @return p-value
        * @throws IllegalArgumentException if the precondition is not met
  +     * @throws MathException if an error computing the p-value
        */
       boolean tTest(double mu, double[] sample, double alpha)
           throws IllegalArgumentException, MathException;
  @@ -298,6 +303,7 @@
        * @param sample array of sample data values
        * @return p-value
        * @throws IllegalArgumentException if the precondition is not met
  +     * @throws MathException if an error occurs computing the p-value
        */
       double tTest(double mu, double[] sample)
           throws IllegalArgumentException, MathException;
  @@ -319,7 +325,7 @@
        * @throws IllegalArgumentException if the precondition is not met
        */
       double t(double mu, StatisticalSummary sampleStats) 
  -        throws IllegalArgumentException, MathException;
  +        throws IllegalArgumentException;
       
       /**
        * Computes a <a href="http://www.itl.nist.gov/div898/handbook/prc/section3
  @@ -340,7 +346,7 @@
        * @throws IllegalArgumentException if the precondition is not met
        */
       double t(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2) 
  -        throws IllegalArgumentException, MathException;
  +        throws IllegalArgumentException;
       
       /**
        * Returns the <i>observed significance level</i>, or <a href=
  @@ -372,6 +378,7 @@
        * @param sampleStats2 StatisticalSummary describing data from the second sample
        * @return p-value for t-test
        * @throws IllegalArgumentException if the precondition is not met
  +     * @throws MathException if an error occurs computing the p-value
        */
       double tTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2)
           throws IllegalArgumentException, MathException;
  @@ -421,6 +428,7 @@
        * @return true if the null hypothesis can be rejected with 
        * confidence 1 - alpha
        * @throws IllegalArgumentException if the preconditions are not met
  +     * @throws MathException if an error occurs performing the test
        */
       boolean tTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2, 
           double alpha)
  @@ -461,6 +469,7 @@
        * @param alpha significance level of the test
        * @return p-value
        * @throws IllegalArgumentException if the precondition is not met
  +     * @throws MathException if an error occurs computing the p-value
        */
       boolean tTest(double mu, StatisticalSummary sampleStats, double alpha)
           throws IllegalArgumentException, MathException;
  @@ -491,6 +500,7 @@
        * @param sampleStats StatisticalSummary describing sample data
        * @return p-value
        * @throws IllegalArgumentException if the precondition is not met
  +     * @throws MathException if an error occurs computing the p-value
        */
       double tTest(double mu, StatisticalSummary sampleStats)
           throws IllegalArgumentException, MathException;
  
  
  
  1.14      +44 -108   jakarta-commons/math/src/java/org/apache/commons/math/stat/TestStatisticImpl.java
  
  Index: TestStatisticImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/stat/TestStatisticImpl.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- TestStatisticImpl.java	21 Feb 2004 21:35:15 -0000	1.13
  +++ TestStatisticImpl.java	8 Mar 2004 04:22:12 -0000	1.14
  @@ -53,15 +53,13 @@
               throw new IllegalArgumentException("observed, expected array lengths incorrect");
           }
           if ((StatUtils.min(expected) <= 0) || (StatUtils.min(observed) < 0)) {
  -            throw new IllegalArgumentException(
  -                "observed counts must be non-negative,"
  +            throw new IllegalArgumentException( "observed counts must be non-negative,"
                       + " expected counts must be postive");
           }
           for (int i = 0; i < observed.length; i++) {
               dev = (observed[i] - expected[i]);
               sumSq += dev * dev / expected[i];
           }
  -
           return sumSq;
       }
   
  @@ -70,15 +68,13 @@
        * @param expected array of exptected frequency counts
        * @return p-value
        * @throws IllegalArgumentException if preconditions are not met
  +     * @throws MathException if an error occurs computing the p-value
        */
       public double chiSquareTest(double[] expected, double[] observed)
           throws IllegalArgumentException, MathException {
           ChiSquaredDistribution chiSquaredDistribution =
  -            DistributionFactory.newInstance().createChiSquareDistribution(
  -                (double) expected.length - 1);
  -        return 1
  -            - chiSquaredDistribution.cumulativeProbability(
  -                chiSquare(expected, observed));
  +            DistributionFactory.newInstance().createChiSquareDistribution((double) expected.length
- 1);
  +        return 1 - chiSquaredDistribution.cumulativeProbability(chiSquare(expected, observed));
       }
   
       /**
  @@ -88,15 +84,12 @@
        * @return true iff null hypothesis can be rejected with confidence
        * 1 - alpha
        * @throws IllegalArgumentException if preconditions are not met
  +     * @throws MathException if an error occurs performing the test
        */
  -    public boolean chiSquareTest(
  -        double[] expected,
  -        double[] observed,
  -        double alpha)
  +    public boolean chiSquareTest(double[] expected, double[] observed, double alpha)
           throws IllegalArgumentException, MathException {
           if ((alpha <= 0) || (alpha > 0.5)) {
  -            throw new IllegalArgumentException(
  -                "bad significance level: " + alpha);
  +            throw new IllegalArgumentException("bad significance level: " + alpha);
           }
           return (chiSquareTest(expected, observed) < alpha);
       }
  @@ -112,11 +105,7 @@
           if ((observed == null) || (observed.length < 5)) {
               throw new IllegalArgumentException("insufficient data for t statistic");
           }
  -        return t(
  -            StatUtils.mean(observed),
  -            mu,
  -            StatUtils.variance(observed),
  -            observed.length);
  +        return t(StatUtils.mean(observed), mu, StatUtils.variance(observed), observed.length);
       }
   
       /**
  @@ -125,12 +114,12 @@
        * @param alpha significance level of the test
        * @return p-value
        * @throws IllegalArgumentException if the precondition is not met
  +     * @throws MathException if an error occurs computing the p-value
        */
       public boolean tTest(double mu, double[] sample, double alpha)
           throws IllegalArgumentException, MathException {
           if ((alpha <= 0) || (alpha > 0.5)) {
  -            throw new IllegalArgumentException(
  -                "bad significance level: " + alpha);
  +            throw new IllegalArgumentException("bad significance level: " + alpha);
           }
           return (tTest(mu, sample) < alpha);
       }
  @@ -143,18 +132,12 @@
        */
       public double t(double[] sample1, double[] sample2)
           throws IllegalArgumentException {
  -        if ((sample1 == null)
  -            || (sample2 == null
  +        if ((sample1 == null) || (sample2 == null
                   || Math.min(sample1.length, sample2.length) < 5)) {
               throw new IllegalArgumentException("insufficient data for t statistic");
           }
  -        return t(
  -            StatUtils.mean(sample1),
  -            StatUtils.mean(sample2),
  -            StatUtils.variance(sample1),
  -            StatUtils.variance(sample2),
  -            (double) sample1.length,
  -            (double) sample2.length);
  +        return t(StatUtils.mean(sample1), StatUtils.mean(sample2), StatUtils.variance(sample1),
  +            StatUtils.variance(sample2),  (double) sample1.length, (double) sample2.length);
       }
   
       /**
  @@ -163,21 +146,16 @@
        * @param sample2 array of sample data values
        * @return tTest p-value
        * @throws IllegalArgumentException if the precondition is not met
  +     * @throws MathException if an error occurs computing the p-value
        */
       public double tTest(double[] sample1, double[] sample2)
           throws IllegalArgumentException, MathException {
  -        if ((sample1 == null)
  -            || (sample2 == null
  +        if ((sample1 == null) || (sample2 == null
                   || Math.min(sample1.length, sample2.length) < 5)) {
               throw new IllegalArgumentException("insufficient data");
           }
  -        return tTest(
  -            StatUtils.mean(sample1),
  -            StatUtils.mean(sample2),
  -            StatUtils.variance(sample1),
  -            StatUtils.variance(sample2),
  -            (double) sample1.length,
  -            (double) sample2.length);
  +        return tTest(StatUtils.mean(sample1), StatUtils.mean(sample2), StatUtils.variance(sample1),
  +            StatUtils.variance(sample2), (double) sample1.length, (double) sample2.length);
       }
   
       /**
  @@ -185,14 +163,14 @@
        * @param sample2 array of sample data values
        * @param alpha significance level
        * @return true if the null hypothesis can be rejected with 
  -     * confidence 1 - alpha
  +     *     confidence 1 - alpha
        * @throws IllegalArgumentException if the preconditions are not met
  +     * @throws MathException if an error occurs performing the test
        */
       public boolean tTest(double[] sample1, double[] sample2, double alpha)
           throws IllegalArgumentException, MathException {
           if ((alpha <= 0) || (alpha > 0.5)) {
  -            throw new IllegalArgumentException(
  -                "bad significance level: " + alpha);
  +            throw new IllegalArgumentException("bad significance level: " + alpha);
           }
           return (tTest(sample1, sample2) < alpha);
       }
  @@ -202,17 +180,14 @@
        * @param sample array of sample data values
        * @return p-value
        * @throws IllegalArgumentException if the precondition is not met
  +     * @throws MathException if an error occurs computing the p-value
        */
       public double tTest(double mu, double[] sample)
           throws IllegalArgumentException, MathException {
           if ((sample == null) || (sample.length < 5)) {
               throw new IllegalArgumentException("insufficient data for t statistic");
           }
  -        return tTest(
  -            StatUtils.mean(sample),
  -            mu,
  -            StatUtils.variance(sample),
  -            sample.length);
  +        return tTest( StatUtils.mean(sample), mu, StatUtils.variance(sample), sample.length);
       }
   
       /**
  @@ -226,11 +201,7 @@
           if ((sampleStats == null) || (sampleStats.getN() < 5)) {
               throw new IllegalArgumentException("insufficient data for t statistic");
           }
  -        return t(
  -            sampleStats.getMean(),
  -            mu,
  -            sampleStats.getVariance(),
  -            sampleStats.getN());
  +        return t(sampleStats.getMean(), mu, sampleStats.getVariance(), sampleStats.getN());
       }
   
       /**
  @@ -239,22 +210,15 @@
        * @return t statistic
        * @throws IllegalArgumentException if the precondition is not met
        */
  -    public double t(
  -        StatisticalSummary sampleStats1,
  -        StatisticalSummary sampleStats2)
  +    public double t(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2)
           throws IllegalArgumentException {
           if ((sampleStats1 == null)
               || (sampleStats2 == null
                   || Math.min(sampleStats1.getN(), sampleStats2.getN()) < 5)) {
               throw new IllegalArgumentException("insufficient data for t statistic");
           }
  -        return t(
  -            sampleStats1.getMean(),
  -            sampleStats2.getMean(),
  -            sampleStats1.getVariance(),
  -            sampleStats2.getVariance(),
  -            (double) sampleStats1.getN(),
  -            (double) sampleStats2.getN());
  +        return t(sampleStats1.getMean(), sampleStats2.getMean(), sampleStats1.getVariance(),
  +            sampleStats2.getVariance(), (double) sampleStats1.getN(), (double) sampleStats2.getN());
       }
   
       /**
  @@ -262,23 +226,16 @@
        * @param sampleStats2 StatisticalSummary describing data from the second sample
        * @return p-value for t-test
        * @throws IllegalArgumentException if the precondition is not met
  +     * @throws MathException if an error occurs computing the p-value
        */
  -    public double tTest(
  -        StatisticalSummary sampleStats1,
  -        StatisticalSummary sampleStats2)
  +    public double tTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2)
           throws IllegalArgumentException, MathException {
  -        if ((sampleStats1 == null)
  -            || (sampleStats2 == null
  +        if ((sampleStats1 == null) || (sampleStats2 == null
                   || Math.min(sampleStats1.getN(), sampleStats2.getN()) < 5)) {
               throw new IllegalArgumentException("insufficient data for t statistic");
           }
  -        return tTest(
  -            sampleStats1.getMean(),
  -            sampleStats2.getMean(),
  -            sampleStats1.getVariance(),
  -            sampleStats2.getVariance(),
  -            (double) sampleStats1.getN(),
  -            (double) sampleStats2.getN());
  +        return tTest(sampleStats1.getMean(), sampleStats2.getMean(), sampleStats1.getVariance(),
  +            sampleStats2.getVariance(), (double) sampleStats1.getN(), (double) sampleStats2.getN());
       }
   
       /**
  @@ -286,17 +243,15 @@
        * @param sampleStats2 StatisticalSummary describing sample data values
        * @param alpha significance level of the test
        * @return true if the null hypothesis can be rejected with 
  -     * confidence 1 - alpha
  +     *     confidence 1 - alpha
        * @throws IllegalArgumentException if the preconditions are not met
  +     * @throws MathException if an error occurs performing the test
        */
  -    public boolean tTest(
  -        StatisticalSummary sampleStats1,
  -        StatisticalSummary sampleStats2,
  +    public boolean tTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2,
           double alpha)
           throws IllegalArgumentException, MathException {
           if ((alpha <= 0) || (alpha > 0.5)) {
  -            throw new IllegalArgumentException(
  -                "bad significance level: " + alpha);
  +            throw new IllegalArgumentException("bad significance level: " + alpha);
           }
           return (tTest(sampleStats1, sampleStats2) < alpha);
       }
  @@ -307,15 +262,12 @@
        * @param alpha significance level of the test
        * @return p-value
        * @throws IllegalArgumentException if the precondition is not met
  +     * @throws MathException if an error occurs computing the p-value
        */
  -    public boolean tTest(
  -        double mu,
  -        StatisticalSummary sampleStats,
  -        double alpha)
  +    public boolean tTest( double mu, StatisticalSummary sampleStats,double alpha)
           throws IllegalArgumentException, MathException {
           if ((alpha <= 0) || (alpha > 0.5)) {
  -            throw new IllegalArgumentException(
  -                "bad significance level: " + alpha);
  +            throw new IllegalArgumentException("bad significance level: " + alpha);
           }
           return (tTest(mu, sampleStats) < alpha);
       }
  @@ -325,17 +277,14 @@
        * @param sampleStats StatisticalSummary describing sample data
        * @return p-value
        * @throws IllegalArgumentException if the precondition is not met
  +     * @throws MathException if an error occurs computing the p-value
        */
       public double tTest(double mu, StatisticalSummary sampleStats)
           throws IllegalArgumentException, MathException {
           if ((sampleStats == null) || (sampleStats.getN() < 5)) {
               throw new IllegalArgumentException("insufficient data for t statistic");
           }
  -        return tTest(
  -            sampleStats.getMean(),
  -            mu,
  -            sampleStats.getVariance(),
  -            sampleStats.getN());
  +        return tTest(sampleStats.getMean(), mu, sampleStats.getVariance(), sampleStats.getN());
       }
   
       //----------------------------------------------- Private methods 
  @@ -366,13 +315,7 @@
       * @param n2 second sample n
       * @return t test statistic
       */
  -    private double t(
  -        double m1,
  -        double m2,
  -        double v1,
  -        double v2,
  -        double n1,
  -        double n2) {
  +    private double t(double m1, double m2,  double v1, double v2, double n1,double n2)
 {
           return (m1 - m2) / Math.sqrt((v1 / n1) + (v2 / n2));
       }
   
  @@ -400,18 +343,11 @@
        * @param n2 second sample n
        * @return p-value
        */
  -    private double tTest(
  -        double m1,
  -        double m2,
  -        double v1,
  -        double v2,
  -        double n1,
  -        double n2)
  +    private double tTest(double m1, double m2, double v1, double v2, double n1, double
n2)
           throws MathException {
           double t = Math.abs(t(m1, m2, v1, v2, n1, n2));
           TDistribution tDistribution =
  -            DistributionFactory.newInstance().createTDistribution(
  -                df(v1, v2, n1, n2));
  +            DistributionFactory.newInstance().createTDistribution(df(v1, v2, n1, n2));
           return 1.0 - tDistribution.cumulativeProbability(-t, t);
       }
   
  
  
  

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