Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13025 was 13025, checked in by gkronber, 9 years ago

#2488 implemented quantile extension method in HeuristicLab.Common

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Diagnostics.Contracts;
25using System.Linq;
26
27namespace 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      // iterate only once
36      double[] valuesArr = values.ToArray();
37      int n = valuesArr.Length;
38      if (n == 0) throw new InvalidOperationException("Enumeration contains no elements.");
39
40      Array.Sort(valuesArr);
41
42      // return the middle element (if n is uneven) or the average of the two middle elements if n is even.
43      if (n % 2 == 1) {
44        return valuesArr[n / 2];
45      } else {
46        return (valuesArr[(n / 2) - 1] + valuesArr[n / 2]) / 2.0;
47      }
48    }
49
50    /// <summary>
51    /// Calculates the alpha-quantile element of the enumeration.
52    /// </summary>
53    /// <param name="values"></param>
54    /// <returns></returns>
55    public static double Quantile(this IEnumerable<double> values, double alpha) {
56      Contract.Assert(alpha > 0 && alpha < 1);
57      // iterate only once
58      double[] valuesArr = values.ToArray();
59      int n = valuesArr.Length;
60      if (n == 0) throw new InvalidOperationException("Enumeration contains no elements.");
61
62      Array.Sort(valuesArr);
63      //  starts at 0
64
65      // return the element at Math.Ceiling (if n*alpha is fractional) or the average of two elements if n*alpha is integer.
66      var pos = n * alpha;
67      Contract.Assert(pos >= 0);
68      Contract.Assert(pos < n);
69      bool isInteger = Math.Round(pos).IsAlmost(pos);
70      if (isInteger) {
71        return 0.5 * (valuesArr[(int)pos - 1] + valuesArr[(int)pos]);
72      } else {
73        return valuesArr[(int)Math.Ceiling(pos) - 1];
74      }
75    }
76
77    /// <summary>
78    /// Calculates the range (max - min) of the enumeration.
79    /// </summary>
80    /// <param name="values"></param>
81    /// <returns></returns>
82    public static double Range(this IEnumerable<double> values) {
83      double min = double.PositiveInfinity;
84      double max = double.NegativeInfinity;
85      int i = 0;
86      foreach (var e in values) {
87        if (min > e) min = e;
88        if (max < e) max = e;
89        i++;
90      }
91      if (i < 1) throw new ArgumentException("The enumerable must contain at least two elements", "values");
92      return max - min;
93    }
94
95
96    /// <summary>
97    /// Calculates the standard deviation of values.
98    /// </summary>
99    /// <param name="values"></param>
100    /// <returns></returns>
101    public static double StandardDeviation(this IEnumerable<double> values) {
102      return Math.Sqrt(Variance(values));
103    }
104
105    /// <summary>
106    /// Calculates the variance of values. (sum (x - x_mean)² / n)
107    /// </summary>
108    /// <param name="values"></param>
109    /// <returns></returns>
110    public static double Variance(this IEnumerable<double> values) {
111      int m_n = 0;
112      double m_oldM = 0.0;
113      double m_newM = 0.0;
114      double m_oldS = 0.0;
115      double m_newS = 0.0;
116      foreach (double x in values) {
117        m_n++;
118        if (m_n == 1) {
119          m_oldM = m_newM = x;
120          m_oldS = 0.0;
121        } else {
122          m_newM = m_oldM + (x - m_oldM) / m_n;
123          m_newS = m_oldS + (x - m_oldM) * (x - m_newM);
124
125          // set up for next iteration
126          m_oldM = m_newM;
127          m_oldS = m_newS;
128        }
129      }
130      return ((m_n > 1) ? m_newS / (m_n - 1) : 0.0);
131    }
132
133    /// <summary>
134    /// Calculates the pth percentile of the values.
135    /// </summary
136    public static double Percentile(this IEnumerable<double> values, double p) {
137      // iterate only once
138      double[] valuesArr = values.ToArray();
139      int n = valuesArr.Length;
140      if (n == 0) throw new InvalidOperationException("Enumeration contains no elements.");
141      if (n == 1) return values.ElementAt(0);
142
143      if (p.IsAlmost(0.0)) return valuesArr[0];
144      if (p.IsAlmost(1.0)) return valuesArr[n - 1];
145
146      double t = p * (n - 1);
147      int index = (int)Math.Floor(t);
148      double percentage = t - index;
149      return valuesArr[index] * (1 - percentage) + valuesArr[index + 1] * percentage;
150    }
151
152    public static IEnumerable<double> LimitToRange(this IEnumerable<double> values, double min, double max) {
153      if (min > max) throw new ArgumentException(string.Format("Minimum {0} is larger than maximum {1}.", min, max));
154      foreach (var x in values) {
155        if (double.IsNaN(x)) yield return (max + min) / 2.0;
156        else if (x < min) yield return min;
157        else if (x > max) yield return max;
158        else yield return x;
159      }
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.