4 // FORESTER -- software libraries and applications
5 // for evolutionary biology research and applications.
7 // Copyright (C) 2008-2009 Christian M. Zmasek
8 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 // Contact: phylosoft @ gmail . com
26 // WWW: www.phylosoft.org/forester
28 package org.forester.util;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
34 public class BasicDescriptiveStatistics implements DescriptiveStatistics {
36 private List<Double> _data;
40 private double _sigma;
41 private boolean _recalc_sigma;
43 public BasicDescriptiveStatistics() {
48 * @see org.forester.util.DescriptiveStatisticsI#addValue(double)
51 public void addValue( final double d ) {
54 _data.add( new Double( d ) );
64 * @see org.forester.util.DescriptiveStatisticsI#arithmeticMean()
67 public double arithmeticMean() {
69 return getSum() / getN();
73 * @see org.forester.util.DescriptiveStatisticsI#asSummary()
76 public String asSummary() {
78 return arithmeticMean() + DescriptiveStatistics.PLUS_MINUS + sampleStandardDeviation() + " [" + getMin()
79 + "..." + getMax() + "]";
82 return "" + arithmeticMean();
87 * @see org.forester.util.DescriptiveStatisticsI#coefficientOfVariation()
90 public double coefficientOfVariation() {
92 return ( sampleStandardDeviation() / arithmeticMean() );
96 * @see org.forester.util.DescriptiveStatisticsI#getDataAsDoubleArray()
99 public double[] getDataAsDoubleArray() {
101 final double[] data_array = new double[ getN() ];
102 for( int i = 0; i < getN(); ++i ) {
103 data_array[ i ] = getValue( i );
109 * @see org.forester.util.DescriptiveStatisticsI#getMax()
112 public double getMax() {
118 * @see org.forester.util.DescriptiveStatisticsI#getMin()
121 public double getMin() {
127 * @see org.forester.util.DescriptiveStatisticsI#getN()
135 * @see org.forester.util.DescriptiveStatisticsI#getSum()
138 public double getSum() {
144 * @see org.forester.util.DescriptiveStatisticsI#getSummaryAsString()
147 public String getSummaryAsString() {
149 final double mean = arithmeticMean();
150 final double sd = sampleStandardDeviation();
151 return "" + mean + ( ( char ) 177 ) + sd + " [" + getMin() + "..." + getMax() + "]";
155 * @see org.forester.util.DescriptiveStatisticsI#getValue(int)
158 public double getValue( final int index ) {
160 return ( ( ( _data.get( index ) ) ).doubleValue() );
163 private void init() {
164 _data = new ArrayList<Double>();
166 _min = Double.MAX_VALUE;
167 _max = -Double.MAX_VALUE;
169 _recalc_sigma = true;
173 * @see org.forester.util.DescriptiveStatisticsI#median()
176 public double median() {
180 median = getValue( 0 );
183 final int index = ( getN() / 2 );
184 final double[] data_array = getDataAsDoubleArray();
185 Arrays.sort( data_array );
186 if ( ( ( data_array.length ) % 2 ) == 0 ) {
187 // even number of data values
188 median = ( data_array[ index - 1 ] + data_array[ index ] ) / 2.0;
191 median = data_array[ index ];
198 * @see org.forester.util.DescriptiveStatisticsI#midrange()
201 public double midrange() {
203 return ( _min + _max ) / 2.0;
207 * @see org.forester.util.DescriptiveStatisticsI#pearsonianSkewness()
210 public double pearsonianSkewness() {
212 final double mean = arithmeticMean();
213 final double median = median();
214 final double sd = sampleStandardDeviation();
215 return ( ( 3 * ( mean - median ) ) / sd );
219 * @see org.forester.util.DescriptiveStatisticsI#sampleStandardDeviation()
222 public double sampleStandardDeviation() {
223 return Math.sqrt( sampleVariance() );
227 * @see org.forester.util.DescriptiveStatisticsI#sampleStandardUnit(double)
230 public double sampleStandardUnit( final double value ) {
232 return BasicDescriptiveStatistics.sampleStandardUnit( value, arithmeticMean(), sampleStandardDeviation() );
236 * @see org.forester.util.DescriptiveStatisticsI#sampleVariance()
239 public double sampleVariance() {
242 throw new ArithmeticException( "attempt to calculate sample variance for less then two values" );
244 return ( sumDeviations() / ( getN() - 1 ) );
248 * @see org.forester.util.DescriptiveStatisticsI#standardErrorOfMean()
251 public double standardErrorOfMean() {
253 return ( sampleStandardDeviation() / Math.sqrt( getN() ) );
257 * @see org.forester.util.DescriptiveStatisticsI#sumDeviations()
260 public double sumDeviations() {
262 if ( _recalc_sigma ) {
263 _recalc_sigma = false;
265 final double mean = arithmeticMean();
266 for( int i = 0; i < getN(); ++i ) {
267 _sigma += Math.pow( ( getValue( i ) - mean ), 2 );
274 * @see org.forester.util.DescriptiveStatisticsI#toString()
277 public String toString() {
279 return "empty data set statistics";
281 final StringBuffer sb = new StringBuffer();
282 sb.append( "Descriptive statistics:" );
283 sb.append( ForesterUtil.getLineSeparator() );
284 sb.append( "n : " + getN() );
286 sb.append( ForesterUtil.getLineSeparator() );
287 sb.append( "min : " + getMin() );
288 sb.append( ForesterUtil.getLineSeparator() );
289 sb.append( "max : " + getMax() );
290 sb.append( ForesterUtil.getLineSeparator() );
291 sb.append( "midrange : " + midrange() );
292 sb.append( ForesterUtil.getLineSeparator() );
293 sb.append( "median : " + median() );
294 sb.append( ForesterUtil.getLineSeparator() );
295 sb.append( "mean : " + arithmeticMean() );
296 sb.append( ForesterUtil.getLineSeparator() );
297 sb.append( "sd : " + sampleStandardDeviation() );
298 sb.append( ForesterUtil.getLineSeparator() );
299 sb.append( "variance : " + sampleVariance() );
300 sb.append( ForesterUtil.getLineSeparator() );
301 sb.append( "standard error of mean : " + standardErrorOfMean() );
302 sb.append( ForesterUtil.getLineSeparator() );
303 sb.append( "coefficient of variation: " + coefficientOfVariation() );
304 sb.append( ForesterUtil.getLineSeparator() );
305 sb.append( "pearsonian skewness : " + pearsonianSkewness() );
307 return sb.toString();
310 private void validate() throws ArithmeticException {
312 throw new ArithmeticException( "attempt to get a result from empty data set statistics" );
316 public static int[] performBinning( final double[] values,
319 final int number_of_bins ) {
321 throw new IllegalArgumentException( "min [" + min + "] is larger than or equal to max [" + max + "]" );
323 if ( number_of_bins < 3 ) {
324 throw new IllegalArgumentException( "number of bins is smaller than 3" );
326 final int[] bins = new int[ number_of_bins ];
327 final double binning_factor = number_of_bins / ( max - min );
328 final int last_index = number_of_bins - 1;
329 for( final double d : values ) {
330 if ( !( ( d > max ) || ( d < min ) ) ) {
331 final int bin = ( int ) ( ( d - min ) * binning_factor );
332 if ( bin > last_index ) {
333 ++bins[ last_index ];
344 * Computes the sample standard unit (z-score). Used to compute 'value' in
345 * terms of standard units. Note that 'value', 'mean' and 'sd' must be all
346 * from the same sample data.
349 * a double in the sample for which
351 * the mean of the sample.
353 * The standard deviation of the sample.
354 * @return 'value' in terms of standard units
356 public static double sampleStandardUnit( final double value, final double mean, final double sd ) {
357 return ( value - mean ) / sd;