Skip to content

Instantly share code, notes, and snippets.

@alex-groshev
Last active May 27, 2019 11:31
Show Gist options
  • Save alex-groshev/f433f0e0972da24c2106 to your computer and use it in GitHub Desktop.
Save alex-groshev/f433f0e0972da24c2106 to your computer and use it in GitHub Desktop.
C# Moving averages extensions (Cumulative, Simple, Exponential).
public static class MovingAverageExtensions
{
public static IEnumerable<decimal> CumulativeMovingAverage(this IEnumerable<decimal> source)
{
ulong count = 0;
decimal sum = 0;
foreach (var d in source)
{
yield return (sum += d) / ++count;
}
}
public static IEnumerable<decimal> SimpleMovingAverage(this IEnumerable<decimal> source, int length)
{
var sample = new Queue<decimal>(length);
foreach (var d in source)
{
if (sample.Count == length)
{
sample.Dequeue();
}
sample.Enqueue(d);
yield return sample.Average();
}
}
public static IEnumerable<decimal> ExponentialMovingAverage(this IEnumerable<decimal> source, int length)
{
var alpha = 2 / (decimal)(length + 1);
var s = source.ToArray();
decimal result = 0;
for (var i = 0; i < s.Length; i++)
{
result = i == 0
? s[i]
: alpha*s[i] + (1 - alpha) * result;
yield return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment