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: https://sites.google.com/site/cmzmasek/home/software/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() {
48 public BasicDescriptiveStatistics( final String desc ) {
50 setDescription( desc );
54 public void addValue( final double d ) {
57 _data.add( new Double( d ) );
67 public double arithmeticMean() {
69 return getSum() / getN();
73 public String asSummary() {
75 return arithmeticMean() + DescriptiveStatistics.PLUS_MINUS + sampleStandardDeviation() + " [" + getMin()
76 + "..." + getMax() + "]";
79 return "" + arithmeticMean();
84 public double coefficientOfVariation() {
86 return ( sampleStandardDeviation() / arithmeticMean() );
90 public List<Double> getData() {
95 public double[] getDataAsDoubleArray() {
97 final double[] data_array = new double[ getN() ];
98 for( int i = 0; i < getN(); ++i ) {
99 data_array[ i ] = getValue( i );
105 public String getDescription() {
110 public double getMax() {
116 public double getMin() {
127 public double getSum() {
133 public String getSummaryAsString() {
135 final double mean = arithmeticMean();
136 final double sd = sampleStandardDeviation();
137 return "" + mean + ( ( char ) 177 ) + sd + " [" + getN() + "] [" + getMin() + "-" + getMax() + "]";
141 public double getValue( final int index ) {
143 return ( ( ( _data.get( index ) ) ).doubleValue() );
147 public double median() {
151 median = getValue( 0 );
154 final int index = ( getN() / 2 );
155 final double[] data_array = getDataAsDoubleArray();
156 Arrays.sort( data_array );
157 if ( ( ( data_array.length ) % 2 ) == 0 ) {
158 // even number of data values
159 median = ( data_array[ index - 1 ] + data_array[ index ] ) / 2.0;
162 median = data_array[ index ];
169 public double midrange() {
171 return ( _min + _max ) / 2.0;
175 public double pearsonianSkewness() {
177 final double mean = arithmeticMean();
178 final double median = median();
179 final double sd = sampleStandardDeviation();
180 return ( ( 3 * ( mean - median ) ) / sd );
184 public double sampleStandardDeviation() {
185 return Math.sqrt( sampleVariance() );
189 public double sampleStandardUnit( final double value ) {
191 return BasicDescriptiveStatistics.sampleStandardUnit( value, arithmeticMean(), sampleStandardDeviation() );
195 public double sampleVariance() {
200 return ( sumDeviations() / ( getN() - 1 ) );
204 public void setDescription( final String desc ) {
209 public double standardErrorOfMean() {
211 return ( sampleStandardDeviation() / Math.sqrt( getN() ) );
215 public double sumDeviations() {
217 if ( _recalc_sigma ) {
218 _recalc_sigma = false;
220 final double mean = arithmeticMean();
221 for( int i = 0; i < getN(); ++i ) {
222 _sigma += Math.pow( ( getValue( i ) - mean ), 2 );
229 public String toString() {
231 return "empty data set statistics";
233 final StringBuffer sb = new StringBuffer();
234 sb.append( "Descriptive statistics:" );
235 sb.append( ForesterUtil.getLineSeparator() );
236 sb.append( "n : " + getN() );
238 sb.append( ForesterUtil.getLineSeparator() );
239 sb.append( "min : " + getMin() );
240 sb.append( ForesterUtil.getLineSeparator() );
241 sb.append( "max : " + getMax() );
242 sb.append( ForesterUtil.getLineSeparator() );
243 sb.append( "midrange : " + midrange() );
244 sb.append( ForesterUtil.getLineSeparator() );
245 sb.append( "median : " + median() );
246 sb.append( ForesterUtil.getLineSeparator() );
247 sb.append( "mean : " + arithmeticMean() );
248 sb.append( ForesterUtil.getLineSeparator() );
249 sb.append( "sd : " + sampleStandardDeviation() );
250 sb.append( ForesterUtil.getLineSeparator() );
251 sb.append( "variance : " + sampleVariance() );
252 sb.append( ForesterUtil.getLineSeparator() );
253 sb.append( "standard error of mean : " + standardErrorOfMean() );
254 sb.append( ForesterUtil.getLineSeparator() );
255 sb.append( "coefficient of variation: " + coefficientOfVariation() );
256 sb.append( ForesterUtil.getLineSeparator() );
257 sb.append( "pearsonian skewness : " + pearsonianSkewness() );
259 return sb.toString();
262 private void init() {
263 _data = new ArrayList<Double>();
265 _min = Double.MAX_VALUE;
266 _max = -Double.MAX_VALUE;
268 _recalc_sigma = true;
272 private void validate() throws ArithmeticException {
274 throw new ArithmeticException( "attempt to get a result from empty data set statistics" );
278 public static int[] performBinning( final double[] values,
281 final int number_of_bins ) {
283 throw new IllegalArgumentException( "min [" + min + "] is larger than or equal to max [" + max + "]" );
285 if ( number_of_bins < 3 ) {
286 throw new IllegalArgumentException( "number of bins is smaller than 3" );
288 final int[] bins = new int[ number_of_bins ];
289 final double binning_factor = number_of_bins / ( max - min );
290 final int last_index = number_of_bins - 1;
291 for( final double d : values ) {
292 if ( !( ( d > max ) || ( d < min ) ) ) {
293 final int bin = ( int ) ( ( d - min ) * binning_factor );
294 if ( bin > last_index ) {
295 ++bins[ last_index ];
306 * Computes the sample standard unit (z-score). Used to compute 'value' in
307 * terms of standard units. Note that 'value', 'mean' and 'sd' must be all
308 * from the same sample data.
311 * a double in the sample for which
313 * the mean of the sample.
315 * The standard deviation of the sample.
316 * @return 'value' in terms of standard units
318 public static double sampleStandardUnit( final double value, final double mean, final double sd ) {
319 return ( value - mean ) / sd;