add variance to notification usage stats
Change-Id: I9b6abd08cf01a1d80c8e5dd581c8e82e019542fc
diff --git a/services/core/java/com/android/server/notification/NotificationUsageStats.java b/services/core/java/com/android/server/notification/NotificationUsageStats.java
index faa5a43..d9e2b91 100644
--- a/services/core/java/com/android/server/notification/NotificationUsageStats.java
+++ b/services/core/java/com/android/server/notification/NotificationUsageStats.java
@@ -20,7 +20,6 @@
import android.os.SystemClock;
import android.service.notification.StatusBarNotification;
-import android.util.Log;
import java.io.PrintWriter;
import java.util.HashMap;
@@ -250,21 +249,28 @@
*/
public static class Aggregate {
long numSamples;
- long sum;
- long avg;
+ double avg;
+ double sum2;
+ double var;
public void addSample(long sample) {
+ // Welford's "Method for Calculating Corrected Sums of Squares"
+ // http://www.jstor.org/stable/1266577?seq=2
numSamples++;
- sum += sample;
- avg = sum / numSamples;
+ final double n = numSamples;
+ final double delta = sample - avg;
+ avg += (1.0 / n) * delta;
+ sum2 += ((n - 1) / n) * delta * delta;
+ final double divisor = numSamples == 1 ? 1.0 : n - 1.0;
+ var = sum2 / divisor;
}
@Override
public String toString() {
return "Aggregate{" +
"numSamples=" + numSamples +
- ", sum=" + sum +
", avg=" + avg +
+ ", var=" + var +
'}';
}
}