public void testStddev()
        {
            IRunningAverageAndStdDev runningAverage = new FullRunningAverageAndStdDev();

            Assert.AreEqual(0, runningAverage.GetCount());
            Assert.True(Double.IsNaN(runningAverage.GetAverage()));
            runningAverage.AddDatum(1.0);
            Assert.AreEqual(1, runningAverage.GetCount());
            Assert.AreEqual(1.0, runningAverage.GetAverage(), EPSILON);
            Assert.True(Double.IsNaN(runningAverage.GetStandardDeviation()));
            runningAverage.AddDatum(1.0);
            Assert.AreEqual(2, runningAverage.GetCount());
            Assert.AreEqual(1.0, runningAverage.GetAverage(), EPSILON);
            Assert.AreEqual(0.0, runningAverage.GetStandardDeviation(), EPSILON);

            runningAverage.AddDatum(7.0);
            Assert.AreEqual(3, runningAverage.GetCount());
            Assert.AreEqual(3.0, runningAverage.GetAverage(), EPSILON);
            Assert.AreEqual(3.464101552963257, runningAverage.GetStandardDeviation(), EPSILON);

            runningAverage.AddDatum(5.0);
            Assert.AreEqual(4, runningAverage.GetCount());
            Assert.AreEqual(3.5, runningAverage.GetAverage(), EPSILON);
            Assert.AreEqual(3.0, runningAverage.GetStandardDeviation(), EPSILON);
        }
        public void testFullBig()
        {
            IRunningAverageAndStdDev average = new FullRunningAverageAndStdDev();

            var r = RandomUtils.getRandom();
            for (int i = 0; i < 1000000; i++) {
              average.AddDatum(r.nextDouble() * 1000.0);
            }
            Assert.AreEqual(500.0, average.GetAverage(), SMALL_EPSILON);
            Assert.AreEqual(1000.0 / Math.Sqrt(12.0), average.GetStandardDeviation(), SMALL_EPSILON);
        }
        public void testFull()
        {
            IRunningAverageAndStdDev average = new FullRunningAverageAndStdDev();

            Assert.AreEqual(0, average.GetCount());
            Assert.True(Double.IsNaN(average.GetAverage()));
            Assert.True(Double.IsNaN(average.GetStandardDeviation()));

            average.AddDatum(6.0);
            Assert.AreEqual(1, average.GetCount());
            Assert.AreEqual(6.0, average.GetAverage(), EPSILON);
            Assert.True(Double.IsNaN(average.GetStandardDeviation()));

            average.AddDatum(6.0);
            Assert.AreEqual(2, average.GetCount());
            Assert.AreEqual(6.0, average.GetAverage(), EPSILON);
            Assert.AreEqual(0.0, average.GetStandardDeviation(), EPSILON);

            average.RemoveDatum(6.0);
            Assert.AreEqual(1, average.GetCount());
            Assert.AreEqual(6.0, average.GetAverage(), EPSILON);
            Assert.True(Double.IsNaN(average.GetStandardDeviation()));

            average.AddDatum(-4.0);
            Assert.AreEqual(2, average.GetCount());
            Assert.AreEqual(1.0, average.GetAverage(), EPSILON);
            Assert.AreEqual(5.0 * 1.4142135623730951, average.GetStandardDeviation(), EPSILON);

            average.RemoveDatum(4.0);
            Assert.AreEqual(1, average.GetCount());
            Assert.AreEqual(-2.0, average.GetAverage(), EPSILON);
            Assert.True(Double.IsNaN(average.GetStandardDeviation()));
        }
        public void testFull()
        {
            IRunningAverageAndStdDev average = new FullRunningAverageAndStdDev();

            Assert.AreEqual(0, average.GetCount());
            Assert.True(Double.IsNaN(average.GetAverage()));
            Assert.True(Double.IsNaN(average.GetStandardDeviation()));

            average.AddDatum(6.0);
            Assert.AreEqual(1, average.GetCount());
            Assert.AreEqual(6.0, average.GetAverage(), EPSILON);
            Assert.True(Double.IsNaN(average.GetStandardDeviation()));

            average.AddDatum(6.0);
            Assert.AreEqual(2, average.GetCount());
            Assert.AreEqual(6.0, average.GetAverage(), EPSILON);
            Assert.AreEqual(0.0, average.GetStandardDeviation(), EPSILON);

            average.RemoveDatum(6.0);
            Assert.AreEqual(1, average.GetCount());
            Assert.AreEqual(6.0, average.GetAverage(), EPSILON);
            Assert.True(Double.IsNaN(average.GetStandardDeviation()));

            average.AddDatum(-4.0);
            Assert.AreEqual(2, average.GetCount());
            Assert.AreEqual(1.0, average.GetAverage(), EPSILON);
            Assert.AreEqual(5.0 * 1.4142135623730951, average.GetStandardDeviation(), EPSILON);

            average.RemoveDatum(4.0);
            Assert.AreEqual(1, average.GetCount());
            Assert.AreEqual(-2.0, average.GetAverage(), EPSILON);
            Assert.True(Double.IsNaN(average.GetStandardDeviation()));
        }
        public void testFullBig()
        {
            IRunningAverageAndStdDev average = new FullRunningAverageAndStdDev();

            var r = RandomUtils.getRandom();

            for (int i = 0; i < 1000000; i++)
            {
                average.AddDatum(r.nextDouble() * 1000.0);
            }
            Assert.AreEqual(500.0, average.GetAverage(), SMALL_EPSILON);
            Assert.AreEqual(1000.0 / Math.Sqrt(12.0), average.GetStandardDeviation(), SMALL_EPSILON);
        }
        public void testStddev()
        {
            IRunningAverageAndStdDev runningAverage = new FullRunningAverageAndStdDev();

            Assert.AreEqual(0, runningAverage.GetCount());
            Assert.True(Double.IsNaN(runningAverage.GetAverage()));
            runningAverage.AddDatum(1.0);
            Assert.AreEqual(1, runningAverage.GetCount());
            Assert.AreEqual(1.0, runningAverage.GetAverage(), EPSILON);
            Assert.True(Double.IsNaN(runningAverage.GetStandardDeviation()));
            runningAverage.AddDatum(1.0);
            Assert.AreEqual(2, runningAverage.GetCount());
            Assert.AreEqual(1.0, runningAverage.GetAverage(), EPSILON);
            Assert.AreEqual(0.0, runningAverage.GetStandardDeviation(), EPSILON);

            runningAverage.AddDatum(7.0);
            Assert.AreEqual(3, runningAverage.GetCount());
            Assert.AreEqual(3.0, runningAverage.GetAverage(), EPSILON);
            Assert.AreEqual(3.464101552963257, runningAverage.GetStandardDeviation(), EPSILON);

            runningAverage.AddDatum(5.0);
            Assert.AreEqual(4, runningAverage.GetCount());
            Assert.AreEqual(3.5, runningAverage.GetAverage(), EPSILON);
            Assert.AreEqual(3.0, runningAverage.GetStandardDeviation(), EPSILON);
        }
 private static double computeThreshold(IPreferenceArray prefs) {
   if (prefs.Length() < 2) {
     // Not enough data points -- return a threshold that allows everything
     return Double.NegativeInfinity;
   }
   IRunningAverageAndStdDev stdDev = new FullRunningAverageAndStdDev();
   int size = prefs.Length();
   for (int i = 0; i < size; i++) {
     stdDev.AddDatum(prefs.GetValue(i));
   }
   return stdDev.GetAverage() + stdDev.GetStandardDeviation();
 }