1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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;
|
---|
25 | using System.Linq;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Common {
|
---|
28 | public static class EnumerableStatisticExtensions {
|
---|
29 | /// <summary>
|
---|
30 | /// Calculates the median element of the enumeration.
|
---|
31 | /// </summary>
|
---|
32 | /// <param name="values"></param>
|
---|
33 | /// <returns></returns>
|
---|
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.");
|
---|
37 |
|
---|
38 | double[] sortedValues = new double[n];
|
---|
39 | int i = 0;
|
---|
40 | foreach (double x in values)
|
---|
41 | sortedValues[i++] = x;
|
---|
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.
|
---|
46 | if (n % 2 == 1) {
|
---|
47 | return sortedValues[n / 2];
|
---|
48 | } else {
|
---|
49 | return (sortedValues[(n / 2) - 1] + sortedValues[n / 2]) / 2.0;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | /// <summary>
|
---|
55 | /// Calculates the standard deviation of values.
|
---|
56 | /// </summary>
|
---|
57 | /// <param name="values"></param>
|
---|
58 | /// <returns></returns>
|
---|
59 | public static double StandardDeviation(this IEnumerable<double> values) {
|
---|
60 | return Math.Sqrt(Variance(values));
|
---|
61 | }
|
---|
62 |
|
---|
63 | /// <summary>
|
---|
64 | /// Calculates the variance of values. (sum (x - x_mean)² / n)
|
---|
65 | /// </summary>
|
---|
66 | /// <param name="values"></param>
|
---|
67 | /// <returns></returns>
|
---|
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.");
|
---|
74 |
|
---|
75 | double mean = list.Average();
|
---|
76 | double squaredErrorsSum = 0.0;
|
---|
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;
|
---|
82 | squaredErrorsSum += d * d;
|
---|
83 | s++;
|
---|
84 | }
|
---|
85 | }
|
---|
86 | if (s == 0) {
|
---|
87 | throw new ArgumentException("Enumeration contains no non-NaN elements.");
|
---|
88 | }
|
---|
89 | return squaredErrorsSum / n;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|