public Statistics(IEnumerable<double> values)
        {
            list = values.ToList();
            N = list.Count;
            if (N == 0)
                throw new InvalidOperationException("StatSummary: Sequence contains no elements");
            list.Sort();

            if (N == 1)
                Q1 = Median = Q3 = list[0];
            else
            {
                Func<IList<double>, double> getMedian = x => x.Count % 2 == 0
                    ? (x[x.Count / 2 - 1] + x[x.Count / 2]) / 2
                    : x[x.Count / 2];
                Median = getMedian(list);
                Q1 = getMedian(list.Take(N / 2).ToList());
                Q3 = getMedian(list.Skip((N + 1) / 2).ToList());
            }

            Min = list.First();
            Mean = list.Average();
            Max = list.Last();

            InterquartileRange = Q3 - Q1;
            LowerFence = Q1 - 1.5 * InterquartileRange;
            UpperFence = Q3 + 1.5 * InterquartileRange;

            Outliers = list.Where(IsOutlier).ToArray();

            StandardDeviation = N == 1 ? 0 : Math.Sqrt(list.Sum(d => Math.Pow(d - Mean, 2)) / (N - 1));
            StandardError = StandardDeviation / Math.Sqrt(N);
            ConfidenceInterval = new ConfidenceInterval(Mean, StandardError);
            Percentiles = new PercentileValues(list);
        }
        public static MeasurementsStatistics Calculate(List <Measurement> measurements, bool removeOutliers)
        {
            int n = measurements.Count;

            if (n == 0)
            {
                throw new InvalidOperationException("StatSummary: Sequence contains no elements");
            }

            double sum  = Sum(measurements);
            double mean = sum / n;

            double variance           = Variance(measurements, n, mean);
            double standardDeviation  = Math.Sqrt(variance);
            double standardError      = standardDeviation / Math.Sqrt(n);
            var    confidenceInterval = new ConfidenceInterval(mean, standardError, n);

            if (!removeOutliers) // most simple scenario is done without allocations! but this is not the default case
            {
                return(new MeasurementsStatistics(standardError, mean, confidenceInterval));
            }

            measurements.Sort(); // sort in place

            double q1, median, q3;

            if (n == 1)
            {
                q1 = median = q3 = measurements[0].Nanoseconds;
            }
            else
            {
                q1     = GetQuartile(measurements, measurements.Count / 2);
                median = GetQuartile(measurements, measurements.Count);
                q3     = GetQuartile(measurements, measurements.Count * 3 / 2);
            }

            double interquartileRange = q3 - q1;
            double lowerFence         = q1 - 1.5 * interquartileRange;
            double upperFence         = q3 + 1.5 * interquartileRange;

            SumWithoutOutliers(measurements, lowerFence, upperFence, out sum, out n); // updates sum and N
            mean = sum / n;

            variance           = VarianceWithoutOutliers(measurements, n, mean, lowerFence, upperFence);
            standardDeviation  = Math.Sqrt(variance);
            standardError      = standardDeviation / Math.Sqrt(n);
            confidenceInterval = new ConfidenceInterval(mean, standardError, n);

            return(new MeasurementsStatistics(standardError, mean, confidenceInterval));
        }
