Commits

Devin Martin  committed 323ee7c

Improve stat extensions. Create standard deviation filter

  • Participants
  • Parent commits 079e0c5

Comments (0)

Files changed (2)

File FlipSideOut.Linqpad/Histogram.cs

     /// </remarks>
     public class Histogram
     {
-        public static byte[] CreateHistogram(IEnumerable<int> data, int size)
+        public static byte[] CreateHistogram(IEnumerable<int> data, int size, bool ignoreOutliers = false)
         {
-            var histogramData = CreateHistogramData(data);
+            var histogramData = CreateHistogramData(data, ignoreOutliers);
             return CreateHistogramGraph(size, histogramData);
         }
 
             return results;
         }
 
-        private static int[] CreateHistogramData(IEnumerable<int> data)
+        private static int[] CreateHistogramData(IEnumerable<int> data, bool ignoreOutliers)
         {
+            if (ignoreOutliers)
+            {
+                data = data.WhereWithinStandardDeviations();
+            }
+
             var query = from datum in data
                         group datum by datum into aggregateData
                         select new { Key = aggregateData.Key, Count = aggregateData.Count() };

File FlipSideOut.Linqpad/StatisticalExtensions.cs

         /// <summary>
         /// Calculates the standard deviation
         /// </summary>
-        /// <param name="set"></param>
         /// <returns></returns>
-        public static double StandardDeviation(IEnumerable<double> set)
+        public static double StandardDeviation(this IEnumerable<double> set, out double average)
         {
             var data = set.ToArray();
-            double average = data.Average();
+            average = data.Average();
             double sumOfDerivation = 0;
             foreach (double value in data)
             {
                 sumOfDerivation += (value) * (value);
             }
             double sumOfDerivationAverage = sumOfDerivation / (data.Count() - 1);
-            return Math.Sqrt(sumOfDerivationAverage - (average * average));  
+            return Math.Sqrt(sumOfDerivationAverage - (average * average));
+        }
+
+        /// <summary>
+        /// Calculates the standard deviation
+        /// </summary>
+        /// <param name="set"></param>
+        /// <returns></returns>
+        public static double StandardDeviation(this IEnumerable<double> set)
+        {
+            double average;
+            return set.StandardDeviation(out average);
+        }
+
+        public static IEnumerable<double> WhereWithinStandardDeviations(this IEnumerable<double> set, int standardDeviations = 3)
+        {
+            var data = set.ToArray();
+            double average;
+            var standardDev = data.StandardDeviation(out average);
+            double min = average - (standardDeviations * standardDev);
+            double max = average + (standardDeviations * standardDev);
+            return data.Where(a => a <= max && a >= min);
+        }
+
+        public static IEnumerable<int> WhereWithinStandardDeviations(this IEnumerable<int> set, int standardDeviations = 3)
+        {
+            var d = (double)1;
+            var data = set.ToArray();
+            double average;
+            var standardDev = data.Select(a => (double)a).StandardDeviation(out average);
+            double min = average - (standardDeviations * standardDev);
+            double max = average + (standardDeviations * standardDev);
+            return data.Where(a => a <= max && a >= min);
         }
     }
 }