|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Perfolizer.Mathematics.Common; |
| 4 | +using Perfolizer.Mathematics.OutlierDetection; |
| 5 | + |
| 6 | +namespace BitFaster.Caching.ThroughputAnalysis |
| 7 | +{ |
| 8 | + // https://github.com/dotnet/BenchmarkDotNet/blob/b4ac9df9f7890ca9669e2b9c8835af35c072a453/src/BenchmarkDotNet/Mathematics/MeasurementsStatistics.cs#L13 |
| 9 | + internal readonly ref struct MeasurementsStatistics |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// Standard error. |
| 13 | + /// </summary> |
| 14 | + public double StandardError { get; } |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Mean. |
| 18 | + /// </summary> |
| 19 | + public double Mean { get; } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// 99.9% confidence interval. |
| 23 | + /// </summary> |
| 24 | + public ConfidenceInterval ConfidenceInterval { get; } |
| 25 | + |
| 26 | + private MeasurementsStatistics(double standardError, double mean, ConfidenceInterval confidenceInterval) |
| 27 | + { |
| 28 | + StandardError = standardError; |
| 29 | + Mean = mean; |
| 30 | + ConfidenceInterval = confidenceInterval; |
| 31 | + } |
| 32 | + |
| 33 | + public static MeasurementsStatistics Calculate(List<double> measurements, OutlierMode outlierMode) |
| 34 | + { |
| 35 | + int n = measurements.Count; |
| 36 | + if (n == 0) |
| 37 | + throw new InvalidOperationException("StatSummary: Sequence contains no elements"); |
| 38 | + |
| 39 | + double sum = Sum(measurements); |
| 40 | + double mean = sum / n; |
| 41 | + |
| 42 | + double variance = Variance(measurements, n, mean); |
| 43 | + double standardDeviation = Math.Sqrt(variance); |
| 44 | + double standardError = standardDeviation / Math.Sqrt(n); |
| 45 | + var confidenceInterval = new ConfidenceInterval(mean, standardError, n); |
| 46 | + |
| 47 | + if (outlierMode == OutlierMode.DontRemove) // most simple scenario is done without allocations! but this is not the default case |
| 48 | + return new MeasurementsStatistics(standardError, mean, confidenceInterval); |
| 49 | + |
| 50 | + measurements.Sort(); // sort in place |
| 51 | + |
| 52 | + double q1, q3; |
| 53 | + |
| 54 | + if (n == 1) |
| 55 | + q1 = q3 = measurements[0]; |
| 56 | + else |
| 57 | + { |
| 58 | + q1 = GetQuartile(measurements, measurements.Count / 2); |
| 59 | + q3 = GetQuartile(measurements, measurements.Count * 3 / 2); |
| 60 | + } |
| 61 | + |
| 62 | + double interquartileRange = q3 - q1; |
| 63 | + double lowerFence = q1 - 1.5 * interquartileRange; |
| 64 | + double upperFence = q3 + 1.5 * interquartileRange; |
| 65 | + |
| 66 | + SumWithoutOutliers(outlierMode, measurements, lowerFence, upperFence, out sum, out n); // updates sum and N |
| 67 | + mean = sum / n; |
| 68 | + |
| 69 | + variance = VarianceWithoutOutliers(outlierMode, measurements, n, mean, lowerFence, upperFence); |
| 70 | + standardDeviation = Math.Sqrt(variance); |
| 71 | + standardError = standardDeviation / Math.Sqrt(n); |
| 72 | + confidenceInterval = new ConfidenceInterval(mean, standardError, n); |
| 73 | + |
| 74 | + return new MeasurementsStatistics(standardError, mean, confidenceInterval); |
| 75 | + } |
| 76 | + |
| 77 | + private static double Sum(List<double> measurements) |
| 78 | + { |
| 79 | + double sum = 0; |
| 80 | + foreach (var m in measurements) |
| 81 | + sum += m; |
| 82 | + return sum; |
| 83 | + } |
| 84 | + |
| 85 | + private static void SumWithoutOutliers(OutlierMode outlierMode, List<double> measurements, |
| 86 | + double lowerFence, double upperFence, out double sum, out int n) |
| 87 | + { |
| 88 | + sum = 0; |
| 89 | + n = 0; |
| 90 | + |
| 91 | + foreach (var m in measurements) |
| 92 | + if (!IsOutlier(outlierMode, m, lowerFence, upperFence)) |
| 93 | + { |
| 94 | + sum += m; |
| 95 | + ++n; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static double Variance(List<double> measurements, int n, double mean) |
| 100 | + { |
| 101 | + if (n == 1) |
| 102 | + return 0; |
| 103 | + |
| 104 | + double variance = 0; |
| 105 | + foreach (var m in measurements) |
| 106 | + variance += (m - mean) * (m - mean) / (n - 1); |
| 107 | + |
| 108 | + return variance; |
| 109 | + } |
| 110 | + |
| 111 | + private static double VarianceWithoutOutliers(OutlierMode outlierMode, List<double> measurements, int n, double mean, double lowerFence, double upperFence) |
| 112 | + { |
| 113 | + if (n == 1) |
| 114 | + return 0; |
| 115 | + |
| 116 | + double variance = 0; |
| 117 | + foreach (var m in measurements) |
| 118 | + if (!IsOutlier(outlierMode, m, lowerFence, upperFence)) |
| 119 | + variance += (m - mean) * (m - mean) / (n - 1); |
| 120 | + |
| 121 | + return variance; |
| 122 | + } |
| 123 | + |
| 124 | + private static double GetQuartile(List<double> measurements, int count) |
| 125 | + { |
| 126 | + if (count % 2 == 0) |
| 127 | + return (measurements[count / 2 - 1] + measurements[count / 2]) / 2; |
| 128 | + |
| 129 | + return measurements[count / 2]; |
| 130 | + } |
| 131 | + |
| 132 | + private static bool IsOutlier(OutlierMode outlierMode, double value, double lowerFence, double upperFence) |
| 133 | + { |
| 134 | + switch (outlierMode) |
| 135 | + { |
| 136 | + case OutlierMode.DontRemove: |
| 137 | + return false; |
| 138 | + case OutlierMode.RemoveUpper: |
| 139 | + return value > upperFence; |
| 140 | + case OutlierMode.RemoveLower: |
| 141 | + return value < lowerFence; |
| 142 | + case OutlierMode.RemoveAll: |
| 143 | + return value < lowerFence || value > upperFence; |
| 144 | + default: |
| 145 | + throw new ArgumentOutOfRangeException(nameof(outlierMode), outlierMode, null); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments