Free cookie consent management tool by TermsFeed Policy Generator

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

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

Updated year of copyrights to 2012 (#1716)

File size: 3.9 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 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
49
50    /// <summary>
51    /// Calculates the standard deviation of values.
52    /// </summary>
53    /// <param name="values"></param>
54    /// <returns></returns>
[3452]55    public static double StandardDeviation(this IEnumerable<double> values) {
[2]56      return Math.Sqrt(Variance(values));
57    }
58
59    /// <summary>
[3452]60    /// Calculates the variance of values. (sum (x - x_mean)² / n)
[2]61    /// </summary>
62    /// <param name="values"></param>
63    /// <returns></returns>
[3452]64    public static double Variance(this IEnumerable<double> values) {
[3984]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);
[2]78
[3984]79          // set up for next iteration
80          m_oldM = m_newM;
81          m_oldS = m_newS;
[2]82        }
83      }
[3984]84      return ((m_n > 1) ? m_newS / (m_n - 1) : 0.0);
[2]85    }
[4652]86
87    /// <summary>
88    /// Calculates the pth percentile of the values.
89    /// </summary
90    public static double Percentile(this IEnumerable<double> values, double p) {
[5809]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);
[4652]96
[5809]97      if (p.IsAlmost(0.0)) return valuesArr[0];
98      if (p.IsAlmost(1.0)) return valuesArr[n - 1];
[4652]99
100      double t = p * (n - 1);
101      int index = (int)Math.Floor(t);
102      double percentage = t - index;
[5809]103      return valuesArr[index] * (1 - percentage) + valuesArr[index + 1] * percentage;
[4652]104    }
[5809]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    }
[2]114  }
115}
Note: See TracBrowser for help on using the repository browser.