001 package jp.osdl.jbento2.analyzer;
002
003 import java.math.BigDecimal;
004 import java.math.BigInteger;
005 import java.util.ArrayList;
006 import java.util.Collections;
007 import java.util.Iterator;
008
009 public class Stats {
010
011 private ArrayList values = new ArrayList();
012
013 public void add(long l) {
014 values.add(new Long(l));
015 }
016
017 public int count() {
018 return values.size();
019 }
020
021 public BigInteger total() {
022 BigInteger result = new BigInteger("0");
023 for (Iterator it = values.iterator(); it.hasNext(); ) {
024 result = result.add(new BigInteger(String.valueOf(it.next())));
025 }
026 return result;
027 }
028
029 public long min() {
030 ArrayList clone = (ArrayList)values.clone();
031 Collections.sort(clone);
032 Long result = (Long)clone.get(0);
033 return result.longValue();
034 }
035
036 public long max() {
037 ArrayList clone = (ArrayList)values.clone();
038 Collections.sort(clone);
039 Long result = (Long)clone.get(clone.size() - 1);
040 return result.longValue();
041 }
042
043 public double average() {
044 if (count() == 0) {
045 return 0.0d;
046 }
047 return
048 new BigDecimal(total()).divide(
049 new BigDecimal(count()), 3, BigDecimal.ROUND_HALF_UP
050 ).doubleValue();
051 }
052
053 public long percentile(int percent) {
054 ArrayList clone = (ArrayList)values.clone();
055 Collections.sort(clone);
056 Long result = (Long)clone.get(count() * percent / 100);
057 return result.longValue();
058 }
059
060 public double variance() {
061 double average = average();
062 double total = 0;
063 for (Iterator it = values.iterator(); it.hasNext(); ) {
064 total += Math.pow(((Long)it.next()).longValue() - average, 2);
065 }
066 return total / count();
067 }
068
069 public double sd() {
070 return Math.sqrt(variance());
071 }
072
073 }