Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12344 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 4.6 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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;
[3452]24using System.Linq;
[2]25
[3462]26namespace HeuristicLab.Common {
27  public static class EnumerableStatisticExtensions {
[2]28    /// <summary>
[3452]29    /// Calculates the median element of the enumeration.
[2]30    /// </summary>
31    /// <param name="values"></param>
32    /// <returns></returns>
[3452]33    public static double Median(this IEnumerable<double> values) {
[5809]34      // iterate only once
35      double[] valuesArr = values.ToArray();
36      int n = valuesArr.Length;
[3452]37      if (n == 0) throw new InvalidOperationException("Enumeration contains no elements.");
[2]38
[5809]39      Array.Sort(valuesArr);
[2]40
41      // return the middle element (if n is uneven) or the average of the two middle elements if n is even.
[1786]42      if (n % 2 == 1) {
[5809]43        return valuesArr[n / 2];
[2]44      } else {
[5809]45        return (valuesArr[(n / 2) - 1] + valuesArr[n / 2]) / 2.0;
[2]46      }
47    }
48
[8564]49    /// <summary>
50    /// Calculates the range (max - min) of the enumeration.
51    /// </summary>
52    /// <param name="values"></param>
53    /// <returns></returns>
54    public static double Range(this IEnumerable<double> values) {
55      double min = double.PositiveInfinity;
56      double max = double.NegativeInfinity;
57      int i = 0;
58      foreach (var e in values) {
59        if (min > e) min = e;
60        if (max < e) max = e;
61        i++;
62      }
[8605]63      if (i < 1) throw new ArgumentException("The enumerable must contain at least two elements", "values");
[8564]64      return max - min;
65    }
[2]66
[8564]67
[2]68    /// <summary>
69    /// Calculates the standard deviation of values.
70    /// </summary>
71    /// <param name="values"></param>
72    /// <returns></returns>
[3452]73    public static double StandardDeviation(this IEnumerable<double> values) {
[2]74      return Math.Sqrt(Variance(values));
75    }
76
77    /// <summary>
[3452]78    /// Calculates the variance of values. (sum (x - x_mean)² / n)
[2]79    /// </summary>
80    /// <param name="values"></param>
81    /// <returns></returns>
[3452]82    public static double Variance(this IEnumerable<double> values) {
[3984]83      int m_n = 0;
84      double m_oldM = 0.0;
85      double m_newM = 0.0;
86      double m_oldS = 0.0;
87      double m_newS = 0.0;
88      foreach (double x in values) {
89        m_n++;
90        if (m_n == 1) {
91          m_oldM = m_newM = x;
92          m_oldS = 0.0;
93        } else {
94          m_newM = m_oldM + (x - m_oldM) / m_n;
95          m_newS = m_oldS + (x - m_oldM) * (x - m_newM);
[2]96
[3984]97          // set up for next iteration
98          m_oldM = m_newM;
99          m_oldS = m_newS;
[2]100        }
101      }
[3984]102      return ((m_n > 1) ? m_newS / (m_n - 1) : 0.0);
[2]103    }
[4652]104
105    /// <summary>
106    /// Calculates the pth percentile of the values.
107    /// </summary
108    public static double Percentile(this IEnumerable<double> values, double p) {
[5809]109      // iterate only once
110      double[] valuesArr = values.ToArray();
111      int n = valuesArr.Length;
112      if (n == 0) throw new InvalidOperationException("Enumeration contains no elements.");
113      if (n == 1) return values.ElementAt(0);
[4652]114
[5809]115      if (p.IsAlmost(0.0)) return valuesArr[0];
116      if (p.IsAlmost(1.0)) return valuesArr[n - 1];
[4652]117
118      double t = p * (n - 1);
119      int index = (int)Math.Floor(t);
120      double percentage = t - index;
[5809]121      return valuesArr[index] * (1 - percentage) + valuesArr[index + 1] * percentage;
[4652]122    }
[5809]123
124    public static IEnumerable<double> LimitToRange(this IEnumerable<double> values, double min, double max) {
[8531]125      if (min > max) throw new ArgumentException(string.Format("Minimum {0} is larger than maximum {1}.", min, max));
[5809]126      foreach (var x in values) {
127        if (double.IsNaN(x)) yield return (max + min) / 2.0;
128        else if (x < min) yield return min;
129        else if (x > max) yield return max;
130        else yield return x;
131      }
132    }
[2]133  }
134}
Note: See TracBrowser for help on using the repository browser.