Coverage Report - org.apache.commons.math.stat.descriptive.SummaryStatisticsImpl

Classes in this File Line Coverage Branch Coverage Complexity
SummaryStatisticsImpl
82% 
100% 
1.154

 1  
 /*
 2  
  * Copyright 2004 The Apache Software Foundation.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.commons.math.stat.descriptive;
 17  
 
 18  
 import java.io.Serializable;
 19  
 import org.apache.commons.math.stat.descriptive.moment.SecondMoment;
 20  
 import org.apache.commons.math.stat.descriptive.moment.GeometricMean;
 21  
 import org.apache.commons.math.stat.descriptive.moment.Mean;
 22  
 import org.apache.commons.math.stat.descriptive.moment.Variance;
 23  
 import org.apache.commons.math.stat.descriptive.rank.Max;
 24  
 import org.apache.commons.math.stat.descriptive.rank.Min;
 25  
 import org.apache.commons.math.stat.descriptive.summary.Sum;
 26  
 import org.apache.commons.math.stat.descriptive.summary.SumOfLogs;
 27  
 import org.apache.commons.math.stat.descriptive.summary.SumOfSquares;
 28  
 
 29  
 /**
 30  
  * Provides a default {@link SummaryStatistics} implementation.
 31  
  *
 32  
  * @version $Revision$ $Date: 2005-02-26 05:11:52 -0800 (Sat, 26 Feb 2005) $  
 33  
  */
 34  
 public class SummaryStatisticsImpl extends SummaryStatistics implements Serializable {
 35  
 
 36  
     /** Serializable version identifier */
 37  
     static final long serialVersionUID = 8787174276883311692L;
 38  
 
 39  
     /** count of values that have been added */
 40  10168
     protected long n = 0;
 41  
     
 42  
     /** SecondMoment is used to compute the mean and variance */
 43  10168
     protected SecondMoment secondMoment = null;
 44  
     
 45  
     /** sum of values that have been added */
 46  10168
     protected Sum sum = null;
 47  
 
 48  
     /** sum of the square of each value that has been added */
 49  10168
     protected SumOfSquares sumsq = null;
 50  
 
 51  
     /** min of values that have been added */
 52  10168
     protected Min min = null;
 53  
 
 54  
     /** max of values that have been added */
 55  10168
     protected Max max = null;
 56  
 
 57  
     /** sumLog of values that have been added */
 58  10168
     protected SumOfLogs sumLog = null;
 59  
 
 60  
     /** geoMean of values that have been added */
 61  10168
     protected GeometricMean geoMean = null;
 62  
 
 63  
     /** mean of values that have been added */
 64  10168
     protected Mean mean = null;
 65  
 
 66  
     /** variance of values that have been added */
 67  10168
     protected Variance variance = null;
 68  
 
 69  
     /**
 70  
      * Construct a SummaryStatistics
 71  
      */
 72  10168
     public SummaryStatisticsImpl() {
 73  10168
         sum = new Sum();
 74  10168
         sumsq = new SumOfSquares();
 75  10168
         min = new Min();
 76  10168
         max = new Max();
 77  10168
         sumLog = new SumOfLogs();
 78  10168
         geoMean = new GeometricMean();
 79  10168
         secondMoment = new SecondMoment();
 80  10168
     }
 81  
 
 82  
     /**
 83  
      * Add a value to the data
 84  
      * 
 85  
      * @param value  the value to add
 86  
      */
 87  
     public void addValue(double value) {
 88  131406
         sum.increment(value);
 89  131406
         sumsq.increment(value);
 90  131406
         min.increment(value);
 91  131406
         max.increment(value);
 92  131406
         sumLog.increment(value);
 93  131406
         geoMean.increment(value);
 94  131406
         secondMoment.increment(value);
 95  131406
         n++;
 96  131406
     }
 97  
 
 98  
     /** 
 99  
      * Returns the number of available values
 100  
      * @return The number of available values
 101  
      */
 102  
     public long getN() {
 103  97714
         return n;
 104  
     }
 105  
 
 106  
     /**
 107  
      * Returns the sum of the values that have been added to Univariate.
 108  
      * @return The sum or Double.NaN if no values have been added
 109  
      */
 110  
     public double getSum() {
 111  488
         return sum.getResult();
 112  
     }
 113  
 
 114  
     /**
 115  
      * Returns the sum of the squares of the values that have been added.
 116  
      * <p>
 117  
      *  Double.NaN is returned if no values have been added.</p>
 118  
      * 
 119  
      * @return The sum of squares
 120  
      */
 121  
     public double getSumsq() {
 122  462
         return sumsq.getResult();
 123  
     }
 124  
 
 125  
     /**
 126  
      * Returns the mean of the values that have been added.
 127  
      * <p>
 128  
      *  Double.NaN is returned if no values have been added.</p>
 129  
      * 
 130  
      * @return the mean
 131  
      */
 132  
     public double getMean() {
 133  16612
       return new Mean(secondMoment).getResult();
 134  
     }
 135  
 
 136  
     /**
 137  
      * Returns the standard deviation of the values that have been added.
 138  
      * <p>
 139  
      *  Double.NaN is returned if no values have been added.</p>
 140  
      * 
 141  
      * @return the standard deviation
 142  
      */
 143  
     public double getStandardDeviation() {
 144  30522
         double stdDev = Double.NaN;
 145  30522
         if (getN() > 0) {
 146  30514
             if (getN() > 1) {
 147  29004
                 stdDev = Math.sqrt(getVariance());
 148  
             } else {
 149  1510
                 stdDev = 0.0;
 150  
             }
 151  
         }
 152  30522
         return (stdDev);
 153  
     }
 154  
 
 155  
     /**
 156  
      * Returns the variance of the values that have been added.
 157  
      * <p>
 158  
      *  Double.NaN is returned if no values have been added.</p>
 159  
      *
 160  
      * @return the variance 
 161  
      */
 162  
     public double getVariance() {
 163  29598
         return new Variance(secondMoment).getResult();
 164  
     }
 165  
 
 166  
     /**
 167  
      * Returns the maximum of the values that have been added.
 168  
      * <p>
 169  
      *  Double.NaN is returned if no values have been added.</p>
 170  
      *
 171  
      * @return the maximum  
 172  
      */
 173  
     public double getMax() {
 174  512
         return max.getResult();
 175  
     }
 176  
 
 177  
     /**
 178  
      * Returns the minimum of the values that have been added.
 179  
      * <p>
 180  
      *  Double.NaN is returned if no values have been added.</p>
 181  
      *
 182  
      * @return the minimum  
 183  
      */
 184  
     public double getMin() {
 185  514
         return min.getResult();
 186  
     }
 187  
 
 188  
     /**
 189  
      * Returns the geometric mean of the values that have been added.
 190  
      * <p>
 191  
      *  Double.NaN is returned if no values have been added.</p>
 192  
      *
 193  
      * @return the geometric mean  
 194  
      */
 195  
     public double getGeometricMean() {
 196  506
         return geoMean.getResult();
 197  
     }
 198  
     
 199  
     /**
 200  
      * Generates a text report displaying
 201  
      * summary statistics from values that
 202  
      * have been added.
 203  
      * @return String with line feeds displaying statistics
 204  
      */
 205  
     public String toString() {
 206  0
         StringBuffer outBuffer = new StringBuffer();
 207  0
         outBuffer.append("SummaryStatistics:\n");
 208  0
         outBuffer.append("n: " + getN() + "\n");
 209  0
         outBuffer.append("min: " + getMin() + "\n");
 210  0
         outBuffer.append("max: " + getMax() + "\n");
 211  0
         outBuffer.append("mean: " + getMean() + "\n");
 212  0
         outBuffer.append("geometric mean: " + getGeometricMean() + "\n");
 213  0
         outBuffer.append("variance: " + getVariance() + "\n");
 214  0
         outBuffer.append("sum of squares: " + getSumsq() + "\n");
 215  0
         outBuffer.append("standard deviation: " + getStandardDeviation() + "\n");
 216  0
         return outBuffer.toString();
 217  
     }
 218  
 
 219  
     /** 
 220  
      * Resets all statistics and storage
 221  
      */
 222  
     public void clear() {
 223  16
         this.n = 0;
 224  16
         min.clear();
 225  16
         max.clear();
 226  16
         sum.clear();
 227  16
         sumLog.clear();
 228  16
         sumsq.clear();
 229  16
         geoMean.clear();
 230  16
         secondMoment.clear();
 231  16
     }
 232  
 
 233  
 }