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.Linq;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Common {
|
---|
27 | public static class EnumerableStatisticExtensions {
|
---|
28 | /// <summary>
|
---|
29 | /// Calculates the median element of the enumeration.
|
---|
30 | /// </summary>
|
---|
31 | /// <param name="values"></param>
|
---|
32 | /// <returns></returns>
|
---|
33 | public static double Median(this IEnumerable<double> values) {
|
---|
34 | int n = values.Count();
|
---|
35 | if (n == 0) throw new InvalidOperationException("Enumeration contains no elements.");
|
---|
36 |
|
---|
37 | double[] sortedValues = new double[n];
|
---|
38 | int i = 0;
|
---|
39 | foreach (double x in values)
|
---|
40 | sortedValues[i++] = x;
|
---|
41 |
|
---|
42 | Array.Sort(sortedValues);
|
---|
43 |
|
---|
44 | // return the middle element (if n is uneven) or the average of the two middle elements if n is even.
|
---|
45 | if (n % 2 == 1) {
|
---|
46 | return sortedValues[n / 2];
|
---|
47 | } else {
|
---|
48 | return (sortedValues[(n / 2) - 1] + sortedValues[n / 2]) / 2.0;
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | /// <summary>
|
---|
54 | /// Calculates the standard deviation of values.
|
---|
55 | /// </summary>
|
---|
56 | /// <param name="values"></param>
|
---|
57 | /// <returns></returns>
|
---|
58 | public static double StandardDeviation(this IEnumerable<double> values) {
|
---|
59 | return Math.Sqrt(Variance(values));
|
---|
60 | }
|
---|
61 |
|
---|
62 | /// <summary>
|
---|
63 | /// Calculates the variance of values. (sum (x - x_mean)² / n)
|
---|
64 | /// </summary>
|
---|
65 | /// <param name="values"></param>
|
---|
66 | /// <returns></returns>
|
---|
67 | public static double Variance(this IEnumerable<double> values) {
|
---|
68 | int m_n = 0;
|
---|
69 | double m_oldM = 0.0;
|
---|
70 | double m_newM = 0.0;
|
---|
71 | double m_oldS = 0.0;
|
---|
72 | double m_newS = 0.0;
|
---|
73 | foreach (double x in values) {
|
---|
74 | m_n++;
|
---|
75 | if (m_n == 1) {
|
---|
76 | m_oldM = m_newM = x;
|
---|
77 | m_oldS = 0.0;
|
---|
78 | } else {
|
---|
79 | m_newM = m_oldM + (x - m_oldM) / m_n;
|
---|
80 | m_newS = m_oldS + (x - m_oldM) * (x - m_newM);
|
---|
81 |
|
---|
82 | // set up for next iteration
|
---|
83 | m_oldM = m_newM;
|
---|
84 | m_oldS = m_newS;
|
---|
85 | }
|
---|
86 | }
|
---|
87 | return ((m_n > 1) ? m_newS / (m_n - 1) : 0.0);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|