Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataAnalysis/Statistics.cs @ 1325

Last change on this file since 1325 was 1071, checked in by gkronber, 16 years ago

fixed #448 (Calculation of mean throws an exception if the input data contains NaN values).

File size: 7.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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;
25
26namespace HeuristicLab.DataAnalysis {
27  public class Statistics {
28
29    /// <summary>
30    /// Minimum returns the smalles entry of values.
31    /// Throws and exception if values is empty.
32    /// </summary>
33    /// <typeparam name="T"></typeparam>
34    /// <param name="values"></param>
35    /// <returns></returns>
36    public static T Minimum<T>(IEnumerable<T> values) where T : struct, IComparable, IComparable<T> {
37      IEnumerator<T> enumerator = values.GetEnumerator();
38
39      // this will throw an exception if the values collection is empty
40      enumerator.MoveNext();
41      T minimum = enumerator.Current;
42
43      while(enumerator.MoveNext()) {
44        T current = enumerator.Current;
45        if(current.CompareTo(minimum) < 0) {
46          minimum = current;
47        }
48      }
49
50      return minimum;
51    }
52
53    /// <summary>
54    /// Maximum returns the largest entry of values.
55    /// Throws an exception if values is empty.
56    /// </summary>
57    /// <typeparam name="T"></typeparam>
58    /// <param name="values"></param>
59    /// <returns></returns>
60    public static T Maximum<T>(IEnumerable<T> values) where T : struct, IComparable, IComparable<T> {
61      IEnumerator<T> enumerator = values.GetEnumerator();
62
63      // this will throw an exception if the values collection is empty
64      enumerator.MoveNext();
65      T maximum = enumerator.Current;
66
67      while(enumerator.MoveNext()) {
68        T current = enumerator.Current;
69        if(current.CompareTo(maximum) > 0) {
70          maximum = current;
71        }
72      }
73
74      return maximum;
75    }
76
77    /// <summary>
78    /// Range calculates the difference between the larges and smallest entry of values.
79    /// </summary>
80    /// <param name="values"></param>
81    /// <returns></returns>
82    public static double Range(double[] values) {
83      return Range(values, 0, values.Length);
84    }
85
86    /// <summary>
87    /// Range calculates the difference between the larges and smallest entry of values.
88    /// </summary>
89    public static double Range(List<double> values) {
90      return Range(values.ToArray(), 0, values.Count);
91    }
92
93    /// <summary>
94    /// Range calculates the difference between the largest and smallest entry of values between start and end.
95    /// </summary>
96    /// <param name="values">collection of values</param>
97    /// <param name="start">start index (inclusive)</param>
98    /// <param name="end">end index (exclusive)</param>
99    /// <returns></returns>
100    public static double Range(double[] values, int start, int end) {
101      if(start < 0 || start > values.Length || end < 0 || end > values.Length || start > end) {
102        throw new InvalidOperationException();
103      }
104
105      double minimum = values[start];
106      double maximum = minimum;
107      for(int i = start; i < end; i++) {
108        if(values[i] > maximum) {
109          maximum = values[i];
110        }
111        if(values[i] < minimum) {
112          minimum = values[i];
113        }
114      }
115      return (maximum - minimum);
116    }
117
118    /// <summary>
119    /// calculates the sum of all values.
120    /// </summary>
121    /// <param name="values"></param>
122    /// <returns></returns>
123    public static double Sum(double[] values) {
124      int n = values.Length;
125      double sum = 0.0;
126      for(int i = 0; i < n; i++) {
127        if(double.IsNaN(values[i])) {
128          throw new NotFiniteNumberException();
129        } else {
130          sum += values[i];
131        }
132      }
133      return sum;
134    }
135
136    /// <summary>
137    /// Calculates the mean of all values.
138    /// </summary>
139    /// <param name="values"></param>
140    /// <returns></returns>
141    public static double Mean(List<double> values) {
142      return Mean(values.ToArray(), 0, values.Count);
143    }
144
145    // Calculates the mean of all values.
146    public static double Mean(double[] values) {
147      return Mean(values, 0, values.Length);
148    }
149
150    /// <summary>
151    /// Calculates the mean of the values between start and end.
152    /// </summary>
153    /// <param name="values"></param>
154    /// <param name="start">start index (inclusive)</param>
155    /// <param name="end">end index(exclusive)</param>
156    /// <returns></returns>
157    public static double Mean(double[] values, int start, int end) {
158      if(values.Length == 0) throw new InvalidOperationException();
159      double sum = 0.0;
160      int n = 0;
161      for(int i = start; i < end; i++) {
162        if(!double.IsNaN(values[i])) {
163          sum += values[i];
164          n++;
165        }
166      }
167      if (n == 0) throw new InvalidOperationException();
168      return sum / n;
169    }
170
171    /// <summary>
172    /// Calculates the median of the values.
173    /// </summary>
174    /// <param name="values"></param>
175    /// <returns></returns>
176    public static double Median(double[] values) {
177      if(values.Length == 0) throw new InvalidOperationException();
178      int n = values.Length;
179      if(n == 0)
180        return 0;
181
182      double[] sortedValues = new double[n];
183
184      Array.Copy(values, sortedValues, n);
185      Array.Sort(sortedValues);
186
187      // return the middle element (if n is uneven) or the average of the two middle elements if n is even.
188      if(n % 2 == 1) {
189        return sortedValues[n / 2];
190      } else {
191        return (sortedValues[n / 2] + sortedValues[n / 2 + 1]) / 2.0;
192      }
193    }
194
195
196    /// <summary>
197    /// Calculates the standard deviation of values.
198    /// </summary>
199    /// <param name="values"></param>
200    /// <returns></returns>
201    public static double StandardDeviation(double[] values) {
202      return Math.Sqrt(Variance(values));
203    }
204
205    /// <summary>
206    /// Calculates the variance of values.
207    /// </summary>
208    /// <param name="values"></param>
209    /// <returns></returns>
210    public static double Variance(double[] values) {
211      return Variance(values, 0, values.Length);
212    }
213
214
215    /// <summary>
216    /// Calculates the variance of the entries of values between start and end.
217    /// </summary>
218    /// <param name="values"></param>
219    /// <param name="start">start index (inclusive)</param>
220    /// <param name="end">end index (exclusive)</param>
221    /// <returns></returns>
222    public static double Variance(double[] values, int start, int end) {
223      if (end - start == 1)
224        return 0.0;
225
226      double mean = Mean(values, start, end);
227      double squaredErrorsSum = 0.0;
228
229      int n = 0;
230      for (int i = start; i < end; i++) {
231        if (!double.IsNaN(values[i])) {
232          double d = values[i] - mean;
233          squaredErrorsSum += d * d;
234          n++;
235        }
236      }
237      if (n < 2) {
238        throw new InvalidOperationException();
239      }
240      return squaredErrorsSum / (n - 1);
241    }
242  }
243}
Note: See TracBrowser for help on using the repository browser.