Github user cestella commented on a diff in the pull request:
https://github.com/apache/incubator-metron/pull/250#discussion_r78632699
--- Diff:
metron-platform/metron-common/src/main/java/org/apache/metron/common/math/stats/OnlineStatisticsProvider.java
---
@@ -0,0 +1,243 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.metron.common.math.stats;
+
+import com.tdunning.math.stats.TDigest;
+import org.apache.commons.math3.util.FastMath;
+
+/**
+ * A (near) constant memory implementation of a statistics provider.
+ * For first order statistics, simple terms are stored and composed
+ * to return the statistics results. This is intended to provide a
+ * mergeable implementation for a statistics provider.
+ */
+public class OnlineStatisticsProvider implements StatisticsProvider {
+ /**
+ * A sensible default for compression to use in the T-Digest.
+ * As per
https://github.com/tdunning/t-digest/blob/master/src/main/java/com/tdunning/math/stats/TDigest.java#L86
+ * 100 is a sensible default and the number of centroids retained (to
construct the sketch)
+ * is usually a smallish (usually < 10) multiple of the compression.
+ */
+ public static final int COMPRESSION = 100;
+
+
+ /**
+ * A distributional sketch that uses a variant of 1-D k-means to
construct a tree of ranges
+ * that sketches the distribution. See
https://github.com/tdunning/t-digest#t-digest for
+ * more detail.
+ */
+ private TDigest digest;
+
+ private long n = 0;
+ private double sum = 0;
+ private double sumOfSquares = 0;
+ private double sumOfLogs = 0;
+ private Double min = null;
+ private Double max = null;
+
+ //\mu_1, E[X]
+ private double M1 = 0;
+ //\mu_2: E[(X - \mu)^2]
+ private double M2 = 0;
+ //\mu_3: E[(X - \mu)^3]
+ private double M3 = 0;
+ //\mu_4: E[(X - \mu)^4]
+ private double M4 = 0;
+
+ public OnlineStatisticsProvider() {
+ digest = TDigest.createAvlTreeDigest(COMPRESSION);
+ }
+
+ /**
+ * Add a value.
+ * NOTE: This does not store the point, but only updates internal state.
+ * NOTE: This is NOT threadsafe.
+ * @param value
+ */
+ @Override
+ public void addValue(double value) {
+ long n1 = n;
+ min = min == null?value:Math.min(min, value);
+ max = max == null?value:Math.max(max, value);
+ sum += value;
+ sumOfLogs += Math.log(value);
+ sumOfSquares += value*value;
--- End diff --
Yeah, I'd prefer to avoid `BigDecimal` if at all possible due to
performance concerns. Also, I will point out that Commons Math also
[uses](https://github.com/apache/commons-math/blob/050dfa6f0850174fdf958ce6751d2f06900201f8/src/main/java/org/apache/commons/math4/stat/descriptive/summary/SumOfSquares.java)
`double` for its accumulation, so I think we're pretty safe for some large
amount of headroom. That being said, I will do the math ops via `ExactMath` so
it throws an exception on overflow. Good catch.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---