/// <summary> /// Evaluates the sample mean over a moving window, for each samples. /// Returns NaN if no data is empty or if any entry is NaN. /// </summary> /// <param name="samples">The sample stream to calculate the mean of.</param> /// <param name="windowSize">The number of last samples to consider.</param> public static IEnumerable <double> MovingAverage(this IEnumerable <double> samples, int windowSize) { var movingStatistics = new MovingStatistics(windowSize); return(samples.Select(sample => { movingStatistics.Push(sample); return movingStatistics.Mean; })); }
public void PositiveInfinityTest() { var ms = new MovingStatistics(3); ms.Push(1.0); ms.Push(2.0); Assert.That(ms.Minimum, Is.Not.EqualTo(double.PositiveInfinity)); Assert.That(ms.Maximum, Is.Not.EqualTo(double.PositiveInfinity)); Assert.That(ms.Mean, Is.Not.EqualTo(double.PositiveInfinity)); Assert.That(ms.StandardDeviation, Is.Not.EqualTo(double.PositiveInfinity)); ms.Push(double.PositiveInfinity); Assert.That(ms.Minimum, Is.Not.EqualTo(double.PositiveInfinity)); Assert.That(ms.Maximum, Is.EqualTo(double.PositiveInfinity)); Assert.That(ms.Mean, Is.EqualTo(double.PositiveInfinity)); Assert.That(ms.StandardDeviation, Is.EqualTo(double.PositiveInfinity)); ms.Push(1.0); Assert.That(ms.Minimum, Is.Not.EqualTo(double.PositiveInfinity)); Assert.That(ms.Maximum, Is.EqualTo(double.PositiveInfinity)); Assert.That(ms.Mean, Is.EqualTo(double.PositiveInfinity)); Assert.That(ms.StandardDeviation, Is.EqualTo(double.PositiveInfinity)); ms.Push(double.PositiveInfinity); Assert.That(ms.Minimum, Is.Not.EqualTo(double.PositiveInfinity)); Assert.That(ms.Maximum, Is.EqualTo(double.PositiveInfinity)); Assert.That(ms.Mean, Is.EqualTo(double.PositiveInfinity)); Assert.That(ms.StandardDeviation, Is.EqualTo(double.PositiveInfinity)); ms.Push(2.0); Assert.That(ms.Minimum, Is.Not.EqualTo(double.PositiveInfinity)); Assert.That(ms.Maximum, Is.EqualTo(double.PositiveInfinity)); Assert.That(ms.Mean, Is.EqualTo(double.PositiveInfinity)); Assert.That(ms.StandardDeviation, Is.EqualTo(double.PositiveInfinity)); ms.Push(3.0); Assert.That(ms.Minimum, Is.Not.EqualTo(double.PositiveInfinity)); Assert.That(ms.Maximum, Is.EqualTo(double.PositiveInfinity)); Assert.That(ms.Mean, Is.EqualTo(double.PositiveInfinity)); Assert.That(ms.StandardDeviation, Is.EqualTo(double.PositiveInfinity)); ms.Push(4.0); Assert.That(ms.Minimum, Is.Not.EqualTo(double.PositiveInfinity)); Assert.That(ms.Maximum, Is.Not.EqualTo(double.PositiveInfinity)); Assert.That(ms.Mean, Is.Not.EqualTo(double.PositiveInfinity)); Assert.That(ms.StandardDeviation, Is.Not.EqualTo(double.PositiveInfinity)); }
/// <summary> /// Evaluates the sample mean over a moving window, for each samples. /// Returns NaN if no data is empty or if any entry is NaN. /// </summary> /// <param name="samples">The sample stream to calculate the mean of.</param> /// <param name="windowSize">The number of last samples to consider.</param> public static IEnumerable<double> MovingAverage(this IEnumerable<double> samples, int windowSize) { var movingStatistics = new MovingStatistics(windowSize); return samples.Select(sample => { movingStatistics.Push(sample); return movingStatistics.Mean; }); }
public void NaNTest() { var ms = new MovingStatistics(3); Assert.That(ms.Minimum, Is.NaN); Assert.That(ms.Maximum, Is.NaN); Assert.That(ms.Mean, Is.NaN); Assert.That(ms.StandardDeviation, Is.NaN); ms.Push(1.0); Assert.That(ms.Minimum, Is.Not.NaN); Assert.That(ms.Maximum, Is.Not.NaN); Assert.That(ms.Mean, Is.Not.NaN); Assert.That(ms.StandardDeviation, Is.NaN); ms.Push(2.0); Assert.That(ms.Minimum, Is.Not.NaN); Assert.That(ms.Maximum, Is.Not.NaN); Assert.That(ms.Mean, Is.Not.NaN); Assert.That(ms.StandardDeviation, Is.Not.NaN); ms.Push(double.NaN); Assert.That(ms.Minimum, Is.NaN); Assert.That(ms.Maximum, Is.NaN); Assert.That(ms.Mean, Is.NaN); Assert.That(ms.StandardDeviation, Is.NaN); ms.Push(1.0); Assert.That(ms.Minimum, Is.NaN); Assert.That(ms.Maximum, Is.NaN); Assert.That(ms.Mean, Is.NaN); Assert.That(ms.StandardDeviation, Is.NaN); ms.Push(2.0); Assert.That(ms.Minimum, Is.NaN); Assert.That(ms.Maximum, Is.NaN); Assert.That(ms.Mean, Is.NaN); Assert.That(ms.StandardDeviation, Is.NaN); ms.Push(3.0); Assert.That(ms.Minimum, Is.Not.NaN); Assert.That(ms.Maximum, Is.Not.NaN); Assert.That(ms.Mean, Is.Not.NaN); Assert.That(ms.StandardDeviation, Is.Not.NaN); }
public void StabilityTest() { var data = new double[1000000]; Normal.Samples(new SystemRandomSource(0), data, 50, 10); var ms = new MovingStatistics(5, data); ms.PushRange(new[] { 11.11, 22.22, 33.33, 44.44, 55.55 }); Assert.AreEqual(5, ms.Count); Assert.AreEqual(11.11, ms.Minimum); Assert.AreEqual(55.55, ms.Maximum); Assert.AreEqual(33.33, ms.Mean, 1e-11); Assert.AreEqual(308.58025, ms.Variance, 1e-10); Assert.AreEqual(17.5664524022354, ms.StandardDeviation, 1e-11); Assert.AreEqual(246.8642, ms.PopulationVariance, 1e-10); Assert.AreEqual(15.7119126779651, ms.PopulationStandardDeviation, 1e-10); }