/// <summary> /// calculate median with resolution of .01 /// </summary> /// <param name="list"></param> /// <returns></returns> public static float fMedian_100(float[] list) { //convert to int by multiplying by 100 float maxf = Max(list); float minf = MathsExtra.Min(list); int[] ilist = new int[list.Length]; for (int i = 0; i < list.Length; i++) { ilist[i] = (int)((list[i] - minf) * 100); } int imedian = MathsExtra.Median(ilist); return(((float)imedian + minf) / 100f); ////resolution of 0.01 ////populate the histogram //float[] hist = new float[max + 1 - min]; //for (int i = 0; i < list.Length; i++) // hist[list[i] - min]++; //int median = 0; //for (int sum = 0; median < hist.Length && sum < list.Length / 2; median++) // sum += hist[median]; //return (median - 1) + min; }
/// <summary> /// defined as more than 3 std from the mean. Replace with mean /// </summary> /// <param name="list"></param> /// <returns></returns> public static double[] RemoveOutliers(double[] list) { double mean = MathsExtra.Mean(list); double stdDev = MathsExtra.StandardDeviation(list); double[] retlist = new double[list.Length]; for (int i = 0; i < list.Length; i++) { retlist[i] = Math.Abs(list[i] - mean) > stdDev * 3 ? mean : list[i]; } return(retlist); }
public static double StandardDeviation(IList <double> list) { double mean = MathsExtra.Mean(list); double sqsum = 0; for (int i = 0; i < list.Count; i++) { sqsum += (list[i] - mean) * (list[i] - mean); } return(Math.Sqrt(sqsum / (list.Count - 1))); }
public static int Median(int[] list) { int max = Max(list); int min = MathsExtra.Min(list); //populate the histogram int[] hist = new int[max + 1 - min]; for (int i = 0; i < list.Length; i++) { hist[list[i] - min]++; } int median = 0; for (int sum = 0; median < hist.Length && sum < list.Length / 2; median++) { sum += hist[median]; } return((median - 1) + min); }