Advertisement
Willcode4cash

Standard Deviations with LINQ

Oct 31st, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.58 KB | None | 0 0
  1. public static double StdDevP(this IEnumerable<int> source)
  2. {
  3.     return StdDevLogic(source, 0);
  4. }
  5.  
  6. public static double StdDev(this IEnumerable<int> source)
  7. {
  8.     return StdDevLogic(source, 1);
  9. }
  10.  
  11. private static double StdDevLogic(this IEnumerable<int> source, int buffer = 1)
  12. {
  13.     if (source == null)
  14.     { throw new ArgumentNullException("source"); }
  15.  
  16.     var data = source.ToList();
  17.     var average = data.Average();
  18.     var differences = data.Select(u => Math.Pow(average - u, 2.0)).ToList();
  19.     return Math.Sqrt(differences.Sum() / (differences.Count() - buffer));
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement