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)
50 public void addValue( final double d ) {
53 _data.add( new Double( d ) );
63 * @see org.forester.util.DescriptiveStatisticsI#arithmeticMean()
65 public double arithmeticMean() {
67 return getSum() / getN();
71 * @see org.forester.util.DescriptiveStatisticsI#asSummary()
73 public String asSummary() {
75 return arithmeticMean() + DescriptiveStatistics.PLUS_MINUS + sampleStandardDeviation() + " [" + getMin()
76 + "..." + getMax() + "]";
79 return "" + arithmeticMean();
84 * @see org.forester.util.DescriptiveStatisticsI#coefficientOfVariation()
86 public double coefficientOfVariation() {
88 return ( sampleStandardDeviation() / arithmeticMean() );
92 * @see org.forester.util.DescriptiveStatisticsI#getDataAsDoubleArray()
94 public double[] getDataAsDoubleArray() {
96 final double[] data_array = new double[ getN() ];
97 for( int i = 0; i < getN(); ++i ) {
98 data_array[ i ] = getValue( i );
104 * @see org.forester.util.DescriptiveStatisticsI#getMax()
106 public double getMax() {
112 * @see org.forester.util.DescriptiveStatisticsI#getMin()
114 public double getMin() {
120 * @see org.forester.util.DescriptiveStatisticsI#getN()
127 * @see org.forester.util.DescriptiveStatisticsI#getSum()
129 public double getSum() {
135 * @see org.forester.util.DescriptiveStatisticsI#getSummaryAsString()
137 public String getSummaryAsString() {
139 final double mean = arithmeticMean();
140 final double sd = sampleStandardDeviation();
141 return "" + mean + ( ( char ) 177 ) + sd + " [" + getMin() + "..." + getMax() + "]";
145 * @see org.forester.util.DescriptiveStatisticsI#getValue(int)
147 public double getValue( final int index ) {
149 return ( ( ( _data.get( index ) ) ).doubleValue() );
152 private void init() {
153 _data = new ArrayList<Double>();
155 _min = Double.MAX_VALUE;
156 _max = -Double.MAX_VALUE;
158 _recalc_sigma = true;
162 * @see org.forester.util.DescriptiveStatisticsI#median()
164 public double median() {
168 median = getValue( 0 );
171 final int index = ( getN() / 2 );
172 final double[] data_array = getDataAsDoubleArray();
173 Arrays.sort( data_array );
174 if ( ( ( data_array.length ) % 2 ) == 0 ) {
175 // even number of data values
176 median = ( data_array[ index - 1 ] + data_array[ index ] ) / 2.0;
179 median = data_array[ index ];
186 * @see org.forester.util.DescriptiveStatisticsI#midrange()
188 public double midrange() {
190 return ( _min + _max ) / 2.0;
194 * @see org.forester.util.DescriptiveStatisticsI#pearsonianSkewness()
196 public double pearsonianSkewness() {
198 final double mean = arithmeticMean();
199 final double median = median();
200 final double sd = sampleStandardDeviation();
201 return ( ( 3 * ( mean - median ) ) / sd );
205 * @see org.forester.util.DescriptiveStatisticsI#sampleStandardDeviation()
207 public double sampleStandardDeviation() {
208 return Math.sqrt( sampleVariance() );
212 * @see org.forester.util.DescriptiveStatisticsI#sampleStandardUnit(double)
214 public double sampleStandardUnit( final double value ) {
216 return BasicDescriptiveStatistics.sampleStandardUnit( value, arithmeticMean(), sampleStandardDeviation() );
220 * @see org.forester.util.DescriptiveStatisticsI#sampleVariance()
222 public double sampleVariance() {
225 throw new ArithmeticException( "attempt to calculate sample variance for less then two values" );
227 return ( sumDeviations() / ( getN() - 1 ) );
231 * @see org.forester.util.DescriptiveStatisticsI#standardErrorOfMean()
233 public double standardErrorOfMean() {
235 return ( sampleStandardDeviation() / Math.sqrt( getN() ) );
239 * @see org.forester.util.DescriptiveStatisticsI#sumDeviations()
241 public double sumDeviations() {
243 if ( _recalc_sigma ) {
244 _recalc_sigma = false;
246 final double mean = arithmeticMean();
247 for( int i = 0; i < getN(); ++i ) {
248 _sigma += Math.pow( ( getValue( i ) - mean ), 2 );
255 * @see org.forester.util.DescriptiveStatisticsI#toString()
258 public String toString() {
260 return "empty data set statistics";
262 final StringBuffer sb = new StringBuffer();
263 sb.append( "Descriptive statistics:" );
264 sb.append( ForesterUtil.getLineSeparator() );
265 sb.append( "n : " + getN() );
267 sb.append( ForesterUtil.getLineSeparator() );
268 sb.append( "min : " + getMin() );
269 sb.append( ForesterUtil.getLineSeparator() );
270 sb.append( "max : " + getMax() );
271 sb.append( ForesterUtil.getLineSeparator() );
272 sb.append( "midrange : " + midrange() );
273 sb.append( ForesterUtil.getLineSeparator() );
274 sb.append( "median : " + median() );
275 sb.append( ForesterUtil.getLineSeparator() );
276 sb.append( "mean : " + arithmeticMean() );
277 sb.append( ForesterUtil.getLineSeparator() );
278 sb.append( "sd : " + sampleStandardDeviation() );
279 sb.append( ForesterUtil.getLineSeparator() );
280 sb.append( "variance : " + sampleVariance() );
281 sb.append( ForesterUtil.getLineSeparator() );
282 sb.append( "standard error of mean : " + standardErrorOfMean() );
283 sb.append( ForesterUtil.getLineSeparator() );
284 sb.append( "coefficient of variation: " + coefficientOfVariation() );
285 sb.append( ForesterUtil.getLineSeparator() );
286 sb.append( "pearsonian skewness : " + pearsonianSkewness() );
288 return sb.toString();
291 private void validate() throws ArithmeticException {
293 throw new ArithmeticException( "attempt to get a result from empty data set statistics" );
297 public static int[] performBinning( final double[] values,
300 final int number_of_bins ) {
302 throw new IllegalArgumentException( "min [" + min + "] is larger than or equal to max [" + max + "]" );
304 if ( number_of_bins < 3 ) {
305 throw new IllegalArgumentException( "number of bins is smaller than 3" );
307 final int[] bins = new int[ number_of_bins ];
308 final double binning_factor = number_of_bins / ( max - min );
309 final int last_index = number_of_bins - 1;
310 for( final double d : values ) {
311 if ( !( ( d > max ) || ( d < min ) ) ) {
312 final int bin = ( int ) ( ( d - min ) * binning_factor );
313 if ( bin > last_index ) {
314 ++bins[ last_index ];
325 * Computes the sample standard unit (z-score). Used to compute 'value' in
326 * terms of standard units. Note that 'value', 'mean' and 'sd' must be all
327 * from the same sample data.
330 * a double in the sample for which
332 * the mean of the sample.
334 * The standard deviation of the sample.
335 * @return 'value' in terms of standard units
337 public static double sampleStandardUnit( final double value, final double mean, final double sd ) {
338 return ( value - mean ) / sd;