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;
44 public BasicDescriptiveStatistics() {
49 * @see org.forester.util.DescriptiveStatisticsI#addValue(double)
52 public void addValue( final double d ) {
55 _data.add( new Double( d ) );
65 * @see org.forester.util.DescriptiveStatisticsI#arithmeticMean()
68 public double arithmeticMean() {
70 return getSum() / getN();
74 * @see org.forester.util.DescriptiveStatisticsI#asSummary()
77 public String asSummary() {
79 return arithmeticMean() + DescriptiveStatistics.PLUS_MINUS + sampleStandardDeviation() + " [" + getMin()
80 + "..." + getMax() + "]";
83 return "" + arithmeticMean();
88 * @see org.forester.util.DescriptiveStatisticsI#coefficientOfVariation()
91 public double coefficientOfVariation() {
93 return ( sampleStandardDeviation() / arithmeticMean() );
97 * @see org.forester.util.DescriptiveStatisticsI#getDataAsDoubleArray()
100 public double[] getDataAsDoubleArray() {
102 final double[] data_array = new double[ getN() ];
103 for( int i = 0; i < getN(); ++i ) {
104 data_array[ i ] = getValue( i );
110 * @see org.forester.util.DescriptiveStatisticsI#getMax()
113 public double getMax() {
119 * @see org.forester.util.DescriptiveStatisticsI#getMin()
122 public double getMin() {
128 * @see org.forester.util.DescriptiveStatisticsI#getN()
136 * @see org.forester.util.DescriptiveStatisticsI#getSum()
139 public double getSum() {
145 * @see org.forester.util.DescriptiveStatisticsI#getSummaryAsString()
148 public String getSummaryAsString() {
150 final double mean = arithmeticMean();
151 final double sd = sampleStandardDeviation();
152 return "" + mean + ( ( char ) 177 ) + sd + " [" + getMin() + "..." + getMax() + "]";
156 * @see org.forester.util.DescriptiveStatisticsI#getValue(int)
159 public double getValue( final int index ) {
161 return ( ( ( _data.get( index ) ) ).doubleValue() );
164 private void init() {
165 _data = new ArrayList<Double>();
167 _min = Double.MAX_VALUE;
168 _max = -Double.MAX_VALUE;
170 _recalc_sigma = true;
175 * @see org.forester.util.DescriptiveStatisticsI#median()
178 public double median() {
182 median = getValue( 0 );
185 final int index = ( getN() / 2 );
186 final double[] data_array = getDataAsDoubleArray();
187 Arrays.sort( data_array );
188 if ( ( ( data_array.length ) % 2 ) == 0 ) {
189 // even number of data values
190 median = ( data_array[ index - 1 ] + data_array[ index ] ) / 2.0;
193 median = data_array[ index ];
200 * @see org.forester.util.DescriptiveStatisticsI#midrange()
203 public double midrange() {
205 return ( _min + _max ) / 2.0;
209 * @see org.forester.util.DescriptiveStatisticsI#pearsonianSkewness()
212 public double pearsonianSkewness() {
214 final double mean = arithmeticMean();
215 final double median = median();
216 final double sd = sampleStandardDeviation();
217 return ( ( 3 * ( mean - median ) ) / sd );
221 * @see org.forester.util.DescriptiveStatisticsI#sampleStandardDeviation()
224 public double sampleStandardDeviation() {
225 return Math.sqrt( sampleVariance() );
229 * @see org.forester.util.DescriptiveStatisticsI#sampleStandardUnit(double)
232 public double sampleStandardUnit( final double value ) {
234 return BasicDescriptiveStatistics.sampleStandardUnit( value, arithmeticMean(), sampleStandardDeviation() );
238 * @see org.forester.util.DescriptiveStatisticsI#sampleVariance()
241 public double sampleVariance() {
244 throw new ArithmeticException( "attempt to calculate sample variance for less then two values" );
246 return ( sumDeviations() / ( getN() - 1 ) );
250 * @see org.forester.util.DescriptiveStatisticsI#standardErrorOfMean()
253 public double standardErrorOfMean() {
255 return ( sampleStandardDeviation() / Math.sqrt( getN() ) );
259 * @see org.forester.util.DescriptiveStatisticsI#sumDeviations()
262 public double sumDeviations() {
264 if ( _recalc_sigma ) {
265 _recalc_sigma = false;
267 final double mean = arithmeticMean();
268 for( int i = 0; i < getN(); ++i ) {
269 _sigma += Math.pow( ( getValue( i ) - mean ), 2 );
276 * @see org.forester.util.DescriptiveStatisticsI#toString()
279 public String toString() {
281 return "empty data set statistics";
283 final StringBuffer sb = new StringBuffer();
284 sb.append( "Descriptive statistics:" );
285 sb.append( ForesterUtil.getLineSeparator() );
286 sb.append( "n : " + getN() );
288 sb.append( ForesterUtil.getLineSeparator() );
289 sb.append( "min : " + getMin() );
290 sb.append( ForesterUtil.getLineSeparator() );
291 sb.append( "max : " + getMax() );
292 sb.append( ForesterUtil.getLineSeparator() );
293 sb.append( "midrange : " + midrange() );
294 sb.append( ForesterUtil.getLineSeparator() );
295 sb.append( "median : " + median() );
296 sb.append( ForesterUtil.getLineSeparator() );
297 sb.append( "mean : " + arithmeticMean() );
298 sb.append( ForesterUtil.getLineSeparator() );
299 sb.append( "sd : " + sampleStandardDeviation() );
300 sb.append( ForesterUtil.getLineSeparator() );
301 sb.append( "variance : " + sampleVariance() );
302 sb.append( ForesterUtil.getLineSeparator() );
303 sb.append( "standard error of mean : " + standardErrorOfMean() );
304 sb.append( ForesterUtil.getLineSeparator() );
305 sb.append( "coefficient of variation: " + coefficientOfVariation() );
306 sb.append( ForesterUtil.getLineSeparator() );
307 sb.append( "pearsonian skewness : " + pearsonianSkewness() );
309 return sb.toString();
312 private void validate() throws ArithmeticException {
314 throw new ArithmeticException( "attempt to get a result from empty data set statistics" );
318 public static int[] performBinning( final double[] values,
321 final int number_of_bins ) {
323 throw new IllegalArgumentException( "min [" + min + "] is larger than or equal to max [" + max + "]" );
325 if ( number_of_bins < 3 ) {
326 throw new IllegalArgumentException( "number of bins is smaller than 3" );
328 final int[] bins = new int[ number_of_bins ];
329 final double binning_factor = number_of_bins / ( max - min );
330 final int last_index = number_of_bins - 1;
331 for( final double d : values ) {
332 if ( !( ( d > max ) || ( d < min ) ) ) {
333 final int bin = ( int ) ( ( d - min ) * binning_factor );
334 if ( bin > last_index ) {
335 ++bins[ last_index ];
346 * Computes the sample standard unit (z-score). Used to compute 'value' in
347 * terms of standard units. Note that 'value', 'mean' and 'sd' must be all
348 * from the same sample data.
351 * a double in the sample for which
353 * the mean of the sample.
355 * The standard deviation of the sample.
356 * @return 'value' in terms of standard units
358 public static double sampleStandardUnit( final double value, final double mean, final double sd ) {
359 return ( value - mean ) / sd;
363 public List<Double> getData() {
368 public void setDescription( final String desc ) {
373 public String getDescription() {