001 /*
002 * Created on Aug 31, 2007
003 *
004 */
005 package com.x8ing.mc;
006
007 /**
008 * @author Patrick Heusser
009 */
010 public class Statistic {
011
012 private double[] values = null;
013
014 private Statistic() {
015
016 }
017
018 public Statistic(double values[]) {
019 this.values = values;
020 }
021
022 public ResultDistributionEntry[] getDistribution(int numberOfSegments) {
023
024 double[] extrema = getMinimumAndMaximum();
025 double spread = extrema[1] - extrema[0];
026
027 double slotWidth = spread / (numberOfSegments - 1);
028
029 ResultDistributionEntry entries[] = new ResultDistributionEntry[numberOfSegments];
030
031 // init array
032 for (int i = 0; i < numberOfSegments; i++) {
033
034 ResultDistributionEntry resultDistributionEntry = new ResultDistributionEntry();
035 entries[i] = resultDistributionEntry;
036
037 resultDistributionEntry.numberOfEntries = 0;
038 resultDistributionEntry.rangeFrom = extrema[0] + i * slotWidth;
039 resultDistributionEntry.rangeTo = extrema[0] + (i + 1) * slotWidth;
040
041 }
042
043 // count entries
044 for (int i = 0; i < values.length; i++) {
045 double value = values[i];
046
047 int entrySlot = (int) ((value - extrema[0]) / slotWidth);
048
049 ResultDistributionEntry entry = entries[entrySlot];
050
051 entry.numberOfEntries++;
052 entry.percentile = ((double) entry.numberOfEntries) * 100 / values.length;
053
054 }
055
056 return entries;
057 }
058
059 public String getDistributionPrint(int numberOfSegments) {
060
061 StringBuffer sb = new StringBuffer();
062
063 ResultDistributionEntry[] entries = getDistribution(numberOfSegments);
064
065 // write header
066 sb.append("rangeFrom \trangeTo \tcount \tpercentil \n");
067
068 // write entries
069 for (int i = 0; i < entries.length; i++) {
070 ResultDistributionEntry entry = entries[i];
071 sb.append(entry.rangeFrom).append("\t").append(entry.rangeTo).append("\t").append(entry.numberOfEntries).append("\t")
072 .append(entry.percentile).append("\n");
073 }
074
075 return sb.toString();
076
077 }
078
079 public double getAverage() {
080
081 double sum = 0;
082
083 for (int i = 0; i < values.length; i++) {
084 sum += values[i];
085
086 }
087
088 return sum / values.length;
089 }
090
091 /**
092 *
093 * @return an double array: double[0] is minimum, double[1] is maximum.
094 */
095 public double[] getMinimumAndMaximum() {
096
097 double[] extrema = new double[2];
098
099 extrema[0] = Double.MAX_VALUE;
100 extrema[1] = Double.MIN_VALUE;
101
102 for (int i = 0; i < values.length; i++) {
103
104 double v = values[i];
105
106 // check for new minima
107 if (v < extrema[0]) {
108 extrema[0] = v;
109 }
110
111 // check for new maxima
112 if (v > extrema[1]) {
113 extrema[1] = v;
114 }
115
116 }
117
118 return extrema;
119
120 }
121
122 public static class ResultDistributionEntry {
123
124 public double rangeFrom = 0;
125
126 public double rangeTo = 0;
127
128 public int numberOfEntries = 0;
129
130 public double percentile = 0;
131
132 }
133
134 }
|