View Javadoc

1   /*
2    * Copyright 2010 Fraunhofer Gesellschaft, Munich, Germany,
3    * for its Fraunhofer Institute for Computer Architecture and Software
4    * Technology (FIRST), Berlin, Germany. All rights reserved.
5    * http://www.first.fraunhofer.de/
6    */
7   package net.kwfgrid.gwes.workflowanalyzer;
8   
9   import org.apache.log4j.Logger;
10  
11  /**
12   * @author Andreas Hoheisel
13   *         (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
14   * @author Dietmar Sommerfeld / GWDG 
15   * @version $Id: DoubleDistribution.java 1540 2011-08-17 13:30:37Z hoheisel $
16   */
17  public class DoubleDistribution {
18  
19      static Logger logger = Logger.getLogger(DoubleDistribution.class);
20  
21      private long number;
22      private double mean;
23      private double sumSquare;
24      private double stdDeviation;
25      private double expSmooth;
26      private double min;
27      private double max;
28  
29      public DoubleDistribution() {
30          this.number = 0;
31          this.mean = 0f;
32          this.sumSquare = 0f;
33          this.stdDeviation = 0f;
34          this.expSmooth = 0f;
35          this.min = Float.MAX_VALUE;
36          this.max = Float.MIN_VALUE;
37      }
38  
39      public DoubleDistribution(long number, double mean, double stdDeviation, double expSmooth, double min, double max) {
40          this.number = number;
41          this.mean = mean;
42          this.stdDeviation = stdDeviation;
43          this.expSmooth = expSmooth;
44          this.min = min;
45          this.max = max;
46          this.sumSquare = (number - 1) * stdDeviation * stdDeviation + number * mean * mean;
47      }
48  
49      public synchronized void put(double value) {
50          if (logger.isDebugEnabled()) logger.debug("put: number=" + number + " expSmooth=" + expSmooth + " put value=" + value);
51          if (number == 0) {   // first insert
52  		    expSmooth = value;
53          } else {
54  		    expSmooth = 0.3d * value + 0.7d * expSmooth;
55          }
56  
57          if (value < min) min = value;
58          if (value > max) max = value;
59          mean = (number * mean + value) / (number + 1);
60          sumSquare += value*value;
61          number++;
62          stdDeviation = (number == 1) ? 0d : Math.sqrt( (sumSquare - number * mean * mean ) / (number - 1) );
63      }
64  
65      public synchronized void add(DoubleDistribution distribution) {
66          if (logger.isDebugEnabled()) logger.debug("add: number=" + number + " expSmooth=" + expSmooth + " add expSmooth=" + distribution.expSmooth);
67          if (number == 0) {   // first insert
68          	expSmooth = distribution.expSmooth;
69          } else {
70          	expSmooth = 0.3d * distribution.expSmooth + 0.7d * expSmooth;
71          }
72  
73          long newNumber = number + distribution.number;
74          double newMean = (number * mean + distribution.number * distribution.mean) / newNumber;
75          double newSumSquare = sumSquare + distribution.sumSquare;
76          double newStdDeviation = (newNumber == 1) ? 0d : Math.sqrt( (newSumSquare - newNumber * newMean * newMean ) / (newNumber - 1) );
77          number = newNumber;
78          mean = newMean;
79          sumSquare = newSumSquare;
80          stdDeviation = newStdDeviation;
81          if (distribution.min < min) min = distribution.min;
82          if (distribution.max > max) max = distribution.max;
83      }
84  
85      public long getNumber() {
86          return number;
87      }
88  
89      public double getMean() {
90          return mean;
91      }
92  
93      public double getSumSquare() {
94          return sumSquare;
95      }
96  
97      public double getStdDeviation() {
98          return stdDeviation;
99      }
100 
101     public double getExpSmooth() {
102         return expSmooth;
103     }
104 
105     public double getMin() {
106         return min;
107     }
108 
109     public double getMax() {
110         return max;
111     }
112 
113     public String toString() {
114         StringBuffer ret = new StringBuffer("DoubleDistribution(number=");
115         ret.append(number);
116         ret.append(",mean=").append(mean);
117         ret.append(",stdDeviation=").append(stdDeviation);
118         ret.append(",expSmooth=").append(expSmooth);
119         ret.append(",min=").append(min);
120         ret.append(",max=").append(max);
121         ret.append(")");
122         return ret.toString();
123     }
124 
125     @Override
126     public Object clone() {
127         return new DoubleDistribution(number,mean,stdDeviation,expSmooth,min,max);
128     }
129 
130 
131     @Override
132     public boolean equals(Object o) {
133         if (this == o) return true;
134         if (o == null || getClass() != o.getClass()) return false;
135 
136         DoubleDistribution that = (DoubleDistribution) o;
137 
138         if (Double.compare(that.max, max) != 0) return false;
139         if (Double.compare(that.mean, mean) != 0) return false;
140         if (Double.compare(that.min, min) != 0) return false;
141         if (number != that.number) return false;
142         if (Double.compare(that.stdDeviation, stdDeviation) != 0) return false;
143         if (Double.compare(that.expSmooth, expSmooth) != 0) return false;
144 
145         return true;
146     }
147 
148     @Override
149     public int hashCode() {
150         int result;
151         long temp;
152         result = (int) (number ^ (number >>> 32));
153         temp = mean != +0.0d ? Double.doubleToLongBits(mean) : 0L;
154         result = 31 * result + (int) (temp ^ (temp >>> 32));
155         temp = stdDeviation != +0.0d ? Double.doubleToLongBits(stdDeviation) : 0L;
156         result = 31 * result + (int) (temp ^ (temp >>> 32));
157         temp = expSmooth != +0.0d ? Double.doubleToLongBits(expSmooth) : 0L;
158         result = 31 * result + (int) (temp ^ (temp >>> 32));
159         temp = min != +0.0d ? Double.doubleToLongBits(min) : 0L;
160         result = 31 * result + (int) (temp ^ (temp >>> 32));
161         temp = max != +0.0d ? Double.doubleToLongBits(max) : 0L;
162         result = 31 * result + (int) (temp ^ (temp >>> 32));
163         return result;
164     }
165 }