Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.Common/3.3/EnumerableStatisticExtensions.cs @ 9333

Last change on this file since 9333 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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      // iterate only once
35      double[] valuesArr = values.ToArray();
36      int n = valuesArr.Length;
37      if (n == 0) throw new InvalidOperationException("Enumeration contains no elements.");
38
39      Array.Sort(valuesArr);
40
41      // return the middle element (if n is uneven) or the average of the two middle elements if n is even.
42      if (n % 2 == 1) {
43        return valuesArr[n / 2];
44      } else {
45        return (valuesArr[(n / 2) - 1] + valuesArr[n / 2]) / 2.0;
46      }
47    }
48
49
50    /// <summary>
51    /// Calculates the standard deviation of values.
52    /// </summary>
53    /// <param name="values"></param>
54    /// <returns></returns>
55    public static double StandardDeviation(this IEnumerable<double> values) {
56      return Math.Sqrt(Variance(values));
57    }
58
59    /// <summary>
60    /// Calculates the variance of values. (sum (x - x_mean)² / n)
61    /// </summary>
62    /// <param name="values"></param>
63    /// <returns></returns>
64    public static double Variance(this IEnumerable<double> values) {
65      int m_n = 0;
66      double m_oldM = 0.0;
67      double m_newM = 0.0;
68      double m_oldS = 0.0;
69      double m_newS = 0.0;
70      foreach (double x in values) {
71        m_n++;
72        if (m_n == 1) {
73          m_oldM = m_newM = x;
74          m_oldS = 0.0;
75        } else {
76          m_newM = m_oldM + (x - m_oldM) / m_n;
77          m_newS = m_oldS + (x - m_oldM) * (x - m_newM);
78
79          // set up for next iteration
80          m_oldM = m_newM;
81          m_oldS = m_newS;
82        }
83      }
84      return ((m_n > 1) ? m_newS / (m_n - 1) : 0.0);
85    }
86
87    /// <summary>
88    /// Calculates the pth percentile of the values.
89    /// </summary
90    public static double Percentile(this IEnumerable<double> values, double p) {
91      // iterate only once
92      double[] valuesArr = values.ToArray();
93      int n = valuesArr.Length;
94      if (n == 0) throw new InvalidOperationException("Enumeration contains no elements.");
95      if (n == 1) return values.ElementAt(0);
96
97      if (p.IsAlmost(0.0)) return valuesArr[0];
98      if (p.IsAlmost(1.0)) return valuesArr[n - 1];
99
100      double t = p * (n - 1);
101      int index = (int)Math.Floor(t);
102      double percentage = t - index;
103      return valuesArr[index] * (1 - percentage) + valuesArr[index + 1] * percentage;
104    }
105
106    public static IEnumerable<double> LimitToRange(this IEnumerable<double> values, double min, double max) {
107      foreach (var x in values) {
108        if (double.IsNaN(x)) yield return (max + min) / 2.0;
109        else if (x < min) yield return min;
110        else if (x > max) yield return max;
111        else yield return x;
112      }
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.