Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4023 was 3984, checked in by gkronber, 14 years ago

Changed implementation of extension method for the calculation of std. dev. of IEnumerable<double> to a routine which calculates the std. dev. in a single iteration over the enumerable. #938

File size: 2.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Text;
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      int n = values.Count();
36      if (n == 0) throw new InvalidOperationException("Enumeration contains no elements.");
37
38      double[] sortedValues = new double[n];
39      int i = 0;
40      foreach (double x in values)
41        sortedValues[i++] = x;
42
43      Array.Sort(sortedValues);
44
45      // return the middle element (if n is uneven) or the average of the two middle elements if n is even.
46      if (n % 2 == 1) {
47        return sortedValues[n / 2];
48      } else {
49        return (sortedValues[(n / 2) - 1] + sortedValues[n / 2]) / 2.0;
50      }
51    }
52
53
54    /// <summary>
55    /// Calculates the standard deviation of values.
56    /// </summary>
57    /// <param name="values"></param>
58    /// <returns></returns>
59    public static double StandardDeviation(this IEnumerable<double> values) {
60      return Math.Sqrt(Variance(values));
61    }
62
63    /// <summary>
64    /// Calculates the variance of values. (sum (x - x_mean)² / n)
65    /// </summary>
66    /// <param name="values"></param>
67    /// <returns></returns>
68    public static double Variance(this IEnumerable<double> values) {
69      int m_n = 0;
70      double m_oldM = 0.0;
71      double m_newM = 0.0;
72      double m_oldS = 0.0;
73      double m_newS = 0.0;
74      foreach (double x in values) {
75        m_n++;
76        if (m_n == 1) {
77          m_oldM = m_newM = x;
78          m_oldS = 0.0;
79        } else {
80          m_newM = m_oldM + (x - m_oldM) / m_n;
81          m_newS = m_oldS + (x - m_oldM) * (x - m_newM);
82
83          // set up for next iteration
84          m_oldM = m_newM;
85          m_oldS = m_newS;
86        }
87      }
88      return ((m_n > 1) ? m_newS / (m_n - 1) : 0.0);
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.