Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Common/3.3/EnumerableStatisticExtensions.cs @ 5606

Last change on this file since 5606 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25
26namespace 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    /// <summary>
91    /// Calculates the pth percentile of the values.
92    /// </summary
93    public static double Percentile(this IEnumerable<double> values, double p) {
94      if (values.Count() == 0) throw new InvalidOperationException("Enumeration contains no elements.");
95      if (values.Count() == 1) return values.ElementAt(0);
96
97      double[] sortedValues = values.ToArray();
98      Array.Sort(sortedValues);
99
100      int n = sortedValues.Length;
101      if (p.IsAlmost(0.0)) return sortedValues[0];
102      if (p.IsAlmost(1.0)) return sortedValues[n - 1];
103
104      double t = p * (n - 1);
105      int index = (int)Math.Floor(t);
106      double percentage = t - index;
107      return sortedValues[index] * (1 - percentage) + sortedValues[index + 1] * percentage;
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.