Beispiel #3
0
        public Statistics(IEnumerable <double> values)
        {
            originalValues = values.Where(d => !double.IsNaN(d)).ToList();
            sortedValues   = originalValues.ToList();
            N = sortedValues.Count;
            if (N == 0)
            {
                throw new InvalidOperationException("Sequence of values contains no elements, Statistics can't be calculated");
            }
            sortedValues.Sort();

            if (N == 1)
            {
                Q1 = Median = Q3 = sortedValues[0];
            }
            else
            {
                double GetMedian(IList <double> x) => x.Count % 2 == 0
                    ? (x[x.Count / 2 - 1] + x[x.Count / 2]) / 2
                    : x[x.Count / 2];

                Median = GetMedian(sortedValues);
                Q1     = GetMedian(sortedValues.Take(N / 2).ToList());
                Q3     = GetMedian(sortedValues.Skip((N + 1) / 2).ToList());
            }

            Min  = sortedValues.First();
            Mean = sortedValues.Average();
            Max  = sortedValues.Last();

            InterquartileRange = Q3 - Q1;
            LowerFence         = Q1 - 1.5 * InterquartileRange;
            UpperFence         = Q3 + 1.5 * InterquartileRange;

            AllOutliers   = sortedValues.Where(IsOutlier).ToArray();
            LowerOutliers = sortedValues.Where(IsLowerOutlier).ToArray();
            UpperOutliers = sortedValues.Where(IsUpperOutlier).ToArray();

            Variance           = N == 1 ? 0 : sortedValues.Sum(d => Math.Pow(d - Mean, 2)) / (N - 1);
            StandardDeviation  = Math.Sqrt(Variance);
            StandardError      = StandardDeviation / Math.Sqrt(N);
            Skewness           = CalcCentralMoment(3) / StandardDeviation.Pow(3);
            Kurtosis           = CalcCentralMoment(4) / StandardDeviation.Pow(4);
            ConfidenceInterval = new ConfidenceInterval(Mean, StandardError, N);
            Percentiles        = new PercentileValues(sortedValues);
        }
        public Statistics(IEnumerable <double> values)
        {
            list = values.ToList();
            N    = list.Count;
            if (N == 0)
            {
                throw new InvalidOperationException("Sequence of values contains no elements, Statistics can't be calculated");
            }
            list.Sort();

            if (N == 1)
            {
                Q1 = Median = Q3 = list[0];
            }
            else
            {
                Func <IList <double>, double> getMedian = x => x.Count % 2 == 0
                    ? (x[x.Count / 2 - 1] + x[x.Count / 2]) / 2
                    : x[x.Count / 2];
                Median = getMedian(list);
                Q1     = getMedian(list.Take(N / 2).ToList());
                Q3     = getMedian(list.Skip((N + 1) / 2).ToList());
            }

            Min  = list.First();
            Mean = list.Average();
            Max  = list.Last();

            InterquartileRange = Q3 - Q1;
            LowerFence         = Q1 - 1.5 * InterquartileRange;
            UpperFence         = Q3 + 1.5 * InterquartileRange;

            Outliers = list.Where(IsOutlier).ToArray();

            Variance           = N == 1 ? 0 : list.Sum(d => Math.Pow(d - Mean, 2)) / (N - 1);
            StandardDeviation  = Math.Sqrt(Variance);
            StandardError      = StandardDeviation / Math.Sqrt(N);
            Skewness           = CalcCentralMoment(3) / StandardDeviation.Pow(3);
            Kurtosis           = CalcCentralMoment(4) / StandardDeviation.Pow(4);
            ConfidenceInterval = new ConfidenceInterval(Mean, StandardError);
            Percentiles        = new PercentileValues(list);
        }
        public Statistics(IEnumerable <double> values)
        {
            OriginalValues = values.Where(d => !double.IsNaN(d)).ToArray();
            SortedValues   = OriginalValues.OrderBy(value => value).ToArray();
            N = SortedValues.Count;
            if (N == 0)
            {
                throw new InvalidOperationException("Sequence of values contains no elements, Statistics can't be calculated");
            }

            var quartiles = Quartiles.FromSorted(SortedValues);

            Min                = quartiles.Min;
            Q1                 = quartiles.Q1;
            Median             = quartiles.Median;
            Q3                 = quartiles.Q3;
            Max                = quartiles.Max;
            InterquartileRange = quartiles.InterquartileRange;

            var moments = Moments.Create(SortedValues);

            Mean = moments.Mean;
            StandardDeviation = moments.StandardDeviation;
            Variance          = moments.Variance;
            Skewness          = moments.Skewness;
            Kurtosis          = moments.Kurtosis;

            var tukey = TukeyOutlierDetector.FromQuartiles(quartiles);

            LowerFence      = tukey.LowerFence;
            UpperFence      = tukey.UpperFence;
            AllOutliers     = SortedValues.Where(tukey.IsOutlier).ToArray();
            LowerOutliers   = SortedValues.Where(tukey.IsLowerOutlier).ToArray();
            UpperOutliers   = SortedValues.Where(tukey.IsUpperOutlier).ToArray();
            outlierDetector = tukey;

            StandardError      = StandardDeviation / Math.Sqrt(N);
            ConfidenceInterval = new ConfidenceInterval(Mean, StandardError, N);
            Percentiles        = new PercentileValues(SortedValues);
        }
Beispiel #6
0
        public Statistics(IEnumerable <double> values)
        {
            list = values.ToList();
            N    = list.Count;
            if (N == 0)
            {
                throw new InvalidOperationException("StatSummary: Sequence contains no elements");
            }
            list.Sort();

            if (N == 1)
            {
                Q1 = Median = Q3 = list[0];
            }
            else
            {
                Func <IList <double>, double> getMedian = x => x.Count % 2 == 0
                    ? (x[x.Count / 2 - 1] + x[x.Count / 2]) / 2
                    : x[x.Count / 2];
                Median = getMedian(list);
                Q1     = getMedian(list.Take(N / 2).ToList());
                Q3     = getMedian(list.Skip((N + 1) / 2).ToList());
            }

            Min  = list.First();
            Mean = list.Average();
            Max  = list.Last();

            InterquartileRange = Q3 - Q1;
            LowerFence         = Q1 - 1.5 * InterquartileRange;
            UpperFence         = Q3 + 1.5 * InterquartileRange;

            Outliers = list.Where(IsOutlier).ToArray();

            StandardDeviation  = N == 1 ? 0 : Math.Sqrt(list.Sum(d => Math.Pow(d - Mean, 2)) / (N - 1));
            StandardError      = StandardDeviation / Math.Sqrt(N);
            ConfidenceInterval = new ConfidenceInterval(Mean, StandardError);
        }
 private MeasurementsStatistics(double standardError, double mean, ConfidenceInterval confidenceInterval)
 {
     StandardError      = standardError;
     Mean               = mean;
     ConfidenceInterval = confidenceInterval;
 }