http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/DataPoint.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/DataPoint.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/DataPoint.java index 21ad3df..a5a82af 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/DataPoint.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/DataPoint.java @@ -1,4 +1,3 @@ - package com.yahoo.labs.samoa.moa.core; /* @@ -29,106 +28,107 @@ import com.yahoo.labs.samoa.instances.Attribute; import com.yahoo.labs.samoa.instances.DenseInstance; import com.yahoo.labs.samoa.instances.Instance; -public class DataPoint extends DenseInstance{ - - private static final long serialVersionUID = 1L; - - protected int timestamp; - private HashMap<String, String> measure_values; - - protected int noiseLabel; - - public DataPoint(Instance nextInstance, Integer timestamp) { - super(nextInstance); - this.setDataset(nextInstance.dataset()); - this.timestamp = timestamp; - measure_values = new HashMap<String, String>(); - - Attribute classLabel = dataset().classAttribute(); - noiseLabel = classLabel.indexOfValue("noise"); // -1 returned if there is no noise - } +public class DataPoint extends DenseInstance { - public void updateWeight(int cur_timestamp, double decay_rate){ - setWeight(Math.pow(2,(-1.0)*decay_rate*(cur_timestamp-timestamp))); - } + private static final long serialVersionUID = 1L; - public void setMeasureValue(String measureKey, double value){ - synchronized(measure_values){ - measure_values.put(measureKey, Double.toString(value)); - } - } + protected int timestamp; + private HashMap<String, String> measure_values; - public void setMeasureValue(String measureKey,String value){ - synchronized(measure_values){ - measure_values.put(measureKey, value); - } - } + protected int noiseLabel; + + public DataPoint(Instance nextInstance, Integer timestamp) { + super(nextInstance); + this.setDataset(nextInstance.dataset()); + this.timestamp = timestamp; + measure_values = new HashMap<String, String>(); + + Attribute classLabel = dataset().classAttribute(); + noiseLabel = classLabel.indexOfValue("noise"); // -1 returned if there is no + // noise + } - public String getMeasureValue(String measureKey){ - if(measure_values.containsKey(measureKey)) - synchronized(measure_values){ - return measure_values.get(measureKey); - } - else - return ""; + public void updateWeight(int cur_timestamp, double decay_rate) { + setWeight(Math.pow(2, (-1.0) * decay_rate * (cur_timestamp - timestamp))); + } + + public void setMeasureValue(String measureKey, double value) { + synchronized (measure_values) { + measure_values.put(measureKey, Double.toString(value)); } + } - public int getTimestamp(){ - return timestamp; + public void setMeasureValue(String measureKey, String value) { + synchronized (measure_values) { + measure_values.put(measureKey, value); } - - public String getInfo(int x_dim, int y_dim) { - StringBuffer sb = new StringBuffer(); - sb.append("<html><table>"); - sb.append("<tr><td>Point</td><td>"+timestamp+"</td></tr>"); - for (int i = 0; i < numAttributes() - 1; i++) { //m_AttValues.length - String label = "Dim "+i; - if(i == x_dim) - label = "<b>X</b>"; - if(i == y_dim) - label = "<b>Y</b>"; - sb.append("<tr><td>"+label+"</td><td>"+value(i)+"</td></tr>"); - } - sb.append("<tr><td>Decay</td><td>"+weight()+"</td></tr>"); - sb.append("<tr><td>True cluster</td><td>"+classValue()+"</td></tr>"); - sb.append("</table>"); - sb.append("<br>"); - sb.append("<b>Evaluation</b><br>"); - sb.append("<table>"); - - TreeSet<String> sortedset; - synchronized(measure_values){ - sortedset = new TreeSet<String>(measure_values.keySet()); - } - - Iterator miterator = sortedset.iterator(); - while(miterator.hasNext()) { - String key = (String)miterator.next(); - sb.append("<tr><td>"+key+"</td><td>"+measure_values.get(key)+"</td></tr>"); - } - - sb.append("</table></html>"); - return sb.toString(); + } + + public String getMeasureValue(String measureKey) { + if (measure_values.containsKey(measureKey)) + synchronized (measure_values) { + return measure_values.get(measureKey); + } + else + return ""; + } + + public int getTimestamp() { + return timestamp; + } + + public String getInfo(int x_dim, int y_dim) { + StringBuffer sb = new StringBuffer(); + sb.append("<html><table>"); + sb.append("<tr><td>Point</td><td>" + timestamp + "</td></tr>"); + for (int i = 0; i < numAttributes() - 1; i++) { // m_AttValues.length + String label = "Dim " + i; + if (i == x_dim) + label = "<b>X</b>"; + if (i == y_dim) + label = "<b>Y</b>"; + sb.append("<tr><td>" + label + "</td><td>" + value(i) + "</td></tr>"); + } + sb.append("<tr><td>Decay</td><td>" + weight() + "</td></tr>"); + sb.append("<tr><td>True cluster</td><td>" + classValue() + "</td></tr>"); + sb.append("</table>"); + sb.append("<br>"); + sb.append("<b>Evaluation</b><br>"); + sb.append("<table>"); + + TreeSet<String> sortedset; + synchronized (measure_values) { + sortedset = new TreeSet<String>(measure_values.keySet()); } - public double getDistance(DataPoint other){ - double distance = 0.0; - int numDims = numAttributes(); - if(classIndex()!=0) numDims--; - - for (int i = 0; i < numDims; i++) { - double d = value(i) - other.value(i); - distance += d * d; - } - return Math.sqrt(distance); + Iterator miterator = sortedset.iterator(); + while (miterator.hasNext()) { + String key = (String) miterator.next(); + sb.append("<tr><td>" + key + "</td><td>" + measure_values.get(key) + "</td></tr>"); } - - public boolean isNoise() { - return (int)classValue() == noiseLabel; - } - - public double getNoiseLabel() { - return noiseLabel; + sb.append("</table></html>"); + return sb.toString(); + } + + public double getDistance(DataPoint other) { + double distance = 0.0; + int numDims = numAttributes(); + if (classIndex() != 0) + numDims--; + + for (int i = 0; i < numDims; i++) { + double d = value(i) - other.value(i); + distance += d * d; } + return Math.sqrt(distance); + } + + public boolean isNoise() { + return (int) classValue() == noiseLabel; + } + + public double getNoiseLabel() { + return noiseLabel; + } }
http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/DoubleVector.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/DoubleVector.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/DoubleVector.java index 1373d22..0c4d4a6 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/DoubleVector.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/DoubleVector.java @@ -24,172 +24,172 @@ import com.yahoo.labs.samoa.moa.AbstractMOAObject; /** * Vector of double numbers with some utilities. - * + * * @author Richard Kirkby ([email protected]) * @version $Revision: 7 $ */ public class DoubleVector extends AbstractMOAObject { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - protected double[] array; + protected double[] array; - public DoubleVector() { - this.array = new double[0]; - } + public DoubleVector() { + this.array = new double[0]; + } - public DoubleVector(double[] toCopy) { - this.array = new double[toCopy.length]; - System.arraycopy(toCopy, 0, this.array, 0, toCopy.length); - } + public DoubleVector(double[] toCopy) { + this.array = new double[toCopy.length]; + System.arraycopy(toCopy, 0, this.array, 0, toCopy.length); + } - public DoubleVector(DoubleVector toCopy) { - this(toCopy.getArrayRef()); - } + public DoubleVector(DoubleVector toCopy) { + this(toCopy.getArrayRef()); + } - public int numValues() { - return this.array.length; - } + public int numValues() { + return this.array.length; + } - public void setValue(int i, double v) { - if (i >= this.array.length) { - setArrayLength(i + 1); - } - this.array[i] = v; + public void setValue(int i, double v) { + if (i >= this.array.length) { + setArrayLength(i + 1); } + this.array[i] = v; + } - public void addToValue(int i, double v) { - if (i >= this.array.length) { - setArrayLength(i + 1); - } - this.array[i] += v; + public void addToValue(int i, double v) { + if (i >= this.array.length) { + setArrayLength(i + 1); } + this.array[i] += v; + } - public void addValues(DoubleVector toAdd) { - addValues(toAdd.getArrayRef()); - } + public void addValues(DoubleVector toAdd) { + addValues(toAdd.getArrayRef()); + } - public void addValues(double[] toAdd) { - if (toAdd.length > this.array.length) { - setArrayLength(toAdd.length); - } - for (int i = 0; i < toAdd.length; i++) { - this.array[i] += toAdd[i]; - } + public void addValues(double[] toAdd) { + if (toAdd.length > this.array.length) { + setArrayLength(toAdd.length); } - - public void subtractValues(DoubleVector toSubtract) { - subtractValues(toSubtract.getArrayRef()); + for (int i = 0; i < toAdd.length; i++) { + this.array[i] += toAdd[i]; } + } - public void subtractValues(double[] toSubtract) { - if (toSubtract.length > this.array.length) { - setArrayLength(toSubtract.length); - } - for (int i = 0; i < toSubtract.length; i++) { - this.array[i] -= toSubtract[i]; - } - } + public void subtractValues(DoubleVector toSubtract) { + subtractValues(toSubtract.getArrayRef()); + } - public void addToValues(double toAdd) { - for (int i = 0; i < this.array.length; i++) { - this.array[i] = this.array[i] + toAdd; - } + public void subtractValues(double[] toSubtract) { + if (toSubtract.length > this.array.length) { + setArrayLength(toSubtract.length); } - - public void scaleValues(double multiplier) { - for (int i = 0; i < this.array.length; i++) { - this.array[i] = this.array[i] * multiplier; - } + for (int i = 0; i < toSubtract.length; i++) { + this.array[i] -= toSubtract[i]; } + } - // returns 0.0 for values outside of range - public double getValue(int i) { - return ((i >= 0) && (i < this.array.length)) ? this.array[i] : 0.0; + public void addToValues(double toAdd) { + for (int i = 0; i < this.array.length; i++) { + this.array[i] = this.array[i] + toAdd; } + } - public double sumOfValues() { - double sum = 0.0; - for (double element : this.array) { - sum += element; - } - return sum; - } - - public int maxIndex() { - int max = -1; - for (int i = 0; i < this.array.length; i++) { - if ((max < 0) || (this.array[i] > this.array[max])) { - max = i; - } - } - return max; - } - - public void normalize() { - scaleValues(1.0 / sumOfValues()); - } - - public int numNonZeroEntries() { - int count = 0; - for (double element : this.array) { - if (element != 0.0) { - count++; - } - } - return count; + public void scaleValues(double multiplier) { + for (int i = 0; i < this.array.length; i++) { + this.array[i] = this.array[i] * multiplier; } + } - public double minWeight() { - if (this.array.length > 0) { - double min = this.array[0]; - for (int i = 1; i < this.array.length; i++) { - if (this.array[i] < min) { - min = this.array[i]; - } - } - return min; - } - return 0.0; - } + // returns 0.0 for values outside of range + public double getValue(int i) { + return ((i >= 0) && (i < this.array.length)) ? this.array[i] : 0.0; + } - public double[] getArrayCopy() { - double[] aCopy = new double[this.array.length]; - System.arraycopy(this.array, 0, aCopy, 0, this.array.length); - return aCopy; + public double sumOfValues() { + double sum = 0.0; + for (double element : this.array) { + sum += element; } + return sum; + } - public double[] getArrayRef() { - return this.array; + public int maxIndex() { + int max = -1; + for (int i = 0; i < this.array.length; i++) { + if ((max < 0) || (this.array[i] > this.array[max])) { + max = i; + } } + return max; + } - protected void setArrayLength(int l) { - double[] newArray = new double[l]; - int numToCopy = this.array.length; - if (numToCopy > l) { - numToCopy = l; - } - System.arraycopy(this.array, 0, newArray, 0, numToCopy); - this.array = newArray; - } + public void normalize() { + scaleValues(1.0 / sumOfValues()); + } - public void getSingleLineDescription(StringBuilder out) { - getSingleLineDescription(out, numValues()); + public int numNonZeroEntries() { + int count = 0; + for (double element : this.array) { + if (element != 0.0) { + count++; + } } + return count; + } - public void getSingleLineDescription(StringBuilder out, int numValues) { - out.append("{"); - for (int i = 0; i < numValues; i++) { - if (i > 0) { - out.append("|"); - } - out.append(StringUtils.doubleToString(getValue(i), 3)); + public double minWeight() { + if (this.array.length > 0) { + double min = this.array[0]; + for (int i = 1; i < this.array.length; i++) { + if (this.array[i] < min) { + min = this.array[i]; } - out.append("}"); - } - - @Override - public void getDescription(StringBuilder sb, int indent) { - getSingleLineDescription(sb); - } + } + return min; + } + return 0.0; + } + + public double[] getArrayCopy() { + double[] aCopy = new double[this.array.length]; + System.arraycopy(this.array, 0, aCopy, 0, this.array.length); + return aCopy; + } + + public double[] getArrayRef() { + return this.array; + } + + protected void setArrayLength(int l) { + double[] newArray = new double[l]; + int numToCopy = this.array.length; + if (numToCopy > l) { + numToCopy = l; + } + System.arraycopy(this.array, 0, newArray, 0, numToCopy); + this.array = newArray; + } + + public void getSingleLineDescription(StringBuilder out) { + getSingleLineDescription(out, numValues()); + } + + public void getSingleLineDescription(StringBuilder out, int numValues) { + out.append("{"); + for (int i = 0; i < numValues; i++) { + if (i > 0) { + out.append("|"); + } + out.append(StringUtils.doubleToString(getValue(i), 3)); + } + out.append("}"); + } + + @Override + public void getDescription(StringBuilder sb, int indent) { + getSingleLineDescription(sb); + } } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/Example.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/Example.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/Example.java index 658ea14..1693350 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/Example.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/Example.java @@ -1,4 +1,3 @@ - package com.yahoo.labs.samoa.moa.core; /* @@ -21,11 +20,11 @@ package com.yahoo.labs.samoa.moa.core; * #L% */ -public interface Example< T extends Object> { +public interface Example<T extends Object> { + + public T getData(); - public T getData(); + public double weight(); - public double weight(); - - public void setWeight(double weight); -} + public void setWeight(double weight); +} http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/FastVector.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/FastVector.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/FastVector.java index 102c6b1..fb03c40 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/FastVector.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/FastVector.java @@ -1,4 +1,3 @@ - /* * FastVector.java @@ -30,38 +29,41 @@ import java.util.ArrayList; /** * Simple extension of ArrayList. Exists for legacy reasons. - * + * * @author Eibe Frank ([email protected]) * @version $Revision: 8034 $ */ public class FastVector<E> extends ArrayList<E> { - /** - * Adds an element to this vector. Increases its capacity if its not large - * enough. - * - * @param element the element to add - */ - public final void addElement(E element) { - add(element); - } + /** + * Adds an element to this vector. Increases its capacity if its not large + * enough. + * + * @param element + * the element to add + */ + public final void addElement(E element) { + add(element); + } - /** - * Returns the element at the given position. - * - * @param index the element's index - * @return the element with the given index - */ - public final E elementAt(int index) { - return get(index); - } + /** + * Returns the element at the given position. + * + * @param index + * the element's index + * @return the element with the given index + */ + public final E elementAt(int index) { + return get(index); + } - /** - * Deletes an element from this vector. - * - * @param index the index of the element to be deleted - */ - public final void removeElementAt(int index) { - remove(index); - } + /** + * Deletes an element from this vector. + * + * @param index + * the index of the element to be deleted + */ + public final void removeElementAt(int index) { + remove(index); + } } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/GaussianEstimator.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/GaussianEstimator.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/GaussianEstimator.java index e784e29..6677bf0 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/GaussianEstimator.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/GaussianEstimator.java @@ -23,101 +23,105 @@ package com.yahoo.labs.samoa.moa.core; import com.yahoo.labs.samoa.moa.AbstractMOAObject; /** - * Gaussian incremental estimator that uses incremental method that is more resistant to floating point imprecision. - * for more info see Donald Knuth's "The Art of Computer Programming, Volume 2: Seminumerical Algorithms", section 4.2.2. - * + * Gaussian incremental estimator that uses incremental method that is more + * resistant to floating point imprecision. for more info see Donald Knuth's + * "The Art of Computer Programming, Volume 2: Seminumerical Algorithms", + * section 4.2.2. + * * @author Richard Kirkby ([email protected]) * @version $Revision: 7 $ */ public class GaussianEstimator extends AbstractMOAObject { - private static final long serialVersionUID = 1L; - - protected double weightSum; - - protected double mean; + private static final long serialVersionUID = 1L; - protected double varianceSum; + protected double weightSum; - public static final double NORMAL_CONSTANT = Math.sqrt(2 * Math.PI); + protected double mean; - public void addObservation(double value, double weight) { - if (Double.isInfinite(value) || Double.isNaN(value)) { - return; - } - if (this.weightSum > 0.0) { - this.weightSum += weight; - double lastMean = this.mean; - this.mean += weight * (value - lastMean) / this.weightSum; - this.varianceSum += weight * (value - lastMean) * (value - this.mean); - } else { - this.mean = value; - this.weightSum = weight; - } - } - - public void addObservations(GaussianEstimator obs) { - // Follows Variance Combination Rule in Section 2 of - // Brian Babcock, Mayur Datar, Rajeev Motwani, Liadan O'Callaghan: - // Maintaining variance and k-medians over data stream windows. PODS 2003: 234-243 - // - if ((this.weightSum >= 0.0) && (obs.weightSum > 0.0)) { - double oldMean = this.mean; - this.mean = (this.mean * (this.weightSum / (this.weightSum + obs.weightSum))) - + (obs.mean * (obs.weightSum / (this.weightSum + obs.weightSum))); - this.varianceSum += obs.varianceSum + (this.weightSum * obs.weightSum / (this.weightSum + obs.weightSum) * - Math.pow(obs.mean-oldMean, 2)); - this.weightSum += obs.weightSum; - } - } + protected double varianceSum; - public double getTotalWeightObserved() { - return this.weightSum; - } + public static final double NORMAL_CONSTANT = Math.sqrt(2 * Math.PI); - public double getMean() { - return this.mean; + public void addObservation(double value, double weight) { + if (Double.isInfinite(value) || Double.isNaN(value)) { + return; } - - public double getStdDev() { - return Math.sqrt(getVariance()); + if (this.weightSum > 0.0) { + this.weightSum += weight; + double lastMean = this.mean; + this.mean += weight * (value - lastMean) / this.weightSum; + this.varianceSum += weight * (value - lastMean) * (value - this.mean); + } else { + this.mean = value; + this.weightSum = weight; } - - public double getVariance() { - return this.weightSum > 1.0 ? this.varianceSum / (this.weightSum - 1.0) - : 0.0; + } + + public void addObservations(GaussianEstimator obs) { + // Follows Variance Combination Rule in Section 2 of + // Brian Babcock, Mayur Datar, Rajeev Motwani, Liadan O'Callaghan: + // Maintaining variance and k-medians over data stream windows. PODS 2003: + // 234-243 + // + if ((this.weightSum >= 0.0) && (obs.weightSum > 0.0)) { + double oldMean = this.mean; + this.mean = (this.mean * (this.weightSum / (this.weightSum + obs.weightSum))) + + (obs.mean * (obs.weightSum / (this.weightSum + obs.weightSum))); + this.varianceSum += obs.varianceSum + (this.weightSum * obs.weightSum / (this.weightSum + obs.weightSum) * + Math.pow(obs.mean - oldMean, 2)); + this.weightSum += obs.weightSum; } - - public double probabilityDensity(double value) { - if (this.weightSum > 0.0) { - double stdDev = getStdDev(); - if (stdDev > 0.0) { - double diff = value - getMean(); - return (1.0 / (NORMAL_CONSTANT * stdDev)) - * Math.exp(-(diff * diff / (2.0 * stdDev * stdDev))); - } - return value == getMean() ? 1.0 : 0.0; - } - return 0.0; + } + + public double getTotalWeightObserved() { + return this.weightSum; + } + + public double getMean() { + return this.mean; + } + + public double getStdDev() { + return Math.sqrt(getVariance()); + } + + public double getVariance() { + return this.weightSum > 1.0 ? this.varianceSum / (this.weightSum - 1.0) + : 0.0; + } + + public double probabilityDensity(double value) { + if (this.weightSum > 0.0) { + double stdDev = getStdDev(); + if (stdDev > 0.0) { + double diff = value - getMean(); + return (1.0 / (NORMAL_CONSTANT * stdDev)) + * Math.exp(-(diff * diff / (2.0 * stdDev * stdDev))); + } + return value == getMean() ? 1.0 : 0.0; } - - public double[] estimatedWeight_LessThan_EqualTo_GreaterThan_Value( - double value) { - double equalToWeight = probabilityDensity(value) * this.weightSum; - double stdDev = getStdDev(); - double lessThanWeight = stdDev > 0.0 ? com.yahoo.labs.samoa.moa.core.Statistics.normalProbability((value - getMean()) / stdDev) - * this.weightSum - equalToWeight - : (value < getMean() ? this.weightSum - equalToWeight : 0.0); - double greaterThanWeight = this.weightSum - equalToWeight - - lessThanWeight; - if (greaterThanWeight < 0.0) { - greaterThanWeight = 0.0; - } - return new double[]{lessThanWeight, equalToWeight, greaterThanWeight}; + return 0.0; + } + + public double[] estimatedWeight_LessThan_EqualTo_GreaterThan_Value( + double value) { + double equalToWeight = probabilityDensity(value) * this.weightSum; + double stdDev = getStdDev(); + double lessThanWeight = stdDev > 0.0 ? com.yahoo.labs.samoa.moa.core.Statistics + .normalProbability((value - getMean()) / stdDev) + * this.weightSum - equalToWeight + : (value < getMean() ? this.weightSum - equalToWeight : 0.0); + double greaterThanWeight = this.weightSum - equalToWeight + - lessThanWeight; + if (greaterThanWeight < 0.0) { + greaterThanWeight = 0.0; } + return new double[] { lessThanWeight, equalToWeight, greaterThanWeight }; + } - @Override - public void getDescription(StringBuilder sb, int indent) { - // TODO Auto-generated method stub - } + @Override + public void getDescription(StringBuilder sb, int indent) { + // TODO Auto-generated method stub + } } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/GreenwaldKhannaQuantileSummary.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/GreenwaldKhannaQuantileSummary.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/GreenwaldKhannaQuantileSummary.java index 59af67d..43bce69 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/GreenwaldKhannaQuantileSummary.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/GreenwaldKhannaQuantileSummary.java @@ -27,255 +27,255 @@ import com.yahoo.labs.samoa.moa.AbstractMOAObject; /** * Class for representing summaries of Greenwald and Khanna quantiles. - * + * * @author Richard Kirkby ([email protected]) * @version $Revision: 7 $ */ public class GreenwaldKhannaQuantileSummary extends AbstractMOAObject { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - protected static class Tuple implements Serializable { + protected static class Tuple implements Serializable { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - public double v; + public double v; - public long g; + public long g; - public long delta; + public long delta; - public Tuple(double v, long g, long delta) { - this.v = v; - this.g = g; - this.delta = delta; - } + public Tuple(double v, long g, long delta) { + this.v = v; + this.g = g; + this.delta = delta; + } - public Tuple(double v) { - this(v, 1, 0); - } + public Tuple(double v) { + this(v, 1, 0); } + } - protected Tuple[] summary; + protected Tuple[] summary; - protected int numTuples = 0; + protected int numTuples = 0; - protected long numObservations = 0; + protected long numObservations = 0; - public GreenwaldKhannaQuantileSummary(int maxTuples) { - this.summary = new Tuple[maxTuples]; - } + public GreenwaldKhannaQuantileSummary(int maxTuples) { + this.summary = new Tuple[maxTuples]; + } - public void insert(double val) { - int i = findIndexOfTupleGreaterThan(val); - Tuple nextT = this.summary[i]; - if (nextT == null) { - insertTuple(new Tuple(val, 1, 0), i); - } else { - insertTuple(new Tuple(val, 1, nextT.g + nextT.delta - 1), i); - } - if (this.numTuples == this.summary.length) { - // use method 1 - deleteMergeableTupleMostFull(); - // if (mergeMethod == 1) { - // deleteMergeableTupleMostFull(); - // } else if (mergeMethod == 2) { - // deleteTupleMostFull(); - // } else { - // long maxDelta = findMaxDelta(); - // compress(maxDelta); - // while (numTuples == summary.length) { - // maxDelta++; - // compress(maxDelta); - // } - // } - } - this.numObservations++; + public void insert(double val) { + int i = findIndexOfTupleGreaterThan(val); + Tuple nextT = this.summary[i]; + if (nextT == null) { + insertTuple(new Tuple(val, 1, 0), i); + } else { + insertTuple(new Tuple(val, 1, nextT.g + nextT.delta - 1), i); } - - protected void insertTuple(Tuple t, int index) { - System.arraycopy(this.summary, index, this.summary, index + 1, - this.numTuples - index); - this.summary[index] = t; - this.numTuples++; + if (this.numTuples == this.summary.length) { + // use method 1 + deleteMergeableTupleMostFull(); + // if (mergeMethod == 1) { + // deleteMergeableTupleMostFull(); + // } else if (mergeMethod == 2) { + // deleteTupleMostFull(); + // } else { + // long maxDelta = findMaxDelta(); + // compress(maxDelta); + // while (numTuples == summary.length) { + // maxDelta++; + // compress(maxDelta); + // } + // } } - - protected void deleteTuple(int index) { - this.summary[index] = new Tuple(this.summary[index + 1].v, - this.summary[index].g + this.summary[index + 1].g, - this.summary[index + 1].delta); - System.arraycopy(this.summary, index + 2, this.summary, index + 1, - this.numTuples - index - 2); - this.summary[this.numTuples - 1] = null; - this.numTuples--; + this.numObservations++; + } + + protected void insertTuple(Tuple t, int index) { + System.arraycopy(this.summary, index, this.summary, index + 1, + this.numTuples - index); + this.summary[index] = t; + this.numTuples++; + } + + protected void deleteTuple(int index) { + this.summary[index] = new Tuple(this.summary[index + 1].v, + this.summary[index].g + this.summary[index + 1].g, + this.summary[index + 1].delta); + System.arraycopy(this.summary, index + 2, this.summary, index + 1, + this.numTuples - index - 2); + this.summary[this.numTuples - 1] = null; + this.numTuples--; + } + + protected void deleteTupleMostFull() { + long leastFullness = Long.MAX_VALUE; + int leastFullIndex = 0; + for (int i = 1; i < this.numTuples - 1; i++) { + long fullness = this.summary[i].g + this.summary[i + 1].g + + this.summary[i + 1].delta; + if (fullness < leastFullness) { + leastFullness = fullness; + leastFullIndex = i; + } } - - protected void deleteTupleMostFull() { - long leastFullness = Long.MAX_VALUE; - int leastFullIndex = 0; - for (int i = 1; i < this.numTuples - 1; i++) { - long fullness = this.summary[i].g + this.summary[i + 1].g - + this.summary[i + 1].delta; - if (fullness < leastFullness) { - leastFullness = fullness; - leastFullIndex = i; - } - } - if (leastFullIndex > 0) { - deleteTuple(leastFullIndex); - } + if (leastFullIndex > 0) { + deleteTuple(leastFullIndex); } - - protected void deleteMergeableTupleMostFull() { - long leastFullness = Long.MAX_VALUE; - int leastFullIndex = 0; - for (int i = 1; i < this.numTuples - 1; i++) { - long fullness = this.summary[i].g + this.summary[i + 1].g - + this.summary[i + 1].delta; - if ((this.summary[i].delta >= this.summary[i + 1].delta) - && (fullness < leastFullness)) { - leastFullness = fullness; - leastFullIndex = i; - } - } - if (leastFullIndex > 0) { - deleteTuple(leastFullIndex); - } + } + + protected void deleteMergeableTupleMostFull() { + long leastFullness = Long.MAX_VALUE; + int leastFullIndex = 0; + for (int i = 1; i < this.numTuples - 1; i++) { + long fullness = this.summary[i].g + this.summary[i + 1].g + + this.summary[i + 1].delta; + if ((this.summary[i].delta >= this.summary[i + 1].delta) + && (fullness < leastFullness)) { + leastFullness = fullness; + leastFullIndex = i; + } } - - public long getWorstError() { - long mostFullness = 0; - for (int i = 1; i < this.numTuples - 1; i++) { - long fullness = this.summary[i].g + this.summary[i].delta; - if (fullness > mostFullness) { - mostFullness = fullness; - } - } - return mostFullness; + if (leastFullIndex > 0) { + deleteTuple(leastFullIndex); } - - public long findMaxDelta() { - long maxDelta = 0; - for (int i = 0; i < this.numTuples; i++) { - if (this.summary[i].delta > maxDelta) { - maxDelta = this.summary[i].delta; - } - } - return maxDelta; + } + + public long getWorstError() { + long mostFullness = 0; + for (int i = 1; i < this.numTuples - 1; i++) { + long fullness = this.summary[i].g + this.summary[i].delta; + if (fullness > mostFullness) { + mostFullness = fullness; + } } - - public void compress(long maxDelta) { - long[] bandBoundaries = computeBandBoundaries(maxDelta); - for (int i = this.numTuples - 2; i >= 0; i--) { - if (this.summary[i].delta >= this.summary[i + 1].delta) { - int band = 0; - while (this.summary[i].delta < bandBoundaries[band]) { - band++; - } - long belowBandThreshold = Long.MAX_VALUE; - if (band > 0) { - belowBandThreshold = bandBoundaries[band - 1]; - } - long mergeG = this.summary[i + 1].g + this.summary[i].g; - int childI = i - 1; - while (((mergeG + this.summary[i + 1].delta) < maxDelta) - && (childI >= 0) - && (this.summary[childI].delta >= belowBandThreshold)) { - mergeG += this.summary[childI].g; - childI--; - } - if (mergeG + this.summary[i + 1].delta < maxDelta) { - // merge - int numDeleted = i - childI; - this.summary[childI + 1] = new Tuple(this.summary[i + 1].v, - mergeG, this.summary[i + 1].delta); - // todo complete & test this multiple delete - System.arraycopy(this.summary, i + 2, this.summary, - childI + 2, this.numTuples - (i + 2)); - for (int j = this.numTuples - numDeleted; j < this.numTuples; j++) { - this.summary[j] = null; - } - this.numTuples -= numDeleted; - i = childI + 1; - } - } - } + return mostFullness; + } + + public long findMaxDelta() { + long maxDelta = 0; + for (int i = 0; i < this.numTuples; i++) { + if (this.summary[i].delta > maxDelta) { + maxDelta = this.summary[i].delta; + } } - - public double getQuantile(double quant) { - long r = (long) Math.ceil(quant * this.numObservations); - long currRank = 0; - for (int i = 0; i < this.numTuples - 1; i++) { - currRank += this.summary[i].g; - if (currRank + this.summary[i + 1].g > r) { - return this.summary[i].v; - } + return maxDelta; + } + + public void compress(long maxDelta) { + long[] bandBoundaries = computeBandBoundaries(maxDelta); + for (int i = this.numTuples - 2; i >= 0; i--) { + if (this.summary[i].delta >= this.summary[i + 1].delta) { + int band = 0; + while (this.summary[i].delta < bandBoundaries[band]) { + band++; } - return this.summary[this.numTuples - 1].v; + long belowBandThreshold = Long.MAX_VALUE; + if (band > 0) { + belowBandThreshold = bandBoundaries[band - 1]; + } + long mergeG = this.summary[i + 1].g + this.summary[i].g; + int childI = i - 1; + while (((mergeG + this.summary[i + 1].delta) < maxDelta) + && (childI >= 0) + && (this.summary[childI].delta >= belowBandThreshold)) { + mergeG += this.summary[childI].g; + childI--; + } + if (mergeG + this.summary[i + 1].delta < maxDelta) { + // merge + int numDeleted = i - childI; + this.summary[childI + 1] = new Tuple(this.summary[i + 1].v, + mergeG, this.summary[i + 1].delta); + // todo complete & test this multiple delete + System.arraycopy(this.summary, i + 2, this.summary, + childI + 2, this.numTuples - (i + 2)); + for (int j = this.numTuples - numDeleted; j < this.numTuples; j++) { + this.summary[j] = null; + } + this.numTuples -= numDeleted; + i = childI + 1; + } + } } - - public long getTotalCount() { - return this.numObservations; + } + + public double getQuantile(double quant) { + long r = (long) Math.ceil(quant * this.numObservations); + long currRank = 0; + for (int i = 0; i < this.numTuples - 1; i++) { + currRank += this.summary[i].g; + if (currRank + this.summary[i + 1].g > r) { + return this.summary[i].v; + } } - - public double getPropotionBelow(double cutpoint) { - return (double) getCountBelow(cutpoint) / (double) this.numObservations; + return this.summary[this.numTuples - 1].v; + } + + public long getTotalCount() { + return this.numObservations; + } + + public double getPropotionBelow(double cutpoint) { + return (double) getCountBelow(cutpoint) / (double) this.numObservations; + } + + public long getCountBelow(double cutpoint) { + long rank = 0; + for (int i = 0; i < this.numTuples; i++) { + if (this.summary[i].v > cutpoint) { + break; + } + rank += this.summary[i].g; } + return rank; + } - public long getCountBelow(double cutpoint) { - long rank = 0; - for (int i = 0; i < this.numTuples; i++) { - if (this.summary[i].v > cutpoint) { - break; - } - rank += this.summary[i].g; - } - return rank; + public double[] getSuggestedCutpoints() { + double[] cutpoints = new double[this.numTuples]; + for (int i = 0; i < this.numTuples; i++) { + cutpoints[i] = this.summary[i].v; } - - public double[] getSuggestedCutpoints() { - double[] cutpoints = new double[this.numTuples]; - for (int i = 0; i < this.numTuples; i++) { - cutpoints[i] = this.summary[i].v; - } - return cutpoints; + return cutpoints; + } + + protected int findIndexOfTupleGreaterThan(double val) { + int high = this.numTuples, low = -1, probe; + while (high - low > 1) { + probe = (high + low) / 2; + if (this.summary[probe].v > val) { + high = probe; + } else { + low = probe; + } } - - protected int findIndexOfTupleGreaterThan(double val) { - int high = this.numTuples, low = -1, probe; - while (high - low > 1) { - probe = (high + low) / 2; - if (this.summary[probe].v > val) { - high = probe; - } else { - low = probe; - } - } - return high; + return high; + } + + public static long[] computeBandBoundaries(long maxDelta) { + ArrayList<Long> boundaryList = new ArrayList<Long>(); + boundaryList.add(new Long(maxDelta)); + int alpha = 1; + while (true) { + long boundary = (maxDelta - (2 << (alpha - 1)) - (maxDelta % (2 << (alpha - 1)))); + if (boundary >= 0) { + boundaryList.add(new Long(boundary + 1)); + } else { + break; + } + alpha++; } - - public static long[] computeBandBoundaries(long maxDelta) { - ArrayList<Long> boundaryList = new ArrayList<Long>(); - boundaryList.add(new Long(maxDelta)); - int alpha = 1; - while (true) { - long boundary = (maxDelta - (2 << (alpha - 1)) - (maxDelta % (2 << (alpha - 1)))); - if (boundary >= 0) { - boundaryList.add(new Long(boundary + 1)); - } else { - break; - } - alpha++; - } - boundaryList.add(new Long(0)); - long[] boundaries = new long[boundaryList.size()]; - for (int i = 0; i < boundaries.length; i++) { - boundaries[i] = boundaryList.get(i).longValue(); - } - return boundaries; + boundaryList.add(new Long(0)); + long[] boundaries = new long[boundaryList.size()]; + for (int i = 0; i < boundaries.length; i++) { + boundaries[i] = boundaryList.get(i).longValue(); } + return boundaries; + } - public void getDescription(StringBuilder sb, int indent) { - // TODO Auto-generated method stub - } + public void getDescription(StringBuilder sb, int indent) { + // TODO Auto-generated method stub + } } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/InputStreamProgressMonitor.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/InputStreamProgressMonitor.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/InputStreamProgressMonitor.java index 9cda129..8b7dd87 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/InputStreamProgressMonitor.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/InputStreamProgressMonitor.java @@ -27,105 +27,105 @@ import java.io.Serializable; /** * Class for monitoring the progress of reading an input stream. - * + * * @author Richard Kirkby ([email protected]) * @version $Revision: 7 $ */ public class InputStreamProgressMonitor extends FilterInputStream implements Serializable { - /** The number of bytes to read in total */ - protected int inputByteSize; + /** The number of bytes to read in total */ + protected int inputByteSize; + + /** The number of bytes read so far */ + protected int inputBytesRead; + + public InputStreamProgressMonitor(InputStream in) { + super(in); + try { + this.inputByteSize = in.available(); + } catch (IOException ioe) { + this.inputByteSize = 0; + } + this.inputBytesRead = 0; + } - /** The number of bytes read so far */ - protected int inputBytesRead; - - public InputStreamProgressMonitor(InputStream in) { - super(in); - try { - this.inputByteSize = in.available(); - } catch (IOException ioe) { - this.inputByteSize = 0; - } - this.inputBytesRead = 0; - } - - public int getBytesRead() { - return this.inputBytesRead; - } + public int getBytesRead() { + return this.inputBytesRead; + } - public int getBytesRemaining() { - return this.inputByteSize - this.inputBytesRead; - } + public int getBytesRemaining() { + return this.inputByteSize - this.inputBytesRead; + } - public double getProgressFraction() { - return ((double) this.inputBytesRead / (double) this.inputByteSize); - } + public double getProgressFraction() { + return ((double) this.inputBytesRead / (double) this.inputByteSize); + } - /* - * (non-Javadoc) - * - * @see java.io.InputStream#read() - */ - @Override - public int read() throws IOException { - int c = this.in.read(); - if (c > 0) { - this.inputBytesRead++; - } - return c; - } + /* + * (non-Javadoc) + * + * @see java.io.InputStream#read() + */ + @Override + public int read() throws IOException { + int c = this.in.read(); + if (c > 0) { + this.inputBytesRead++; + } + return c; + } - /* - * (non-Javadoc) - * - * @see java.io.InputStream#read(byte[]) - */ - @Override - public int read(byte[] b) throws IOException { - int numread = this.in.read(b); - if (numread > 0) { - this.inputBytesRead += numread; - } - return numread; - } + /* + * (non-Javadoc) + * + * @see java.io.InputStream#read(byte[]) + */ + @Override + public int read(byte[] b) throws IOException { + int numread = this.in.read(b); + if (numread > 0) { + this.inputBytesRead += numread; + } + return numread; + } - /* - * (non-Javadoc) - * - * @see java.io.InputStream#read(byte[], int, int) - */ - @Override - public int read(byte[] b, int off, int len) throws IOException { - int numread = this.in.read(b, off, len); - if (numread > 0) { - this.inputBytesRead += numread; - } - return numread; - } + /* + * (non-Javadoc) + * + * @see java.io.InputStream#read(byte[], int, int) + */ + @Override + public int read(byte[] b, int off, int len) throws IOException { + int numread = this.in.read(b, off, len); + if (numread > 0) { + this.inputBytesRead += numread; + } + return numread; + } - /* - * (non-Javadoc) - * - * @see java.io.InputStream#skip(long) - */ - @Override - public long skip(long n) throws IOException { - long numskip = this.in.skip(n); - if (numskip > 0) { - this.inputBytesRead += numskip; - } - return numskip; - } + /* + * (non-Javadoc) + * + * @see java.io.InputStream#skip(long) + */ + @Override + public long skip(long n) throws IOException { + long numskip = this.in.skip(n); + if (numskip > 0) { + this.inputBytesRead += numskip; + } + return numskip; + } - /* - * (non-Javadoc) - * - * @see java.io.FilterInputStream#reset() - */ - @Override - public synchronized void reset() throws IOException { - this.in.reset(); - this.inputBytesRead = this.inputByteSize - this.in.available(); - } + /* + * (non-Javadoc) + * + * @see java.io.FilterInputStream#reset() + */ + @Override + public synchronized void reset() throws IOException { + this.in.reset(); + this.inputBytesRead = this.inputByteSize - this.in.available(); + } } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/InstanceExample.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/InstanceExample.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/InstanceExample.java index 20866a0..490ee0a 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/InstanceExample.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/InstanceExample.java @@ -25,26 +25,26 @@ import java.io.Serializable; public class InstanceExample implements Example<Instance>, Serializable { - public Instance instance; - - public InstanceExample (Instance inst) - { - this.instance = inst; - } - - @Override - public Instance getData() { - return this.instance; - } - - @Override - public double weight() { - return this.instance.weight(); - } - - @Override - public void setWeight(double w) { - this.instance.setWeight(w); - } - -} + public Instance instance; + + public InstanceExample(Instance inst) + { + this.instance = inst; + } + + @Override + public Instance getData() { + return this.instance; + } + + @Override + public double weight() { + return this.instance.weight(); + } + + @Override + public void setWeight(double w) { + this.instance.setWeight(w); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/Measurement.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/Measurement.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/Measurement.java index 1d1f194..5b9e042 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/Measurement.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/Measurement.java @@ -27,89 +27,89 @@ import com.yahoo.labs.samoa.moa.AbstractMOAObject; /** * Class for storing an evaluation measurement. - * + * * @author Richard Kirkby ([email protected]) * @version $Revision: 7 $ */ public class Measurement extends AbstractMOAObject { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - protected String name; + protected String name; - protected double value; + protected double value; - public Measurement(String name, double value) { - this.name = name; - this.value = value; - } + public Measurement(String name, double value) { + this.name = name; + this.value = value; + } - public String getName() { - return this.name; - } + public String getName() { + return this.name; + } - public double getValue() { - return this.value; - } + public double getValue() { + return this.value; + } - public static Measurement getMeasurementNamed(String name, - Measurement[] measurements) { - for (Measurement measurement : measurements) { - if (name.equals(measurement.getName())) { - return measurement; - } - } - return null; + public static Measurement getMeasurementNamed(String name, + Measurement[] measurements) { + for (Measurement measurement : measurements) { + if (name.equals(measurement.getName())) { + return measurement; + } } + return null; + } - public static void getMeasurementsDescription(Measurement[] measurements, - StringBuilder out, int indent) { - if (measurements.length > 0) { - StringUtils.appendIndented(out, indent, measurements[0].toString()); - for (int i = 1; i < measurements.length; i++) { - StringUtils.appendNewlineIndented(out, indent, measurements[i].toString()); - } + public static void getMeasurementsDescription(Measurement[] measurements, + StringBuilder out, int indent) { + if (measurements.length > 0) { + StringUtils.appendIndented(out, indent, measurements[0].toString()); + for (int i = 1; i < measurements.length; i++) { + StringUtils.appendNewlineIndented(out, indent, measurements[i].toString()); + } - } } + } - public static Measurement[] averageMeasurements(Measurement[][] toAverage) { - List<String> measurementNames = new ArrayList<String>(); - for (Measurement[] measurements : toAverage) { - for (Measurement measurement : measurements) { - if (measurementNames.indexOf(measurement.getName()) < 0) { - measurementNames.add(measurement.getName()); - } - } - } - GaussianEstimator[] estimators = new GaussianEstimator[measurementNames.size()]; - for (int i = 0; i < estimators.length; i++) { - estimators[i] = new GaussianEstimator(); + public static Measurement[] averageMeasurements(Measurement[][] toAverage) { + List<String> measurementNames = new ArrayList<String>(); + for (Measurement[] measurements : toAverage) { + for (Measurement measurement : measurements) { + if (measurementNames.indexOf(measurement.getName()) < 0) { + measurementNames.add(measurement.getName()); } - for (Measurement[] measurements : toAverage) { - for (Measurement measurement : measurements) { - estimators[measurementNames.indexOf(measurement.getName())].addObservation(measurement.getValue(), 1.0); - } - } - List<Measurement> averagedMeasurements = new ArrayList<Measurement>(); - for (int i = 0; i < measurementNames.size(); i++) { - String mName = measurementNames.get(i); - GaussianEstimator mEstimator = estimators[i]; - if (mEstimator.getTotalWeightObserved() > 1.0) { - averagedMeasurements.add(new Measurement("[avg] " + mName, - mEstimator.getMean())); - averagedMeasurements.add(new Measurement("[err] " + mName, - mEstimator.getStdDev() - / Math.sqrt(mEstimator.getTotalWeightObserved()))); - } - } - return averagedMeasurements.toArray(new Measurement[averagedMeasurements.size()]); + } } - - @Override - public void getDescription(StringBuilder sb, int indent) { - sb.append(getName()); - sb.append(" = "); - sb.append(StringUtils.doubleToString(getValue(), 3)); + GaussianEstimator[] estimators = new GaussianEstimator[measurementNames.size()]; + for (int i = 0; i < estimators.length; i++) { + estimators[i] = new GaussianEstimator(); } + for (Measurement[] measurements : toAverage) { + for (Measurement measurement : measurements) { + estimators[measurementNames.indexOf(measurement.getName())].addObservation(measurement.getValue(), 1.0); + } + } + List<Measurement> averagedMeasurements = new ArrayList<Measurement>(); + for (int i = 0; i < measurementNames.size(); i++) { + String mName = measurementNames.get(i); + GaussianEstimator mEstimator = estimators[i]; + if (mEstimator.getTotalWeightObserved() > 1.0) { + averagedMeasurements.add(new Measurement("[avg] " + mName, + mEstimator.getMean())); + averagedMeasurements.add(new Measurement("[err] " + mName, + mEstimator.getStdDev() + / Math.sqrt(mEstimator.getTotalWeightObserved()))); + } + } + return averagedMeasurements.toArray(new Measurement[averagedMeasurements.size()]); + } + + @Override + public void getDescription(StringBuilder sb, int indent) { + sb.append(getName()); + sb.append(" = "); + sb.append(StringUtils.doubleToString(getValue(), 3)); + } } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/MiscUtils.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/MiscUtils.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/MiscUtils.java index 4bfc98f..c176369 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/MiscUtils.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/MiscUtils.java @@ -24,74 +24,74 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.util.Random; - /** * Class implementing some utility methods. - * + * * @author Richard Kirkby ([email protected]) * @author Bernhard Pfahringer ([email protected]) * @version $Revision: 7 $ */ public class MiscUtils { - public static int chooseRandomIndexBasedOnWeights(double[] weights, - Random random) { - double probSum = Utils.sum(weights); - double val = random.nextDouble() * probSum; - int index = 0; - double sum = 0.0; - while ((sum <= val) && (index < weights.length)) { - sum += weights[index++]; - } - return index - 1; + public static int chooseRandomIndexBasedOnWeights(double[] weights, + Random random) { + double probSum = Utils.sum(weights); + double val = random.nextDouble() * probSum; + int index = 0; + double sum = 0.0; + while ((sum <= val) && (index < weights.length)) { + sum += weights[index++]; } + return index - 1; + } - public static int poisson(double lambda, Random r) { - if (lambda < 100.0) { - double product = 1.0; - double sum = 1.0; - double threshold = r.nextDouble() * Math.exp(lambda); - int i = 1; - int max = Math.max(100, 10 * (int) Math.ceil(lambda)); - while ((i < max) && (sum <= threshold)) { - product *= (lambda / i); - sum += product; - i++; - } - return i - 1; - } - double x = lambda + Math.sqrt(lambda) * r.nextGaussian(); - if (x < 0.0) { - return 0; - } - return (int) Math.floor(x); + public static int poisson(double lambda, Random r) { + if (lambda < 100.0) { + double product = 1.0; + double sum = 1.0; + double threshold = r.nextDouble() * Math.exp(lambda); + int i = 1; + int max = Math.max(100, 10 * (int) Math.ceil(lambda)); + while ((i < max) && (sum <= threshold)) { + product *= (lambda / i); + sum += product; + i++; + } + return i - 1; } - - public static String getStackTraceString(Exception ex) { - StringWriter stackTraceWriter = new StringWriter(); - ex.printStackTrace(new PrintWriter(stackTraceWriter)); - return "*** STACK TRACE ***\n" + stackTraceWriter.toString(); + double x = lambda + Math.sqrt(lambda) * r.nextGaussian(); + if (x < 0.0) { + return 0; } + return (int) Math.floor(x); + } - /** - * Returns index of maximum element in a given array of doubles. First - * maximum is returned. - * - * @param doubles the array of doubles - * @return the index of the maximum element - */ - public static /*@pure@*/ int maxIndex(double[] doubles) { + public static String getStackTraceString(Exception ex) { + StringWriter stackTraceWriter = new StringWriter(); + ex.printStackTrace(new PrintWriter(stackTraceWriter)); + return "*** STACK TRACE ***\n" + stackTraceWriter.toString(); + } - double maximum = 0; - int maxIndex = 0; + /** + * Returns index of maximum element in a given array of doubles. First maximum + * is returned. + * + * @param doubles + * the array of doubles + * @return the index of the maximum element + */ + public static/* @pure@ */int maxIndex(double[] doubles) { - for (int i = 0; i < doubles.length; i++) { - if ((i == 0) || (doubles[i] > maximum)) { - maxIndex = i; - maximum = doubles[i]; - } - } + double maximum = 0; + int maxIndex = 0; - return maxIndex; + for (int i = 0; i < doubles.length; i++) { + if ((i == 0) || (doubles[i] > maximum)) { + maxIndex = i; + maximum = doubles[i]; + } } + + return maxIndex; + } } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/ObjectRepository.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/ObjectRepository.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/ObjectRepository.java index d326361..a7a4e35 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/ObjectRepository.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/ObjectRepository.java @@ -22,11 +22,11 @@ package com.yahoo.labs.samoa.moa.core; /** * Interface for object repositories. - * + * * @author Richard Kirkby ([email protected]) * @version $Revision: 7 $ */ public interface ObjectRepository { - Object getObjectNamed(String string); + Object getObjectNamed(String string); } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/SerializeUtils.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/SerializeUtils.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/SerializeUtils.java index 99c9626..5030c55 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/SerializeUtils.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/core/SerializeUtils.java @@ -37,76 +37,76 @@ import java.util.zip.GZIPOutputStream; /** * Class implementing some serialize utility methods. - * + * * @author Richard Kirkby ([email protected]) * @version $Revision: 7 $ */ public class SerializeUtils { - public static class ByteCountingOutputStream extends OutputStream { - - protected int numBytesWritten = 0; - - public int getNumBytesWritten() { - return this.numBytesWritten; - } - - @Override - public void write(int b) throws IOException { - this.numBytesWritten++; - } + public static class ByteCountingOutputStream extends OutputStream { - @Override - public void write(byte[] b, int off, int len) throws IOException { - this.numBytesWritten += len; - } + protected int numBytesWritten = 0; - @Override - public void write(byte[] b) throws IOException { - this.numBytesWritten += b.length; - } + public int getNumBytesWritten() { + return this.numBytesWritten; } - public static void writeToFile(File file, Serializable obj) - throws IOException { - ObjectOutputStream out = new ObjectOutputStream(new GZIPOutputStream( - new BufferedOutputStream(new FileOutputStream(file)))); - out.writeObject(obj); - out.flush(); - out.close(); + @Override + public void write(int b) throws IOException { + this.numBytesWritten++; } - public static Object readFromFile(File file) throws IOException, - ClassNotFoundException { - ObjectInputStream in = new ObjectInputStream(new GZIPInputStream( - new BufferedInputStream(new FileInputStream(file)))); - Object obj = in.readObject(); - in.close(); - return obj; + @Override + public void write(byte[] b, int off, int len) throws IOException { + this.numBytesWritten += len; } - public static Object copyObject(Serializable obj) throws Exception { - ByteArrayOutputStream baoStream = new ByteArrayOutputStream(); - ObjectOutputStream out = new ObjectOutputStream( - new BufferedOutputStream(baoStream)); - out.writeObject(obj); - out.flush(); - out.close(); - byte[] byteArray = baoStream.toByteArray(); - ObjectInputStream in = new ObjectInputStream(new BufferedInputStream( - new ByteArrayInputStream(byteArray))); - Object copy = in.readObject(); - in.close(); - return copy; + @Override + public void write(byte[] b) throws IOException { + this.numBytesWritten += b.length; } + } - public static int measureObjectByteSize(Serializable obj) throws Exception { - ByteCountingOutputStream bcoStream = new ByteCountingOutputStream(); - ObjectOutputStream out = new ObjectOutputStream( - new BufferedOutputStream(bcoStream)); - out.writeObject(obj); - out.flush(); - out.close(); - return bcoStream.getNumBytesWritten(); - } + public static void writeToFile(File file, Serializable obj) + throws IOException { + ObjectOutputStream out = new ObjectOutputStream(new GZIPOutputStream( + new BufferedOutputStream(new FileOutputStream(file)))); + out.writeObject(obj); + out.flush(); + out.close(); + } + + public static Object readFromFile(File file) throws IOException, + ClassNotFoundException { + ObjectInputStream in = new ObjectInputStream(new GZIPInputStream( + new BufferedInputStream(new FileInputStream(file)))); + Object obj = in.readObject(); + in.close(); + return obj; + } + + public static Object copyObject(Serializable obj) throws Exception { + ByteArrayOutputStream baoStream = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream( + new BufferedOutputStream(baoStream)); + out.writeObject(obj); + out.flush(); + out.close(); + byte[] byteArray = baoStream.toByteArray(); + ObjectInputStream in = new ObjectInputStream(new BufferedInputStream( + new ByteArrayInputStream(byteArray))); + Object copy = in.readObject(); + in.close(); + return copy; + } + + public static int measureObjectByteSize(Serializable obj) throws Exception { + ByteCountingOutputStream bcoStream = new ByteCountingOutputStream(); + ObjectOutputStream out = new ObjectOutputStream( + new BufferedOutputStream(bcoStream)); + out.writeObject(obj); + out.flush(); + out.close(); + return bcoStream.getNumBytesWritten(); + } }
