[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[3742] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
[3452] | 25 | using System.Linq;
|
---|
[2] | 26 |
|
---|
[3462] | 27 | namespace HeuristicLab.Common {
|
---|
| 28 | public static class EnumerableStatisticExtensions {
|
---|
[2] | 29 | /// <summary>
|
---|
[3452] | 30 | /// Calculates the median element of the enumeration.
|
---|
[2] | 31 | /// </summary>
|
---|
| 32 | /// <param name="values"></param>
|
---|
| 33 | /// <returns></returns>
|
---|
[3452] | 34 | public static double Median(this IEnumerable<double> values) {
|
---|
| 35 | int n = values.Count();
|
---|
| 36 | if (n == 0) throw new InvalidOperationException("Enumeration contains no elements.");
|
---|
[2] | 37 |
|
---|
| 38 | double[] sortedValues = new double[n];
|
---|
[3452] | 39 | int i = 0;
|
---|
| 40 | foreach (double x in values)
|
---|
| 41 | sortedValues[i++] = x;
|
---|
[2] | 42 |
|
---|
| 43 | Array.Sort(sortedValues);
|
---|
| 44 |
|
---|
| 45 | // return the middle element (if n is uneven) or the average of the two middle elements if n is even.
|
---|
[1786] | 46 | if (n % 2 == 1) {
|
---|
[2] | 47 | return sortedValues[n / 2];
|
---|
| 48 | } else {
|
---|
[3452] | 49 | return (sortedValues[(n / 2) - 1] + sortedValues[n / 2]) / 2.0;
|
---|
[2] | 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 |
|
---|
| 54 | /// <summary>
|
---|
| 55 | /// Calculates the standard deviation of values.
|
---|
| 56 | /// </summary>
|
---|
| 57 | /// <param name="values"></param>
|
---|
| 58 | /// <returns></returns>
|
---|
[3452] | 59 | public static double StandardDeviation(this IEnumerable<double> values) {
|
---|
[2] | 60 | return Math.Sqrt(Variance(values));
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /// <summary>
|
---|
[3452] | 64 | /// Calculates the variance of values. (sum (x - x_mean)² / n)
|
---|
[2] | 65 | /// </summary>
|
---|
| 66 | /// <param name="values"></param>
|
---|
| 67 | /// <returns></returns>
|
---|
[3452] | 68 | public static double Variance(this IEnumerable<double> values) {
|
---|
| 69 | IList<double> list = values as IList<double>;
|
---|
| 70 | if (list == null) {
|
---|
| 71 | list = values.ToList();
|
---|
| 72 | }
|
---|
| 73 | if (list.Count == 0) throw new ArgumentException("Enumeration contains no elements.");
|
---|
[2] | 74 |
|
---|
[3452] | 75 | double mean = list.Average();
|
---|
[2] | 76 | double squaredErrorsSum = 0.0;
|
---|
[3452] | 77 | int n = list.Count;
|
---|
| 78 | int s = 0;
|
---|
| 79 | for (int i = 0; i < n; i++) {
|
---|
| 80 | if (!double.IsNaN(list[i])) {
|
---|
| 81 | double d = list[i] - mean;
|
---|
[1071] | 82 | squaredErrorsSum += d * d;
|
---|
[3452] | 83 | s++;
|
---|
[2] | 84 | }
|
---|
| 85 | }
|
---|
[3452] | 86 | if (s == 0) {
|
---|
| 87 | throw new ArgumentException("Enumeration contains no non-NaN elements.");
|
---|
[1071] | 88 | }
|
---|
[3452] | 89 | return squaredErrorsSum / n;
|
---|
[2] | 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|