Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.DataAnalysis/3.2/Statistics.cs @ 3758

Last change on this file since 3758 was 2136, checked in by gkronber, 15 years ago

Improved handling of exceptional cases in data-based modeling evaluators. #688 (SimpleEvaluators should handle exceptional cases more gracefully)

File size: 7.5 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 = double.PositiveInfinity;
106      double maximum = double.NegativeInfinity;
107      for (int i = start; i < end; i++) {
108        if (!double.IsNaN(values[i])) {
109          if (values[i] > maximum) {
110            maximum = values[i];
111          }
112          if (values[i] < minimum) {
113            minimum = values[i];
114          }
115        }
116      }
117      return (maximum - minimum);
118    }
119
120    /// <summary>
121    /// Calculates the mean of all values.
122    /// </summary>
123    /// <param name="values"></param>
124    /// <returns></returns>
125    public static double Mean(List<double> values) {
126      return Mean(values.ToArray(), 0, values.Count);
127    }
128
129    // Calculates the mean of all values.
130    public static double Mean(double[] values) {
131      return Mean(values, 0, values.Length);
132    }
133
134    /// <summary>
135    /// Calculates the mean of the values between start and end.
136    /// </summary>
137    /// <param name="values"></param>
138    /// <param name="start">start index (inclusive)</param>
139    /// <param name="end">end index(exclusive)</param>
140    /// <returns></returns>
141    public static double Mean(double[] values, int start, int end) {
142      if (values.Length == 0) throw new ArgumentException("Values is empty.");
143      if(end <=start) throw new ArgumentException("End is smaller or equal start");
144      double sum = 0.0;
145      int n = 0;
146      for (int i = start; i < end; i++) {
147        if (!double.IsNaN(values[i])) {
148          sum += values[i];
149          n++;
150        }
151      }
152      if (n > 0)
153        return sum / n;
154      else throw new ArgumentException("Only NaN elements in values");
155    }
156
157    /// <summary>
158    /// Calculates the median of the values.
159    /// </summary>
160    /// <param name="values"></param>
161    /// <returns></returns>
162    public static double Median(double[] values) {
163      if (values.Length == 0) throw new InvalidOperationException();
164      int n = values.Length;
165      if (n == 0)
166        return 0;
167
168      double[] sortedValues = new double[n];
169
170      Array.Copy(values, sortedValues, n);
171      Array.Sort(sortedValues);
172
173      // return the middle element (if n is uneven) or the average of the two middle elements if n is even.
174      if (n % 2 == 1) {
175        return sortedValues[n / 2];
176      } else {
177        return (sortedValues[n / 2] + sortedValues[n / 2 + 1]) / 2.0;
178      }
179    }
180
181
182    /// <summary>
183    /// Calculates the standard deviation of values.
184    /// </summary>
185    /// <param name="values"></param>
186    /// <returns></returns>
187    public static double StandardDeviation(double[] values) {
188      return Math.Sqrt(Variance(values));
189    }
190
191    /// <summary>
192    /// Calculates the variance of values.
193    /// </summary>
194    /// <param name="values"></param>
195    /// <returns></returns>
196    public static double Variance(double[] values) {
197      return Variance(values, 0, values.Length);
198    }
199
200
201    /// <summary>
202    /// Calculates the variance of the entries of values between start and end.
203    /// </summary>
204    /// <param name="values"></param>
205    /// <param name="start">start index (inclusive)</param>
206    /// <param name="end">end index (exclusive)</param>
207    /// <returns></returns>
208    public static double Variance(double[] values, int start, int end) {
209      if (values.Length == 0) throw new ArgumentException("Values is empty.");
210      if (end <= start) throw new ArgumentException("End is smaller or equal start");
211      if (end - start == 1)
212        return 0.0;
213
214      double mean = Mean(values, start, end);
215      double squaredErrorsSum = 0.0;
216
217      int n = 0;
218      for (int i = start; i < end; i++) {
219        if (!double.IsNaN(values[i])) {
220          double d = values[i] - mean;
221          squaredErrorsSum += d * d;
222          n++;
223        }
224      }
225      if (n < 2) {
226        throw new ArgumentException("Only one non-NaN element in values");
227      }
228      return squaredErrorsSum / (n - 1);
229    }
230  }
231}
Note: See TracBrowser for help on using the repository browser.