Permalink
Browse files

Tracking for min and max values

  • Loading branch information...
1 parent b5cfaac commit c98ca55739a5d7827d74c54e90f6616bf7976d46 @ashenfad ashenfad committed May 10, 2012
Showing with 46 additions and 1 deletion.
  1. +1 −1 project.clj
  2. +10 −0 src/clj/histogram/core.clj
  3. +25 −0 src/java/com/bigml/histogram/Histogram.java
  4. +10 −0 test/histogram/test/core.clj
View
@@ -1,4 +1,4 @@
-(defproject histogram "1.9.1"
+(defproject histogram "1.9.2"
:description "Dynamic/streaming histograms"
:source-path "src/clj"
:java-source-path "src/java"
@@ -241,6 +241,16 @@
(conj bins (missing-bin hist))
bins)))
+(defn minimum
+ "Returns the minimum value inserted into the histogram."
+ [^Histogram hist]
+ (.getMinimum hist))
+
+(defn maximum
+ "Returns the maximum value inserted into the histogram."
+ [^Histogram hist]
+ (.getMaximum hist))
+
(defn bounds
"Returns the bounds of the histogram, nil if the histogram is empty.
An optional parameter may be supplied to enable a small buffer to
@@ -49,6 +49,8 @@ public Histogram(int maxBins, boolean countWeightedGaps,
_decimalFormat = new DecimalFormat(DEFAULT_FORMAT_STRING);
_countWeightedGaps = countWeightedGaps;
_missingCount = 0;
+ _minimum = null;
+ _maximum = null;
if (categories != null && !categories.isEmpty()) {
_targetType = TargetType.categorical;
@@ -550,6 +552,20 @@ public T getMissingTarget() {
_missingCount += count;
return this;
}
+
+ /**
+ * Returns the minimum value inserted into the histogram.
+ */
+ public Double getMinimum() {
+ return _minimum;
+ }
+
+ /**
+ * Returns the maximum value inserted into the histogram.
+ */
+ public Double getMaximum() {
+ return _maximum;
+ }
private void checkType(TargetType newType) throws MixedInsertException {
if (_targetType == null) {
@@ -563,6 +579,13 @@ private void processPointTarget(Double point, Target target) {
if (point == null) {
insertMissing(1, (T) target);
} else {
+ if (_minimum == null || _minimum > point) {
+ _minimum = point;
+ }
+ if (_maximum == null || _maximum < point) {
+ _maximum = point;
+ }
+
insertBin(new Bin(point, 1, target));
}
}
@@ -759,4 +782,6 @@ private static Double findZ(double a, double b, double c) {
private HashMap<Object, Integer> _indexMap;
private long _missingCount;
private T _missingTarget;
+ private Double _minimum;
+ private Double _maximum;
}
@@ -229,3 +229,13 @@
(insert-bin! bin1)
(missing-bin))]
(is (= bin1 bin2))))
+
+(deftest min-max-test
+ (let [hist (create)]
+ (is (nil? (minimum hist)))
+ (is (nil? (maximum hist))))
+ (let [hist (reduce insert!
+ (create)
+ (repeatedly 1000 #(rand-int 10)))]
+ (is (== 0 (minimum hist)))
+ (is (== 9 (maximum hist)))))

0 comments on commit c98ca55

Please sign in to comment.